Commit b77a71d7 authored by Romain Courteaud's avatar Romain Courteaud

slapos_jio_api_style: try to move the software instance json type class to a mixin

parent e15a23c5
##############################################################################
#
# Copyright (c) 2024 Nexedi SA and Contributors. All Rights Reserved.
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################
from App.Common import rfc1123_date
from AccessControl import ClassSecurityInfo
from Products.ERP5Type import Permissions
from Products.ERP5Type.UnrestrictedMethod import UnrestrictedMethod
from erp5.component.module.SlapOSCloud import _assertACI
import json
class SoftwareInstanceJsonTypeMixin:
# Declarative security
security = ClassSecurityInfo()
def getSlapTimestamp(self):
return self._getSlapTimestamp()
def useRevision(self):
return getattr(self, "use_jio_api_revision", False)
@UnrestrictedMethod
def _getSlapTimestamp(self):
compute_partition = self.getAggregateValue(portal_type="Compute Partition")
if compute_partition is None:
return int(self.getModificationDate())
timestamp = int(compute_partition.getModificationDate())
newtimestamp = int(self.getBangTimestamp(int(self.getModificationDate())))
if (newtimestamp > timestamp):
timestamp = newtimestamp
# Check if any of the Shared instances hosted in the software instance have been reprocessed
# XXX In the current what shared instances are processed, they cannot be reprocessed if the
# host instance is not processed
if (self.getPortalType() == "Software Instance"):
shared_instance_sql_list = []
if self.useRevision():
shared_instance_sql_list = self.getPortalObject().portal_catalog.unrestrictedSearchResults(
default_aggregate_uid=compute_partition.getUid(),
portal_type='Slave Instance',
validation_state="validated",
sort_on=(("jio_api_revision.revision", "DESC"),),
select_list=('jio_api_revision.revision',),
limit=1,
**{"slapos_item.slap_state": "start_requested"}
)
else:
shared_instance_sql_list = self.getPortalObject().portal_catalog.unrestrictedSearchResults(
default_aggregate_uid=compute_partition.getUid(),
portal_type='Slave Instance',
validation_state="validated",
**{"slapos_item.slap_state": "start_requested"}
)
for shared_instance in shared_instance_sql_list:
shared_instance = _assertACI(shared_instance.getObject())
# XXX Use catalog to filter more efficiently
if shared_instance.getSlapState() == "start_requested":
newtimestamp = int(shared_instance.getBangTimestamp(int(shared_instance.getModificationDate())))
if (newtimestamp > timestamp):
timestamp = newtimestamp
return timestamp
security.declareProtected(Permissions.AccessContentsInformation,
'getJSONSchemaUrl')
def getJSONSchemaUrl(self):
"""
This is an attempt to provide stability to the Schema URL and by extension to asJSONText
"""
portal = self.getPortalObject()
portal_type_path = portal.portal_types.restrictedTraverse("Software Instance")
base_url = portal.portal_preferences.getPreferredSlaposWebSiteUrl().strip("/")
return "/".join([base_url, portal_type_path.getRelativeUrl(), "getTextContent"])
security.declareProtected(Permissions.AccessContentsInformation,
'asJSONText')
def asJSONText(self):
try:
parameter_dict = self._asParameterDict()
except ValueError:
parameter_dict = {}
requested_state = self.getSlapState()
if requested_state == "stop_requested":
state = 'stopped'
elif requested_state == "start_requested":
state = 'started'
elif requested_state == "destroy_requested":
state = 'destroyed'
elif requested_state == "draft":
state = 'draft'
else:
raise ValueError("Unknown slap state : %s" % requested_state)
# software instance has to define an xml parameter
result = {
"$schema": self.getJSONSchemaUrl(),
"title": self.getTitle().decode("UTF-8"),
"reference": self.getReference().decode("UTF-8"),
"software_release_uri": self.getUrlString(),
"software_type": self.getSourceReference().decode("UTF-8"),
"state": state,
"connection_parameters": self.getConnectionXmlAsDict(),
"parameters": self.getInstanceXmlAsDict(),
"shared": False,
"root_instance_title": parameter_dict.get("root_instance_title"),
"ip_list": parameter_dict.get("ip_list"),
"full_ip_list": parameter_dict.get("full_ip_list"),
"sla_parameters": self.getSlaXmlAsDict(),
"compute_node_id": parameter_dict.get("slap_computer_id"),
"compute_partition_id": parameter_dict.get("slap_computer_partition_id"),
"processing_timestamp": self.getSlapTimestamp(),
"access_status_message": self.getTextAccessStatus(),
"portal_type": self.getPortalType(),
}
if self.useRevision():
web_section = self.getWebSectionValue()
web_section = web_section.getRelativeUrl() if web_section else self.REQUEST.get("web_section_relative_url", None)
if web_section:
revision = self.getJIOAPIRevision(web_section)
if revision:
result["api_revision"] = revision
self.REQUEST.response.setHeader('Cache-Control',
'private, max-age=0, must-revalidate')
self.REQUEST.response.setHeader('Vary',
'REMOTE_USER')
self.REQUEST.response.setHeader('Last-Modified',
rfc1123_date(self.getModificationDate()))
return json.dumps(result, indent=2)
security.declareProtected(Permissions.ModifyPortalContent, 'fromJSONText')
def fromJSONText(self, json_content):
return self.setJsonContent(json_content)
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="Mixin Component" module="erp5.portal_type"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>default_reference</string> </key>
<value> <string>SoftwareInstanceJsonTypeMixin</string> </value>
</item>
<item>
<key> <string>default_source_reference</string> </key>
<value>
<none/>
</value>
</item>
<item>
<key> <string>description</string> </key>
<value>
<none/>
</value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>mixin.erp5.SoftwareInstanceJsonTypeMixin</string> </value>
</item>
<item>
<key> <string>portal_type</string> </key>
<value> <string>Mixin Component</string> </value>
</item>
<item>
<key> <string>sid</string> </key>
<value>
<none/>
</value>
</item>
<item>
<key> <string>text_content_error_message</string> </key>
<value>
<tuple/>
</value>
</item>
<item>
<key> <string>text_content_warning_message</string> </key>
<value>
<tuple/>
</value>
</item>
<item>
<key> <string>version</string> </key>
<value> <string>erp5</string> </value>
</item>
<item>
<key> <string>workflow_history</string> </key>
<value>
<persistent> <string encoding="base64">AAAAAAAAAAI=</string> </persistent>
</value>
</item>
</dictionary>
</pickle>
</record>
<record id="2" aka="AAAAAAAAAAI=">
<pickle>
<global name="PersistentMapping" module="Persistence.mapping"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>data</string> </key>
<value>
<dictionary>
<item>
<key> <string>component_validation_workflow</string> </key>
<value>
<persistent> <string encoding="base64">AAAAAAAAAAM=</string> </persistent>
</value>
</item>
</dictionary>
</value>
</item>
</dictionary>
</pickle>
</record>
<record id="3" aka="AAAAAAAAAAM=">
<pickle>
<global name="WorkflowHistoryList" module="Products.ERP5Type.Workflow"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>_log</string> </key>
<value>
<list>
<dictionary>
<item>
<key> <string>action</string> </key>
<value> <string>validate</string> </value>
</item>
<item>
<key> <string>validation_state</string> </key>
<value> <string>validated</string> </value>
</item>
</dictionary>
</list>
</value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
</portal_type> </portal_type>
<portal_type id="Slave Instance"> <portal_type id="Slave Instance">
<item>JIOAPIRevisionMixin</item> <item>JIOAPIRevisionMixin</item>
<item>SoftwareInstanceJsonTypeMixin</item>
</portal_type> </portal_type>
<portal_type id="Software Installation"> <portal_type id="Software Installation">
<item>JIOAPIRevisionMixin</item> <item>JIOAPIRevisionMixin</item>
...@@ -11,5 +12,6 @@ ...@@ -11,5 +12,6 @@
</portal_type> </portal_type>
<portal_type id="Software Instance"> <portal_type id="Software Instance">
<item>JIOAPIRevisionMixin</item> <item>JIOAPIRevisionMixin</item>
<item>SoftwareInstanceJsonTypeMixin</item>
</portal_type> </portal_type>
</type_mixin> </type_mixin>
\ No newline at end of file
mixin.erp5.SoftwareInstallationJsonTypeMixin mixin.erp5.SoftwareInstallationJsonTypeMixin
mixin.erp5.SoftwareInstanceJsonTypeMixin
\ No newline at end of file
...@@ -3,3 +3,5 @@ Slave Instance | JIOAPIRevisionMixin ...@@ -3,3 +3,5 @@ Slave Instance | JIOAPIRevisionMixin
Software Installation | JIOAPIRevisionMixin Software Installation | JIOAPIRevisionMixin
Software Installation | SoftwareInstallationJsonTypeMixin Software Installation | SoftwareInstallationJsonTypeMixin
Software Instance | JIOAPIRevisionMixin Software Instance | JIOAPIRevisionMixin
Software Instance | SoftwareInstanceJsonTypeMixin
Slave Instance | SoftwareInstanceJsonTypeMixin
\ 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