Commit 001a77be authored by Cédric Le Ninivin's avatar Cédric Le Ninivin

slapconfiguration: First version with jIO API call

parent 9544ed71
......@@ -34,7 +34,7 @@ from slapos.recipe.librecipe import unwrap
import six
from six.moves.configparser import RawConfigParser
from netaddr import valid_ipv4, valid_ipv6
from slapos.util import mkdir_p
from slapos.util import mkdir_p, dumps
from slapos import format as slapformat
......@@ -70,6 +70,10 @@ class Recipe(object):
Partition identifier.
Example:
${slap-connection:partition-id}
software-instance-reference (optional)
Reference of the instance, this is used when the jIO API is available
Example:
${slap-connection:software-instance-reference}
storage-home
Path of folder configured for data storage
Example:
......@@ -134,34 +138,87 @@ class Recipe(object):
options.get('key'),
options.get('cert'),
)
computer_partition = slap.registerComputerPartition(
options['computer'],
options['partition'],
)
parameter_dict = computer_partition.getInstanceParameterDict()
options['instance-state'] = computer_partition.getState()
# XXX: those are not partition parameters, strictly speaking.
# Make them available as individual section keys.
for his_key in (
'slap_software_type',
'slap_computer_partition_id',
'slap_computer_id',
'slap_software_release_url',
'slave_instance_list',
'timestamp',
):
try:
value = parameter_dict.pop(his_key)
except KeyError:
pass
else:
options[his_key.replace('_', '-')] = value
# Get Instance and root instance title or return UNKNOWN if not set
options['instance-title'] = parameter_dict.pop('instance_title',
'UNKNOWN Instance')
options['root-instance-title'] = parameter_dict.pop('root_instance_title',
'UNKNOWN')
options['instance-guid'] = computer_partition.getInstanceGuid()
if slap.jio_api_connector:
if options.get("software-instance-reference"):
software_instance = slap.jio_api_connector.get({
"portal_type": "Software Instance",
"reference": options.get("software-instance-reference"),
})
else:
software_instance = slap.jio_api_connector.get({
"portal_type": "Software Instance",
"compute_node_id": options["computer"],
"compute_partition_id": options["partition"],
})
options["instance-state"] = software_instance.get("state")
options["slap_software_type"] = software_instance.get("software_type")
options["slap_computer_partition_id"] = software_instance.get("compute_partition_id")
options["slap_computer_id"] = software_instance.get("compute_node_id")
options["slap_software_release_url"] = software_instance.get("software_release_uri")
options["timestamp"] = software_instance.get("processing_timestamp")
options["instance-title"] = software_instance.get("title")
options["root-instance-title"] = software_instance.get("root_instance_title")
options["instance-guid"] = software_instance.get("reference")
ip_list = software_instance.get("ip_list", [])
full_ip_list = software_instance.get("full_ip_list", [])
parameter_dict = json.loads(software_instance.get("parameters"))
# Get Share instance list
result_shared_instance_list = slap.jio_api_connector.allDocs({
"portal_type": "Shared Instance",
"host_instance_reference": software_instance.get("reference"),
"state": "started",
}).get("result_list", [])
shared_instance_list = []
for shared_instance_brain in result_shared_instance_list:
shared_instance = slap.jio_api_connector.get({
"portal_type": "Software Instance",
"reference": shared_instance_brain.get("reference"),
})
shared_instance_parameter = json.loads(shared_instance.get("parameters"))
shared_instance_connection = json.loads(shared_instance.get("connection_parameters"))
shared_instance_list.append({
'slave_title': shared_instance.get("title"),
'slap_software_type': \
shared_instance.get("type"),
'slave_reference': shared_instance.get("reference"),
'timestamp': shared_instance.get("timestamp"),
'xml': dumps(shared_instance_parameter),
'parameters': shared_instance.get("parameters"),
'connection_xml': dumps(shared_instance_connection),
'connection_parameters': shared_instance.get("connection_parameters"),
})
else:
computer_partition = slap.registerComputerPartition(
options['computer'],
options['partition'],
)
parameter_dict = computer_partition.getInstanceParameterDict()
options['instance-state'] = computer_partition.getState()
# XXX: those are not partition parameters, strictly speaking.
# Make them available as individual section keys.
for his_key in (
'slap_software_type',
'slap_computer_partition_id',
'slap_computer_id',
'slap_software_release_url',
'slave_instance_list',
'timestamp',
):
try:
value = parameter_dict.pop(his_key)
except KeyError:
pass
else:
options[his_key.replace('_', '-')] = value
# Get Instance and root instance title or return UNKNOWN if not set
options['instance-title'] = parameter_dict.pop('instance_title',
'UNKNOWN Instance')
options['root-instance-title'] = parameter_dict.pop('root_instance_title',
'UNKNOWN')
options['instance-guid'] = computer_partition.getInstanceGuid()
ip_list = parameter_dict.pop('ip_list')
full_ip_list = parameter_dict.pop('full_ip_list', [])
ipv4_set = set()
v4_add = ipv4_set.add
......@@ -177,7 +234,7 @@ class Recipe(object):
route_v4_add = route_ipv4_set.add
route_network_set = set()
route_net_add = route_network_set.add
for tap, ip in parameter_dict.pop('ip_list'):
for tap, ip in ip_list:
tap_add(tap)
if valid_ipv4(ip):
v4_add(ip)
......@@ -185,19 +242,18 @@ class Recipe(object):
v6_add(ip)
# XXX: emit warning on unknown address type ?
if 'full_ip_list' in parameter_dict:
for item in parameter_dict.pop('full_ip_list'):
if len(item) == 5:
tap, ip, gw, netmask, network = item
if tap.startswith('route_'):
if valid_ipv4(gw):
route_gw_add(gw)
if valid_ipv4(netmask):
route_mask_add(netmask)
if valid_ipv4(ip):
route_v4_add(ip)
if valid_ipv4(network):
route_net_add(network)
for item in full_ip_list:
if len(item) == 5:
tap, ip, gw, netmask, network = item
if tap.startswith('route_'):
if valid_ipv4(gw):
route_gw_add(gw)
if valid_ipv4(netmask):
route_mask_add(netmask)
if valid_ipv4(ip):
route_v4_add(ip)
if valid_ipv4(network):
route_net_add(network)
options['ipv4'] = ipv4_set
options['ipv6'] = ipv6_set
......
# coding: utf-8
import json
import mock
import json
import httmock
import os
import unittest
import tempfile
......@@ -9,6 +10,23 @@ from slapos.recipe import slapconfiguration
from slapos import format as slapformat
class APIRequestHandler(object):
def __init__(self, response_list):
self.response_list = response_list
self.request_payload_list = []
def request_handler(self, url, req):
if url.path == "/getHateoasUrl":
return ""
elif url.path == "/getJIOAPIUrl":
return "https://127.0.0.1/api/"
if not self.response_list and self.response_list[0][0] != url.path:
raise ValueError("Unexcpected call: %s %s" % (url.path, req.body))
return self.response_list.pop(0)[1]
class SlapConfigurationTest(unittest.TestCase):
def setUp(self):
......@@ -52,3 +70,55 @@ class SlapConfigurationTest(unittest.TestCase):
self.assertEqual(options['address-list'], [10, 20],
"All underscores should be replaced with -")
def test_new_api(self):
"""Test proper call with new api"""
options = {
"url": "http://127.0.0.1:80",
"software-instance-reference": "SOFTINST-12",
}
parameter_dict = {"foo": "bar", "hello": "bye"}
instance_data = {
"reference": options["software-instance-reference"],
"state": "started",
"software_type": "Couscous",
"compute_partition_id": "slappartx12",
"compute_node_id": "COMP-321",
"software_release_uri": "foo.cfg",
"processing_timestamp": 1223231231,
"title": "MyInstance",
"root_instance_title": "MyInstanceRoot",
"ip_list": [
[
"slaptap9",
"fe80::1ff:fe23:4567:890a"
],
[
"slaptap9",
"10.0.246.114"
]
],
"parameters": json.dumps(parameter_dict),
"connection_parameters": {"1": 2, "3": "YourURL"},
}
api_handler = APIRequestHandler([
("/api/get", json.dumps(instance_data)),
("/api/allDocs", json.dumps({"result_list": []}))
])
with httmock.HTTMock(api_handler.request_handler):
slapconfiguration.Recipe(self.buildout, "slapconfiguration", options)
self.assertEqual(options["instance-state"], instance_data.get("state"))
self.assertEqual(options["slap_software_type"], instance_data.get("software_type"))
self.assertEqual(options["slap_computer_partition_id"], instance_data.get("compute_partition_id"))
self.assertEqual(options["slap_computer_id"], instance_data.get("compute_node_id"))
self.assertEqual(options["slap_software_release_url"], instance_data.get("software_release_uri"))
self.assertEqual(options["timestamp"], instance_data.get("processing_timestamp"))
self.assertEqual(options["instance-title"], instance_data.get("title"))
self.assertEqual(options["root-instance-title"], instance_data.get("root_instance_title"))
self.assertEqual(options["instance-guid"], instance_data.get("reference"))
self.assertEqual(options["ipv4"], set([instance_data.get("ip_list")[1][1]]))
self.assertEqual(options["ipv6"], set([instance_data.get("ip_list")[0][1]]))
for key, value in parameter_dict.items():
options['configuration.' + key] = value
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