Commit 50073928 authored by Romain Courteaud's avatar Romain Courteaud

slapos_upgrader: migrate existing subscription requests/projects

parent 7d347f68
portal = context.getPortalObject() portal = context.getPortalObject()
tag = script.id tag = script.id
not_migrated_select_dict={'default_follow_up_uid': None}
############################################
# Delete all object to drop
############################################
packet_kw = {
'activate_kw': {'tag': tag},# 'serialization_tag': tag},
'packet_size': 1, # Separate calls to many transactions (calculation can take time)
'activity_count': 1000,
}
portal.portal_catalog.searchAndActivate(
method_id='Base_deleteProcessedDocumentDuringPurge',
portal_type=["Sale Packing List"],
**packet_kw
)
portal.portal_catalog.searchAndActivate(
method_id='Base_deleteProcessedDocumentDuringPurge',
portal_type=["Upgrade Decision"],
**packet_kw
)
portal.portal_catalog.searchAndActivate(
method_id='Base_deleteProcessedDocumentDuringPurge',
portal_type=["Applied Rule"],
parent_uid=portal.portal_simulation.getUid(),
**packet_kw
)
portal.portal_catalog.searchAndActivate(
method_id='Base_deleteProcessedDocumentDuringPurge',
portal_type=["Open Sale Order"],
**packet_kw
)
portal.portal_catalog.searchAndActivate(
method_id='Base_deleteProcessedDocumentDuringPurge',
portal_type=["Computer Consumption TioXML File"],
**packet_kw
)
portal.portal_catalog.searchAndActivate(
method_id='Base_deleteProcessedDocumentDuringPurge',
portal_type=["Regularisation Request"],
**packet_kw
)
portal.portal_catalog.searchAndActivate(
method_id='Base_deleteProcessedDocumentDuringPurge',
portal_type=["Active Process"],
**packet_kw
)
############################################
# Gather compute node informations
############################################
portal.portal_catalog.searchAndActivate(
method_id='ComputeNode_checkSiteMigrationToVirtualMaster',
activate_kw={'tag': tag, 'priority': 1},
portal_type="Compute Node",
select_dict=not_migrated_select_dict,
left_join_list=not_migrated_select_dict.keys(),
**not_migrated_select_dict
)
############################################
# Gather instance tree informations
############################################
portal.portal_catalog.searchAndActivate(
method_id='InstanceTree_checkSiteMigrationToVirtualMaster',
activate_kw={'tag': tag, 'priority': 1},
portal_type="Instance Tree",
select_dict=not_migrated_select_dict,
left_join_list=not_migrated_select_dict.keys(),
**not_migrated_select_dict
)
context.activate(after_tag=tag).getId() ### Create the bootstrap Trade Condition
currency = portal.restrictedTraverse('currency_module/EUR')
seller_organisation = portal.restrictedTraverse('organisation_module/rapidspace')
# Sale Trade Condition for Tax
sale_trade_condition = portal.sale_trade_condition_module.newContent(
portal_type="Sale Trade Condition",
reference="Tax/payment for: %s" % currency.getRelativeUrl(),
trade_condition_type="default",
# XXX hardcoded
specialise="business_process_module/slapos_ultimate_business_process",
price_currency_value=currency
)
sale_trade_condition.newContent(
portal_type="Trade Model Line",
reference="VAT",
resource="service_module/slapos_tax",
base_application="base_amount/invoicing/taxable",
trade_phase="slapos/tax",
price=0.2,
quantity=1.0,
membership_criterion_base_category=('price_currency', 'base_contribution'),
membership_criterion_category=('price_currency/%s' % currency.getRelativeUrl(), 'base_contribution/base_amount/invoicing/taxable')
)
sale_trade_condition.validate()
sale_trade_condition.reindexObject(activate_kw={'tag': tag})
# Create Trade Condition to create Project
sale_trade_condition = portal.sale_trade_condition_module.newContent(
portal_type="Sale Trade Condition",
reference="Free Virtual Master (%s)" % seller_organisation.getTitle(),
trade_condition_type="virtual_master",
specialise_value=sale_trade_condition,
source_value=seller_organisation,
price_currency_value=currency,
)
sale_trade_condition.validate()
sale_trade_condition.reindexObject(activate_kw={'tag': tag})
return context.activate(after_tag=tag).Base_triggerFullSiteMigrationToVirtualMasterStep2()
portal = context.getPortalObject()
tag = script.id
not_migrated_select_dict={'default_follow_up_uid': None}
############################################
# Delete all object to drop
############################################
packet_kw = {
'activate_kw': {'tag': tag},# 'serialization_tag': tag},
'packet_size': 1, # Separate calls to many transactions (calculation can take time)
'activity_count': 1000,
}
portal.portal_catalog.searchAndActivate(
method_id='Base_deleteProcessedDocumentDuringPurge',
portal_type=["Upgrade Decision"],
**packet_kw
)
portal.portal_catalog.searchAndActivate(
method_id='Base_deleteProcessedDocumentDuringPurge',
portal_type=["Regularisation Request"],
**packet_kw
)
############################################
# Gather compute node informations
############################################
portal.portal_catalog.searchAndActivate(
method_id='ComputeNode_checkSiteMigrationToVirtualMaster',
activate_kw={'tag': tag, 'priority': 1},
portal_type="Compute Node",
select_dict=not_migrated_select_dict,
left_join_list=not_migrated_select_dict.keys(),
**not_migrated_select_dict
)
############################################
# Gather instance tree informations
############################################
portal.portal_catalog.searchAndActivate(
method_id='InstanceTree_checkSiteMigrationToVirtualMaster',
activate_kw={'tag': tag, 'priority': 1},
portal_type="Instance Tree",
select_dict=not_migrated_select_dict,
left_join_list=not_migrated_select_dict.keys(),
**not_migrated_select_dict
)
portal.project_module.activate(**{'tag': tag, 'priority': 1}).ProjectModule_triggerObjectMigrationToVirtualMaster()
context.activate(after_tag=tag).getId()
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="PythonScript" module="Products.PythonScripts.PythonScript"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>_bind_names</string> </key>
<value>
<object>
<klass>
<global name="_reconstructor" module="copy_reg"/>
</klass>
<tuple>
<global name="NameAssignments" module="Shared.DC.Scripts.Bindings"/>
<global name="object" module="__builtin__"/>
<none/>
</tuple>
<state>
<dictionary>
<item>
<key> <string>_asgns</string> </key>
<value>
<dictionary>
<item>
<key> <string>name_container</string> </key>
<value> <string>container</string> </value>
</item>
<item>
<key> <string>name_context</string> </key>
<value> <string>context</string> </value>
</item>
<item>
<key> <string>name_m_self</string> </key>
<value> <string>script</string> </value>
</item>
<item>
<key> <string>name_subpath</string> </key>
<value> <string>traverse_subpath</string> </value>
</item>
</dictionary>
</value>
</item>
</dictionary>
</state>
</object>
</value>
</item>
<item>
<key> <string>_params</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>Base_triggerFullSiteMigrationToVirtualMasterStep2</string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
from zExceptions import Unauthorized
if REQUEST is not None:
raise Unauthorized
portal = context.getPortalObject()
activate_kw = None
##########################################################################
## Find the list of all project where instances must be paid
##########################################################################
payable_project_guid_dict = {}
for sql_result in portal.portal_catalog(
portal_type="Subscription Request",
):
subscription_request = sql_result.getObject()
instance_tree = subscription_request.getAggregateValue(portal_type="Instance Tree")
if (instance_tree is not None):
context.log('COUSCOUS %s' % subscription_request.getRelativeUrl())
# Build the list of prices per project/software release
project_guid = instance_tree.getSuccessorValue().getSlaXmlAsDict().get('project_guid', None)
if project_guid is not None:
payable_project_guid_dict[project_guid] = subscription_request.getPriceCurrency()
# Delete existing subscription request, which must be recreated later
subscription_request.getParentValue().manage_delObjects(ids=[subscription_request.getId()])
##########################################################################
## Migrate all existing projects
##########################################################################
# Copied from Person_addVirtualMaster
project_resource = portal.restrictedTraverse("service_module/slapos_virtual_master_subscription")
for sql_result in portal.portal_catalog(
portal_type="Project",
):
project = sql_result.getObject()
if project.getDestinationValue(None) is None:
# Project is not migrated
customer = project.getDestinationDecisionValue(None)
if (customer is None) or (project.getValidationState() != 'validated'):
# No idea how to migrate this project
continue
project.edit(
destination_value=customer,
destination_decision_value=None
)
is_compute_node_payable = False
is_instance_tree_payable = False
# XXX hardcoded
price_currency = 'currency_module/EUR'
if project.getReference() in payable_project_guid_dict:
is_instance_tree_payable = True
price_currency = payable_project_guid_dict[project.getReference()]
currency_value = portal.restrictedTraverse(price_currency)
# create the subscription request, which will lead to the Open Order
subscription_request = project_resource.Resource_createSubscriptionRequest(customer, [], project, currency_value=currency_value)
subscription_request.reindexObject(activate_kw=activate_kw)
# XXX How to specify the trade condition containing the currency and trade model lines?
specialise_value = subscription_request.getSpecialiseValue(portal_type="Sale Trade Condition")
if ((specialise_value is not None) and
(len(specialise_value.contentValues(portal_type="Trade Model Line")) == 0)):
specialise_value = specialise_value.getSpecialiseValue(portal_type="Sale Trade Condition")
if ((specialise_value is None) or
(len(specialise_value.contentValues(portal_type="Trade Model Line")) == 0)):
raise ValueError('Could not find a Trade Condition with Trade Model Line')
# and create default assignments for the user
# who can manage compute nodes and create instances
customer.newContent(
portal_type='Assignment',
destination_project_value=project,
function='production/manager',
activate_kw=activate_kw
).open()
customer.newContent(
portal_type='Assignment',
destination_project_value=project,
function='customer',
activate_kw=activate_kw
).open()
for is_payable, portal_type, trade_condition_type in [
(is_compute_node_payable, 'Compute Node', "compute_node"),
(is_instance_tree_payable, 'Instance Tree', "instance_tree"),
]:
if is_payable:
source_section_value = subscription_request.getSourceSectionValue(
default=subscription_request.getSourceValue(default=None, portal_type='Organisation'),
portal_type='Organisation'
)
if source_section_value is None:
raise AssertionError('No source section found to generate the invoices')
title = 'Payable %s for: %s' % (portal_type, project.getTitle())
else:
source_section_value = None
title = 'Free %s for: %s' % (portal_type, project.getTitle())
sale_trade_condition = portal.sale_trade_condition_module.newContent(
portal_type="Sale Trade Condition",
reference=title,
trade_condition_type=trade_condition_type,
specialise_value=specialise_value,
source_project_value=project,
source_value=subscription_request.getSourceValue(),
source_section_value=source_section_value,
#source_payment_value=seller_bank_account,
price_currency_value=currency_value,
activate_kw=activate_kw
)
sale_trade_condition.validate()
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="PythonScript" module="Products.PythonScripts.PythonScript"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>_bind_names</string> </key>
<value>
<object>
<klass>
<global name="_reconstructor" module="copy_reg"/>
</klass>
<tuple>
<global name="NameAssignments" module="Shared.DC.Scripts.Bindings"/>
<global name="object" module="__builtin__"/>
<none/>
</tuple>
<state>
<dictionary>
<item>
<key> <string>_asgns</string> </key>
<value>
<dictionary>
<item>
<key> <string>name_container</string> </key>
<value> <string>container</string> </value>
</item>
<item>
<key> <string>name_context</string> </key>
<value> <string>context</string> </value>
</item>
<item>
<key> <string>name_m_self</string> </key>
<value> <string>script</string> </value>
</item>
<item>
<key> <string>name_subpath</string> </key>
<value> <string>traverse_subpath</string> </value>
</item>
</dictionary>
</value>
</item>
</dictionary>
</state>
</object>
</value>
</item>
<item>
<key> <string>_params</string> </key>
<value> <string>REQUEST=None</string> </value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>ProjectModule_triggerObjectMigrationToVirtualMaster</string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment