Commit a5d41e2b authored by Romain Courteaud's avatar Romain Courteaud

Allow to fetch status for computer and partition.

parent 5c592a04
......@@ -336,6 +336,34 @@ class SlapTool(BaseTool):
self.REQUEST.response.setBody(result)
return self.REQUEST.response
security.declareProtected(Permissions.AccessContentsInformation,
'getComputerPartitionStatus')
def getComputerPartitionStatus(self, computer_id, computer_partition_id):
"""
Get the connection status of the partition
"""
try:
instance = self._getSoftwareInstanceForComputerPartition(
computer_id,
computer_partition_id)
except NotFound:
return self._getAccessStatus(None)
else:
return self._getAccessStatus(instance.getReference())
security.declareProtected(Permissions.AccessContentsInformation,
'getComputerStatus')
def getComputerStatus(self, computer_id):
"""
Get the connection status of the partition
"""
computer = self.getPortalObject().portal_catalog.unrestrictedSearchResults(
portal_type='Computer', reference=computer_id,
validation_state="validated")[0].getObject()
# Be sure to prevent accessing information to disallowed users
computer = _assertACI(computer)
return self._getAccessStatus(computer_id)
####################################################
# Public POST methods
####################################################
......@@ -543,6 +571,9 @@ class SlapTool(BaseTool):
"""
Fire up bung on Computer
"""
user = self.getPortalObject().portal_membership.getAuthenticatedMember()\
.getUserName()
self._logAccess(user, computer_id, '#error bang')
return self._getComputerDocument(computer_id).reportComputerBang(
comment=message)
......@@ -670,6 +701,11 @@ class SlapTool(BaseTool):
slap_partition._parameter_dict.update(parameter_dict)
result = xml_marshaller.xml_marshaller.dumps(slap_partition)
user = self.getPortalObject().portal_membership.\
getAuthenticatedMember().getUserName()
self._logAccess(user, user, '#access %s %s' % (computer_reference,
computer_partition_reference))
# Keep in cache server for 7 days
self.REQUEST.response.setStatus(200)
self.REQUEST.response.setHeader('Cache-Control',
......@@ -950,10 +986,46 @@ class SlapTool(BaseTool):
Fire up bang on Software Instance
Add an error for the software Instance Workflow
"""
return self._getSoftwareInstanceForComputerPartition(
software_instance = self._getSoftwareInstanceForComputerPartition(
computer_id,
computer_partition_id).bang(bang_tree=True,
comment=message)
computer_partition_id)
user = self.getPortalObject().portal_membership.\
getAuthenticatedMember().getUserName()
self._logAccess(user, software_instance.getReference(),
'#error bang called')
return software_instance.bang(bang_tree=True, comment=message)
def _getAccessStatus(self, context_reference):
memcached_dict = self._getMemcachedDict()
try:
if context_reference is None:
raise KeyError
else:
d = memcached_dict[context_reference]
except KeyError:
if context_reference is None:
d = {
"user": "SlapOS Master",
'created_at': '%s' % rfc1123_date(DateTime()),
"text": "#error no data found"
}
else:
d = {
"user": "SlapOS Master",
'created_at': '%s' % rfc1123_date(DateTime()),
"text": "#error no data found for %s" % context_reference
}
else:
d = json.loads(d)
# Keep in cache server for 7 days
self.REQUEST.response.setStatus(200)
self.REQUEST.response.setHeader('Cache-Control',
'public, max-age=60, stale-if-error=604800')
self.REQUEST.response.setHeader('Vary',
'REMOTE_USER')
self.REQUEST.response.setHeader('Last-Modified', rfc1123_date(DateTime()))
self.REQUEST.response.setBody(xml_marshaller.xml_marshaller.dumps(d))
return self.REQUEST.response
@convertToREST
def _startedComputerPartition(self, computer_id, computer_partition_id):
......
from Products.ZSQLCatalog.SQLCatalog import Query, ComplexQuery
from Products.ERP5Type.tests.Sequence import SequenceList
from Products.ERP5Type.tests.backportUnittest import skip
import transaction
import unittest
from testVifibSlapWebService import TestVifibSlapWebServiceMixin
from slapos import slap
class TestVifibSlapComputerGetStatus(TestVifibSlapWebServiceMixin):
########################################
# Computer.getStatus
########################################
def stepCheckNoDataComputerGetStatusCall(self, sequence, **kw):
"""
"""
computer_guid = sequence["computer_reference"]
self.slap = slap.slap()
self.slap.initializeConnection(self.server_url, timeout=None)
computer = self.slap.registerComputer(
computer_guid)
status = computer.getStatus()
self.assertEquals('SlapOS Master', status['user'])
self.assertEquals('#error no data found for %s' % \
sequence["computer_reference"], status['text'])
def test_Computer_getStatus_noComputer(self):
"""
"""
sequence_list = SequenceList()
sequence_string = self.prepare_computer + '\
SlapLoginCurrentComputer \
CheckNoDataComputerGetStatusCall \
SlapLogout \
LoginERP5TypeTestCase \
CheckSiteConsistency \
Logout \
'
sequence_list.addSequenceString(sequence_string)
sequence_list.play(self)
def stepCheckOKComputerGetStatusCall(self, sequence, **kw):
"""
"""
computer_guid = sequence["computer_reference"]
self.slap = slap.slap()
self.slap.initializeConnection(self.server_url, timeout=None)
computer = self.slap.registerComputer(
computer_guid)
status = computer.getStatus()
self.assertEquals(sequence['computer_reference'], status['user'])
self.assertEquals('#access %s' % sequence["computer_reference"],
status['text'])
def test_Computer_OK_getStatus(self):
"""
"""
sequence_list = SequenceList()
sequence_string = self.prepare_computer + '\
SlapLoginCurrentComputer \
CheckSuccessComputerGetComputerPartitionCall \
CheckOKComputerGetStatusCall \
SlapLogout \
LoginERP5TypeTestCase \
CheckSiteConsistency \
Logout \
'
sequence_list.addSequenceString(sequence_string)
sequence_list.play(self)
def stepCheckErrorComputerGetStatusCall(self, sequence, **kw):
"""
"""
computer_guid = sequence["computer_reference"]
self.slap = slap.slap()
self.slap.initializeConnection(self.server_url, timeout=None)
computer = self.slap.registerComputer(
computer_guid)
status = computer.getStatus()
self.assertEquals(sequence['computer_reference'], status['user'])
self.assertEquals('#error bang', status['text'])
def stepSoftwareReleaseError(self, sequence,
**kw):
"""
"""
url = sequence["software_release_uri"]
self.slap = slap.slap()
self.slap.initializeConnection(self.server_url, timeout=None)
software_release = self.slap.registerSoftwareRelease(url)
software_release.error("ErrorLog")
def stepComputerBang(self, sequence, **kw):
self.slap = slap.slap()
self.slap.initializeConnection(self.server_url, timeout=None)
slap_computer = self.slap.registerComputer(
sequence['computer_reference'])
slap_computer.bang('foo')
def test_Computer_Error_getStatus(self):
"""
"""
sequence_list = SequenceList()
sequence_string = self.prepare_formated_computer + '\
SlapLoginCurrentComputer \
ComputerBang \
CheckErrorComputerGetStatusCall \
SlapLogout \
LoginERP5TypeTestCase \
CheckSiteConsistency \
Logout \
'
sequence_list.addSequenceString(sequence_string)
sequence_list.play(self)
def test_suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestVifibSlapComputerGetStatus))
return suite
from Products.ERP5Type.tests.Sequence import SequenceList
import unittest
from testVifibSlapWebService import TestVifibSlapWebServiceMixin
from slapos import slap
class TestVifibSlapComputerPartitionGetStatus(TestVifibSlapWebServiceMixin):
########################################
# ComputerPartition.getStatus
########################################
def stepCheckNoDataComputerPartitionGetStatusCall(self, sequence, **kw):
"""
"""
computer_guid = sequence["computer_reference"]
partition_id = sequence.get("computer_partition_reference",
sequence['computer_partition_reference_list'][0])
self.slap = slap.slap()
self.slap.initializeConnection(self.server_url, timeout=None)
computer_partition = self.slap.registerComputerPartition(
computer_guid, partition_id)
status = computer_partition.getStatus()
self.assertEquals('SlapOS Master', status['user'])
self.assertEquals('#error no data found', status['text'])
def test_ComputerPartition_getStatus_emptyPartition(self):
"""
"""
sequence_list = SequenceList()
sequence_string = self.prepare_formated_computer + '\
SlapLoginCurrentComputer \
CheckNoDataComputerPartitionGetStatusCall \
SlapLogout \
LoginERP5TypeTestCase \
CheckSiteConsistency \
Logout \
'
sequence_list.addSequenceString(sequence_string)
sequence_list.play(self)
def stepCheckOKComputerPartitionGetStatusCall(self, sequence, **kw):
"""
"""
computer_guid = sequence["computer_reference"]
partition_id = sequence.get("computer_partition_reference",
sequence['computer_partition_reference_list'][0])
self.slap = slap.slap()
self.slap.initializeConnection(self.server_url, timeout=None)
computer_partition = self.slap.registerComputerPartition(
computer_guid, partition_id)
status = computer_partition.getStatus()
self.assertEquals(sequence['software_instance_reference'], status['user'])
self.assertEquals('#access %s %s' % (
sequence['computer_reference'],
sequence['software_instance_reference']),
status['text'])
def test_ComputerPartition_OK_getStatus(self):
"""
"""
sequence_list = SequenceList()
sequence_string = self.prepare_building_computer_partition_sequence_string + '\
SlapLoginCurrentSoftwareInstance \
CheckSuccessSlapRegisterComputerPartitionCall \
SlapLogout \
SlapLoginCurrentComputer \
CheckOKComputerPartitionGetStatusCall \
SlapLogout \
LoginERP5TypeTestCase \
CheckSiteConsistency \
Logout \
'
sequence_list.addSequenceString(sequence_string)
sequence_list.play(self)
def stepCheckErrorComputerPartitionGetStatusCall(self, sequence, **kw):
"""
"""
computer_guid = sequence["computer_reference"]
partition_id = sequence.get("computer_partition_reference",
sequence['computer_partition_reference_list'][0])
self.slap = slap.slap()
self.slap.initializeConnection(self.server_url, timeout=None)
computer_partition = self.slap.registerComputerPartition(
computer_guid, partition_id)
status = computer_partition.getStatus()
self.assertEquals(sequence['computer_reference'], status['user'])
self.assertEquals('#error while instanciating', status['text'])
def test_ComputerPartition_Error_getStatus(self):
"""
"""
sequence_list = SequenceList()
sequence_string = self.prepare_building_computer_partition_sequence_string + '\
SlapLoginCurrentComputer \
CheckSuccessComputerPartitionErrorCall \
CheckErrorComputerPartitionGetStatusCall \
SlapLogout \
LoginERP5TypeTestCase \
CheckSiteConsistency \
Logout \
'
sequence_list.addSequenceString(sequence_string)
sequence_list.play(self)
def test_suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestVifibSlapComputerPartitionGetStatus))
return suite
......@@ -283,6 +283,16 @@ class IComputerPartition(IBuildoutController, IRequester):
slave_reference -- current reference of the slave instance to modify
"""
def getStatus():
"""
Returns a dictionnary containing the latest status of the
computer partition.
The dictionnary keys are:
user -- user who reported the latest status
created_at -- date of the status
text -- message log of the status
"""
class IComputer(Interface):
"""
Computer interface specification
......@@ -334,6 +344,16 @@ class IComputer(Interface):
configuration_xml -- computer XML description generated by slapformat
"""
def getStatus():
"""
Returns a dictionnary containing the latest status of the
computer.
The dictionnary keys are:
user -- user who reported the latest status
created_at -- date of the status
text -- message log of the status
"""
class IOpenOrder(IRequester):
"""
Open Order interface specification
......
......@@ -292,6 +292,11 @@ class Computer(SlapDocument):
'certificate': getattr(self, '_certificate', None)
}
def getStatus(self):
self._connection_helper.GET(
'/getComputerStatus?computer_id=%s' % self._computer_id)
return xml_marshaller.loads(self._connection_helper.response.read())
def _syncComputerPartitionInformation(func):
"""
Synchronize computer partition object with server information
......@@ -528,6 +533,12 @@ class ComputerPartition(SlapDocument):
'computer_partition_id=%s' % (self._computer_id, self._partition_id))
return xml_marshaller.loads(self._connection_helper.response.read())
def getStatus(self):
self._connection_helper.GET(
'/getComputerPartitionStatus?computer_id=%s&'
'computer_partition_id=%s' % (self._computer_id, self._partition_id))
return xml_marshaller.loads(self._connection_helper.response.read())
class ConnectionHelper:
error_message_timeout = "\nThe connection timed out. Please try again later."
error_message_connect_fail = "Couldn't connect to the server. Please " \
......
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