<?xml version="1.0"?>
<ZopeData>
  <record id="1" aka="AAAAAAAAAAE=">
    <pickle>
      <global name="PythonScript" module="Products.PythonScripts.PythonScript"/>
    </pickle>
    <pickle>
      <dictionary>
        <item>
            <key> <string>Script_magic</string> </key>
            <value> <int>3</int> </value>
        </item>
        <item>
            <key> <string>_bind_names</string> </key>
            <value>
              <object>
                <klass>
                  <global name="NameAssignments" module="Shared.DC.Scripts.Bindings"/>
                </klass>
                <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>_body</string> </key>
            <value> <string encoding="cdata"><![CDATA[

software_instance = state_change[\'object\']\n
portal = software_instance.getPortalObject()\n
# Get required arguments\n
kwargs = state_change.kwargs\n
software_release_url_string = state_change.kwargs[\'software_release\']\n
requested_partition_reference = kwargs["partition_reference"]\n
software_type = kwargs["software_type"]\n
instance_xml = kwargs["instance_xml"]\n
is_slave = kwargs.get("shared", False)\n
sla_xml = kwargs["sla_xml"]\n
state = kwargs["state"]\n
\n
if is_slave == True:\n
  software_instance_portal_type = "Slave Instance"\n
else:\n
  software_instance_portal_type = "Software Instance"\n
\n
# graph allows to "simulate" tree change after requested operation\n
graph = {}\n
# Get root software instance and create initial graph\n
predecessor_software_instance = software_instance\n
while (predecessor_software_instance is not None):\n
  predecessor_software_instance_pred_uid_list = predecessor_software_instance.getPredecessorUidList()\n
  graph[predecessor_software_instance.getUid()] = predecessor_software_instance_pred_uid_list\n
  # as this walking is not fetching all instances fill predecessors into graph, in order to have\n
  # "nearly" complete representation\n
  for uid in predecessor_software_instance_pred_uid_list:\n
    if uid not in graph:\n
      graph[uid] = []\n
  root_software_instance = predecessor_software_instance\n
  predecessor_software_instance = predecessor_software_instance.getPredecessorRelatedValue(\n
      portal_type="Software Instance")\n
\n
tag = "%s_%s_inProgress" % (root_software_instance.getUid(),\n
                               requested_partition_reference)\n
\n
# Check if it already exists\n
request_software_instance = software_instance.portal_catalog.getResultValue(\n
  portal_type=software_instance_portal_type,\n
  # XXX: User based property is used in non manual operation\n
  # XXX-2: Do we really need to use root_uid?\n
  title=requested_partition_reference,\n
  root_uid=root_software_instance.getUid(),\n
)\n
\n
# above query does not find root software instance, but as this case is easy\n
# to find, just check if such request does not come and raise\n
if root_software_instance.getTitle() == requested_partition_reference:\n
  raise ValueError(\'It is disallowed to request root software instance\')\n
\n
if (request_software_instance is None):\n
  if (portal.portal_activities.countMessageWithTag(tag) > 0):\n
    # The software instance is already under creation but can not be fetched from catalog\n
    # As it is not possible to fetch informations, it is better to raise an error\n
    raise NotImplementedError(tag)\n
  else:\n
    # First time that the software instance is requested\n
    # Create a new one\n
    module = software_instance.getDefaultModule(portal_type="Software Instance")\n
    request_software_instance = module.newContent(\n
      portal_type=software_instance_portal_type,\n
      title=requested_partition_reference,\n
      source_reference=software_type,\n
      text_content=instance_xml,\n
      sla_xml=sla_xml,\n
      activate_kw={\'tag\': tag},\n
      **portal.Base_getNewSoftwareInstanceCoordinate()\n
    )\n
    request_software_instance.portal_workflow.doActionFor(request_software_instance, \'validate_action\')\n
    sale_packing_list_line = context.SoftwareInstance_getInstanceSetupPackingListLine(state_change)\n
    hosting_subscription_uid = sale_packing_list_line.getAggregateValue(portal_type=\'Hosting Subscription\').getUid()\n
    request_software_instance.requestComputerPartition(\n
      software_release=software_release_url_string,\n
      hosting_subscription_uid=hosting_subscription_uid,\n
      software_type=software_type,\n
      tag=tag)\n
else:\n
  # Update existing software instance\n
  # Sale Packing List interaction has to be requested automatically with an interaction workflow\n
  request_software_instance.edit(\n
    text_content=instance_xml,\n
    source_reference=software_type,\n
    sla_xml=sla_xml,\n
    activate_kw={\'tag\': tag},\n
  )\n
  # Update the predecessor category of the original caller\n
  predecessor_software_instance = request_software_instance.getPredecessorRelatedValue(\n
    portal_type="Software Instance")\n
  if predecessor_software_instance is None:\n
    raise ValueError(\'Requested Software Instance %s should have a predecessor\' % request_software_instance.getRelativeUrl())\n
  else:\n
    predecessor_uid_list = predecessor_software_instance.getPredecessorUidList()\n
    predecessor_uid_list.remove(request_software_instance.getUid())\n
    predecessor_software_instance.edit(\n
      predecessor_uid_list=predecessor_uid_list,\n
      activate_kw={\'tag\': tag},)\n
    graph[predecessor_software_instance.getUid()] = predecessor_uid_list\n
\n
  if state == \'started\':\n
    request_software_instance.startRequested()\n
    request_software_instance.activate(after_tag=tag).requestStartComputerPartition()\n
  elif state == \'stopped\':\n
    request_software_instance.stopRequested()    \n
    request_software_instance.activate(after_tag=tag).requestStopComputerPartition()\n
  else:\n
    raise ValueError(\'State %r is not supported\' % state)\n
predecessor_list = software_instance.getPredecessorList() + [request_software_instance.getRelativeUrl()]\n
# Add requested software instance to graph if does not exists there yet\n
if not request_software_instance.getUid() in graph:\n
  graph[request_software_instance.getUid()] = []\n
\n
# update graph to reflect requested operation\n
graph[software_instance.getUid()] = software_instance.getPredecessorUidList() + [request_software_instance.getUid()]\n
\n
# check if all elements are still connected and if there is no cycle\n
software_instance.checkConnected(graph, root_software_instance.getUid())\n
software_instance.checkNotCyclic(graph)\n
\n
software_instance.edit(\n
  predecessor_list=predecessor_list,\n
  activate_kw={\'tag\': tag},)\n


]]></string> </value>
        </item>
        <item>
            <key> <string>_params</string> </key>
            <value> <string>state_change</string> </value>
        </item>
        <item>
            <key> <string>id</string> </key>
            <value> <string>SoftwareInstance_requestSoftwareInstance</string> </value>
        </item>
      </dictionary>
    </pickle>
  </record>
</ZopeData>
