Commit 611cfd7f authored by Cédric Le Ninivin's avatar Cédric Le Ninivin Committed by Romain Courteaud

slapos_jio_api_style: Code cleanup to use Slapos Cache catalog mixin

parent 102af3de
if REQUEST:
raise ValueError("This script should not be called directly")
# No need to get all results if an error is raised when at least 2 objects
# are found
l = context.getPortalObject().portal_catalog(limit=2, select_list=("relative_url",), **kw)
if len(l) != 1:
return None
else:
return l[0].relative_url
<?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>_params</string> </key>
<value> <string>REQUEST=None, **kw</string> </value>
</item>
<item>
<key> <string>_proxy_roles</string> </key>
<value>
<tuple>
<string>Manager</string>
</tuple>
</value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>Base_getUnrestrictedDocumentUrl</string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
......@@ -5,7 +5,6 @@ if portal_type == "Software Installation":
if "compute_node_id" in data_dict and "software_release_uri" in data_dict:
compute_node = portal.portal_catalog.getComputeNodeObject(
data_dict["compute_node_id"],
raise_not_found=False,
)
if compute_node:
return compute_node.getSoftwareInstallationFromUrl(data_dict["software_release_uri"])
......
......@@ -4,12 +4,6 @@ compute_partition_id = data_dict.get("compute_partition_id", None)
class SoftwareInstanceNotReady(Exception):
pass
class NotFound(Exception):
pass
class Unauthorized(Exception):
pass
castToStr = context.Base_castDictToXMLString
def logError(e, error_name="", error_code=400, detail_list=None):
......@@ -20,73 +14,8 @@ def logError(e, error_name="", error_code=400, detail_list=None):
detail_list=detail_list,
)
LOG = context.log
portal = context.getPortalObject()
def _assertACI(document):
if context.Base_checkPermission(document.relative_url, 'View'):
return document.getObject()
raise Unauthorized('User has no access to %r' % (document.relative_url))
def getDocument(**kwargs):
# No need to get all results if an error is raised when at least 2 objects
# are found
l = portal.portal_catalog.unrestrictedSearchResults(limit=2, select_list=("relative_url", "uid"), **kwargs)
if len(l) != 1:
raise NotFound, "No document found with parameters: %s" % kwargs
else:
return _assertACI(l[0])
def getNonCachedComputeNodeUidByReference(compute_node_reference):
return portal.portal_catalog.unrestrictedSearchResults(
portal_type='Compute Node', reference=compute_node_reference,
validation_state="validated")[0].UID
def getComputePartitionDocument(compute_node_reference,
compute_partition_reference):
"""
Get the compute partition defined in an available compute_node
"""
# Related key might be nice
return getDocument(portal_type='Compute Partition',
reference=compute_partition_reference,
parent_uid=getNonCachedComputeNodeUidByReference(
compute_node_reference))
def getSoftwareInstanceForComputePartition(compute_node_id,
compute_partition_id, slave_reference=None):
compute_partition_document = getComputePartitionDocument(
compute_node_id, compute_partition_id)
if compute_partition_document.getSlapState() != 'busy':
LOG('SlapTool::_getSoftwareInstanceForComputePartition'
+ 'Compute partition %s shall be busy, is free' %
compute_partition_document.getRelativeUrl())
raise NotFound, "No software instance found for: %s - %s" % (compute_node_id,
compute_partition_id)
else:
query_kw = {
'validation_state': 'validated',
'portal_type': 'Slave Instance',
'default_aggregate_uid': compute_partition_document.getUid(),
}
if slave_reference is None:
query_kw['portal_type'] = "Software Instance"
else:
query_kw['reference'] = slave_reference
software_instance = _assertACI(portal.portal_catalog.unrestrictedGetResultValue(**query_kw))
if software_instance is None:
raise NotFound, "No software instance found for: %s - %s" % (
compute_node_id, compute_partition_id)
else:
return software_instance
# Loads partition parameter
partition_parameter = data_dict.get("parameters", None)
if partition_parameter:
......@@ -120,12 +49,13 @@ try:
shared=data_dict.get("shared", False),
sla_xml=castToStr(filter_kw),
state=data_dict.get("state", "started"))
#raise ValueError("%s" % kw)
if compute_node_id and compute_partition_id:
requester = getSoftwareInstanceForComputePartition(
compute_partition = portal.portal_catalog.getComputePartitionObject(
compute_node_id,
compute_partition_id)
compute_partition_id,
)
requester = compute_partition.getSoftwareInstance()
instance_tree = requester.getSpecialiseValue()
if instance_tree is not None and instance_tree.getSlapState() == "stop_requested":
kw['state'] = 'stopped'
......@@ -172,13 +102,3 @@ except SoftwareInstanceNotReady:
error_name="SoftwareInstanceNotReady",
error_code=102
)
except Unauthorized, log:
return logError(
log,
error_code=401
)
except NotFound, log:
return logError(
log,
error_code=404
)
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