Commit ff28ea86 authored by Romain Courteaud's avatar Romain Courteaud

slapos_slap_tool: add compatibility with the user request

Console client does not send a project reference.
Try to guess it for simple cases
parent f21bc34f
......@@ -3112,6 +3112,38 @@ class TestSlapOSSlapToolPersonAccess(TestSlapOSSlapToolMixin):
if os.path.exists(self.instance_request_simulator):
os.unlink(self.instance_request_simulator)
def test_requestWithoutProjectReference(self):
self.instance_request_simulator = tempfile.mkstemp()[1]
try:
self.login(self.person_user_id)
self.person.requestSoftwareInstance = Simulator(
self.instance_request_simulator, 'requestSoftwareInstance')
response = self.portal_slap.requestComputerPartition(
software_release='req_release',
software_type='req_type',
partition_reference='req_reference',
partition_parameter_xml='<marshal><dictionary id="i2"/></marshal>',
filter_xml='<marshal><dictionary id="i2"/></marshal>',
state='<marshal><string>started</string></marshal>',
shared_xml='<marshal><bool>0</bool></marshal>'
)
self.assertEqual(408, response.status)
self.assertEqual('private',
response.headers.get('cache-control'))
self.assertInstanceRequestSimulator((), {
'instance_xml': "<?xml version='1.0' encoding='utf-8'?>\n<instance/>\n",
'software_title': 'req_reference',
'software_release': 'req_release',
'state': 'started',
'sla_xml': "<?xml version='1.0' encoding='utf-8'?>\n<instance/>\n",
'software_type': 'req_type',
'shared': False,
'project_reference': self.project.getReference()
})
finally:
if os.path.exists(self.instance_request_simulator):
os.unlink(self.instance_request_simulator)
def test_request_allocated_instance(self):
self.tic()
self.person.edit(
......
......@@ -991,6 +991,46 @@ class SlapTool(BaseTool):
else:
# requested as root, so done by human
requester = portal.portal_membership.getAuthenticatedMember().getUserValue()
if project_reference is None:
# Compatibility with the slapos console client
# which does not send any project_reference parameter
# and always connect to a uniq url
# Try to guess the project reference automatically
project_list = portal.portal_catalog(portal_type='Project', limit=2)
if len(project_list) == 1:
# If the user only has access to one project
# we can easily suppose the request must be allocated here
kw['project_reference'] = project_list[0].getReference()
else:
release_variation_list = portal.portal_catalog(
portal_type='Software Product Release Variation',
url_string=software_release,
limit=2
)
if len(release_variation_list) == 1:
# If the user only has access to matching release variation
# we can easily suppose the request must be allocated on the same project
kw['project_reference'] = release_variation_list[0].getParentValue().getFollowUpReference()
# Finally, try to use the SLA parameter to guess where it could be
elif 'project_guid' in filter_kw:
kw['project_reference'] = filter_kw['project_guid']
elif 'computer_guid' in filter_kw:
computer_list = portal.portal_catalog(
portal_type=['Compute Node', 'Remote Node', 'Instance Node'],
limit=2
)
if len(computer_list == 1):
kw['project_reference'] = computer_list[0].getFollowUpReference()
elif 'network_guid' in filter_kw:
network_list = portal.portal_catalog(
portal_type='Computer Network',
limit=2
)
if len(network_list == 1):
kw['project_reference'] = network_list[0].getFollowUpReference()
key = '_'.join([requester.getRelativeUrl(), partition_reference])
last_data = requester.getLastData(key)
......
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