Commit ef723de8 authored by Cédric Le Ninivin's avatar Cédric Le Ninivin Committed by Cédric Le Ninivin

slapos_jio_api_style: Add Update software installation

Can replace the following SlapTool calls:
* softwareReleaseError
* availableSoftwareRelease
* buildingSoftwareRelease
* destroyedSoftwareRelease
parent fd07f4e0
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="ActionInformation" module="Products.CMFCore.ActionInformation"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>action</string> </key>
<value>
<persistent> <string encoding="base64">AAAAAAAAAAI=</string> </persistent>
</value>
</item>
<item>
<key> <string>categories</string> </key>
<value>
<tuple>
<string>action_type/object_slapos_json_put</string>
</tuple>
</value>
</item>
<item>
<key> <string>category</string> </key>
<value> <string>object_slapos_json_put</string> </value>
</item>
<item>
<key> <string>condition</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>description</string> </key>
<value>
<none/>
</value>
</item>
<item>
<key> <string>icon</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>slapos_jio_api_put_software_installation</string> </value>
</item>
<item>
<key> <string>permissions</string> </key>
<value>
<tuple>
<string>View</string>
</tuple>
</value>
</item>
<item>
<key> <string>portal_type</string> </key>
<value> <string>Action Information</string> </value>
</item>
<item>
<key> <string>priority</string> </key>
<value> <float>2.0</float> </value>
</item>
<item>
<key> <string>title</string> </key>
<value> <string>Update Software Installation</string> </value>
</item>
<item>
<key> <string>visible</string> </key>
<value> <int>1</int> </value>
</item>
</dictionary>
</pickle>
</record>
<record id="2" aka="AAAAAAAAAAI=">
<pickle>
<global name="Expression" module="Products.CMFCore.Expression"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>text</string> </key>
<value> <string>string:${object_url}/SoftwareInstallation_updateFromJSON</string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="JSON Form" module="erp5.portal_type"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>after_method_id</string> </key>
<value> <string>SoftwareInstallation_updateFromDict</string> </value>
</item>
<item>
<key> <string>content_type</string> </key>
<value> <string>application/json</string> </value>
</item>
<item>
<key> <string>default_reference</string> </key>
<value> <string>SoftwareInstallation_updateFromJSON</string> </value>
</item>
<item>
<key> <string>description</string> </key>
<value>
<none/>
</value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>SoftwareInstallation_updateFromJSON</string> </value>
</item>
<item>
<key> <string>output_schema</string> </key>
<value> <string>{\n
"title": "bar"\n
}</string> </value>
</item>
<item>
<key> <string>portal_type</string> </key>
<value> <string>JSON Form</string> </value>
</item>
<item>
<key> <string>text_content</string> </key>
<value> <string>{\n
"$schema": "http://json-schema.org/draft-07/schema#",\n
"title": "Software Installation",\n
"description": "Software Installation",\n
"type": "object",\n
"properties": {\n
"software_release_uri": {\n
"title": "Software Release URI",\n
"type": "string",\n
"format": "uri-reference"\n
},\n
"compute_node_id": {\n
"title": "Compute Node ID",\n
"type": "string",\n
"description": "The Id of the compute node, example: COMP-1234"\n
},\n
"id": {\n
"title": "ID",\n
"type": "string",\n
"description": "Unique Id of the object. This ID is defined by KD Portal. It is not editable."\n
},\n
"state": {\n
"title": "Requested State",\n
"type": "string",\n
"enum": ["available", "destroyed"],\n
"description": "State of the requested software",\n
"default": "available"\n
},\n
"reported_state": {\n
"title": "Reported State",\n
"type": "string",\n
"enum": ["available", "destroyed", "building"],\n
"description": "State reported by the node installing the Software Installation"\n
},\n
"error_status": {\n
"title": "Error Status",\n
"description": "Used to inform master of an Error in the local processing of the Software Installation",\n
"type": "string"\n
},\n
"portal_type": {\n
"title": "Portal Type",\n
"const": "Software Installation",\n
"type": "string"\n
}\n
},\n
"required": ["portal_type", "software_release_uri", "compute_node_id"]\n
}\n
</string> </value>
</item>
<item>
<key> <string>version</string> </key>
<value> <string>01</string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
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>
import json
software_installation = context
url = software_installation.getUrlString()
tag = "%s_%s_inProgress" % (software_installation.getAggregateUid(portal_type="Compute Node"),
software_installation.getUrlString())
logError = context.ERP5Site_logApiErrorAndReturn
if "state" in data_dict:
# Change desired state
state = data_dict["state"]
if (state == "available"):
software_installation.requestStart()
elif (state == "destroyed"):
software_installation.requestDestroy(activate_kw={'tag': tag})
else:
return logError(
error_name="SOFTWARE-INSTALLATION-WRONG-STATE",
error_message="State should be available or destroyed, but is %s" % state,
)
if "reported_state" in data_dict:
reported_state = data_dict["reported_state"]
if reported_state == "available":
software_installation.setAccessStatus('software release %s available' % url, "available")
elif reported_state == "building":
software_installation.setBuildingStatus('software release %s' % url, "building")
elif reported_state == "destroyed":
if software_installation.getSlapState() != 'destroy_requested':
return logError(
error_name="SOFTWARE-INSTALLATION-DESTROY-NOT-REQUESTED",
error_message="Reported state is destroyed but requested state is not destroyed",
)
if context.getPortalObject().portal_workflow.isTransitionPossible(software_installation,
'invalidate'):
software_installation.invalidate(
comment="Software Release destroyed report.")
else:
return logError(
error_name="SOFTWARE-INSTALLATION-WRONG-REPORTED-STATE",
error_message="Reported state should be available, destroyed or building, but is %s" % state,
)
if "error_status" in data_dict:
software_installation.setErrorStatus('while installing %s' % url)
return json.dumps({
"$schema": "id-response-schema.json",
"id": software_installation.getRelativeUrl(),
"date": str(DateTime()),
"success": "Done"
}, indent=2)
<?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>data_dict, json_form</string> </value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>SoftwareInstallation_updateFromDict</string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
portal_type = data_dict["portal_type"]
portal = context.getPortalObject()
### Start of Duplicated code for getting documents the SlapOS way"
from Products.ERP5Type.Cache import CachingMethod
def _getNonCachedComputeNodeDocumentUrl(compute_node_reference):
return context.Base_getUnrestrictedDocumentUrl(
portal_type='Compute Node',
# XXX Hardcoded validation state
validation_state="validated",
reference=compute_node_reference)
def _getComputeNodeDocument(compute_node_reference):
"""
Get the validated compute_node with this reference.
"""
result = CachingMethod(_getNonCachedComputeNodeDocumentUrl,
id='_getComputeNodeDocument',
cache_factory='slap_cache_factory')(compute_node_reference)
if result:
return portal.restrictedTraverse(result)
return None
if portal_type == "Software Installation":
if "compute_node_id" in data_dict and "software_release_uri" in data_dict:
compute_node = _getComputeNodeDocument(data_dict["compute_node_id"])
if compute_node:
return compute_node.getSoftwareInstallationFromUrl(data_dict["software_release_uri"])
return None
<?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>data_dict</string> </value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>jIOWebSection_getObjectFromData</string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
Software Installation | slapos_jio_api_put_software_installation
Software Instance | slapos_jio_api_get_software_instance
jIO Web Section | slapos_jio_api_create_compute_node
jIO Web Section | slapos_jio_api_create_software_installation
......
portal_callables/SoftwareInstallation_updateFromJSON
portal_callables/jIOWebSection_createComputeNodeFromJSON
portal_callables/jIOWebSection_createSoftwareInstallationFromJSON
portal_callables/jIOWebSection_requestComputerPartitionFromJSON
\ No newline at end of file
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