Factor request methods.

parent 428e9dad
...@@ -77,6 +77,36 @@ class SlapDocument: ...@@ -77,6 +77,36 @@ class SlapDocument:
# cause failures when accessing _connection_helper property. # cause failures when accessing _connection_helper property.
self._connection_helper = connection_helper self._connection_helper = connection_helper
class SlapRequester(SlapDocument):
"""
Abstract class that allow to factor method for subclasses that use "request()"
"""
def _requestComputerPartition(self, request_dict):
try:
self._connection_helper.POST('/requestComputerPartition', request_dict)
except ResourceNotReady:
return ComputerPartition(
request_dict=request_dict,
connection_helper=self._connection_helper,
)
else:
xml = self._connection_helper.response.read()
software_instance = xml_marshaller.loads(xml)
computer_partition = ComputerPartition(
software_instance.slap_computer_id.encode('UTF-8'),
software_instance.slap_computer_partition_id.encode('UTF-8'),
connection_helper=self._connection_helper,
)
# Hack to give all object attributes to the ComputerPartition instance
computer_partition.__dict__ = software_instance.__dict__.copy()
# XXX not generic enough.
if xml_marshaller.loads(request_dict['shared_xml']):
computer_partition._synced = True
computer_partition._connection_dict = software_instance._connection_dict
computer_partition._parameter_dict = software_instance._parameter_dict
return computer_partition
class SoftwareRelease(SlapDocument): class SoftwareRelease(SlapDocument):
""" """
Contains Software Release information Contains Software Release information
...@@ -182,7 +212,7 @@ class Supply(SlapDocument): ...@@ -182,7 +212,7 @@ class Supply(SlapDocument):
raise NotFoundError("Computer %s has not been found by SlapOS Master." raise NotFoundError("Computer %s has not been found by SlapOS Master."
% computer_guid) % computer_guid)
class OpenOrder(SlapDocument): class OpenOrder(SlapRequester):
zope.interface.implements(interface.IOpenOrder) zope.interface.implements(interface.IOpenOrder)
...@@ -208,28 +238,7 @@ class OpenOrder(SlapDocument): ...@@ -208,28 +238,7 @@ class OpenOrder(SlapDocument):
else: else:
# Let's enforce a default software type # Let's enforce a default software type
request_dict['software_type'] = DEFAULT_SOFTWARE_TYPE request_dict['software_type'] = DEFAULT_SOFTWARE_TYPE
try: return self._requestComputerPartition(request_dict)
self._connection_helper.POST('/requestComputerPartition', request_dict)
except ResourceNotReady:
return ComputerPartition(
request_dict=request_dict,
connection_helper=self._connection_helper,
)
else:
xml = self._connection_helper.response.read()
software_instance = xml_marshaller.loads(xml)
computer_partition = ComputerPartition(
software_instance.slap_computer_id.encode('UTF-8'),
software_instance.slap_computer_partition_id.encode('UTF-8'),
connection_helper=self._connection_helper,
)
# Hack to give all object attributes to the ComputerPartition instance
computer_partition.__dict__ = software_instance.__dict__.copy()
if shared:
computer_partition._synced = True
computer_partition._connection_dict = software_instance._connection_dict
computer_partition._parameter_dict = software_instance._parameter_dict
return computer_partition
def requestComputer(self, computer_reference): def requestComputer(self, computer_reference):
""" """
...@@ -345,7 +354,6 @@ class ComputerPartition(SlapDocument): ...@@ -345,7 +354,6 @@ class ComputerPartition(SlapDocument):
def __getinitargs__(self): def __getinitargs__(self):
return (self._computer_id, self._partition_id, ) return (self._computer_id, self._partition_id, )
# XXX-Cedric: Factor me with OpenOrder.request()
def request(self, software_release, software_type, partition_reference, def request(self, software_release, software_type, partition_reference,
shared=False, partition_parameter_kw=None, filter_kw=None, shared=False, partition_parameter_kw=None, filter_kw=None,
state=None): state=None):
...@@ -365,7 +373,8 @@ class ComputerPartition(SlapDocument): ...@@ -365,7 +373,8 @@ class ComputerPartition(SlapDocument):
if software_type is None: if software_type is None:
software_type = DEFAULT_SOFTWARE_TYPE software_type = DEFAULT_SOFTWARE_TYPE
request_dict = { 'computer_id': self._computer_id, request_dict = {
'computer_id': self._computer_id,
'computer_partition_id': self._partition_id, 'computer_partition_id': self._partition_id,
'software_release': software_release, 'software_release': software_release,
'software_type': software_type, 'software_type': software_type,
...@@ -376,30 +385,7 @@ class ComputerPartition(SlapDocument): ...@@ -376,30 +385,7 @@ class ComputerPartition(SlapDocument):
'filter_xml': xml_marshaller.dumps(filter_kw), 'filter_xml': xml_marshaller.dumps(filter_kw),
'state': xml_marshaller.dumps(state), 'state': xml_marshaller.dumps(state),
} }
try: return self._requestComputerPartition(request_dict)
self._connection_helper.POST('/requestComputerPartition', request_dict)
except ResourceNotReady:
return ComputerPartition(
request_dict=request_dict,
connection_helper=self._connection_helper,
)
else:
xml = self._connection_helper.response.read()
software_instance = xml_marshaller.loads(xml)
computer_partition = ComputerPartition(
software_instance.slap_computer_id.encode('UTF-8'),
software_instance.slap_computer_partition_id.encode('UTF-8'),
connection_helper=self._connection_helper,
)
# Hack to give all object attributes to the ComputerPartition instance
computer_partition.__dict__ = software_instance.__dict__.copy()
if shared:
computer_partition._synced = True
computer_partition._connection_dict = getattr(software_instance,
'_connection_dict', None)
computer_partition._parameter_dict = getattr(software_instance,
'_parameter_dict', None)
return computer_partition
def building(self): def building(self):
self._connection_helper.POST('/buildingComputerPartition', { self._connection_helper.POST('/buildingComputerPartition', {
......
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