Commit 081a7901 authored by Cédric Le Ninivin's avatar Cédric Le Ninivin Committed by Romain Courteaud

slapformat: First implementation with jIO API

parent 12cafacd
......@@ -364,19 +364,51 @@ class Computer(object):
if conf.key_file and conf.cert_file:
connection_dict['key_file'] = conf.key_file
connection_dict['cert_file'] = conf.cert_file
if conf.slapgrid_jio_uri:
connection_dict["slapgrid_jio_uri"] = conf.slapgrid_jio_uri
slap_instance.initializeConnection(conf.master_url,
**connection_dict)
slap_computer = slap_instance.registerComputer(self.reference)
if slap_instance.jio_api_connector:
partition_list = []
for partition in self.partition_list:
ip_list = []
network_interface = partition.tap.name if partition.tap else partition.reference
for address in partition.address_list:
ip_list.append({
"ip-address": address["addr"],
"network-interface": network_interface,
})
if partition.tap and partition.tap.ipv4_addr:
ip_list.append({
"ip-address": partition.tap.ipv4_addr,
"network-interface": partition.tap.name,
"gateway-ip-address": partition.tap.ipv4_gateway,
"netmask": partition.tap.netmask,
"network-address": partition.tap.ipv4_network
})
partition_list.append({
"partition_id": partition.reference,
"ip_list": ip_list
})
if conf.dry_run:
return
slap_instance.jio_api_connector.put({
"compute_node_id": self.reference,
"compute_partition_list": partition_list
})
else:
slap_computer = slap_instance.registerComputer(self.reference)
if conf.dry_run:
return
try:
slap_computer.updateConfiguration(dumps(_getDict(self)))
except slap.NotFoundError as error:
raise slap.NotFoundError("%s\nERROR: This SlapOS node is not recognised by "
"SlapOS Master and/or computer_id and certificates don't match. "
"Please make sure computer_id of slapos.cfg looks "
"like 'COMP-123' and is correct.\nError is : 404 Not Found." % error)
if conf.dry_run:
return
try:
slap_computer.updateConfiguration(dumps(_getDict(self)))
except slap.NotFoundError as error:
raise slap.NotFoundError("%s\nERROR: This SlapOS node is not recognised by "
"SlapOS Master and/or computer_id and certificates don't match. "
"Please make sure computer_id of slapos.cfg looks "
"like 'COMP-123' and is correct.\nError is : 404 Not Found." % error)
def dump(self, path_to_xml, path_to_json, logger):
"""
......@@ -1666,7 +1698,16 @@ def do_format(conf):
conf.logger.exception('slapos failed to prepare the computer.')
return FormatReturn.FAILURE
computer.update()
# Dumping and sending to the erp5 the current configuration
import pdb; pdb.set_trace()
if not conf.dry_run:
computer.dump(path_to_xml=conf.computer_xml,
path_to_json=conf.computer_json,
logger=conf.logger)
conf.logger.info('Posting information to %r' % conf.master_url)
computer.send(conf)
conf.logger.info('slapos successfully prepared the computer.')
class FormatConfig(object):
"""This class represents the options for slapos node format
......
......@@ -29,6 +29,7 @@
from __future__ import print_function
import glob
import json
import logging
import os
import shutil
......@@ -51,6 +52,8 @@ import os
import pwd
import time
import mock
import httmock
from six.moves.urllib import parse
from .test_slapgrid import DummyManager
......@@ -684,6 +687,81 @@ class TestFormatDump(SlapformatMixin):
shutil.rmtree(self._tempdir, True)
super(TestFormatDump, self).tearDown()
class TestConfForAPI():
def __init__(self):
self.master_url = "https://127.0.0.1"
self.slapgrid_jio_uri = "https://127.0.0.1/api/"
self.key_file = None
self.cert_file = None
self.dry_run = False
class TestFormatSendToMaster(SlapformatMixin):
def setUp(self):
super(TestFormatSendToMaster, self).setUp()
self.restoreOs()
self._tempdir = tempfile.mkdtemp()
self.sequence = []
self.body_sequence = []
def jio_api_request_handler(self, url, req):
self.sequence.append(url.path)
if req.method == 'GET':
qs = parse.parse_qs(url.query)
else:
qs = parse.parse_qs(req.body)
if url.path.startswith('/api/'):
content = json.loads(req.body)
self.body_sequence.append(content)
return json.dumps({})
def test(self):
computer = slapos.format.Computer('computer',
instance_root=os.path.join(self._tempdir, 'instance_root'),
software_root=os.path.join(self._tempdir, 'software_root'),
tap_ipv6=True,
interface=slapos.format.Interface(
logger=self.logger, name='myinterface', ipv4_local_network='127.0.0.1/16'),
partition_list=[
slapos.format.Partition(
'partition', 'part_path', slapos.format.User('testuser'), [], tap=slapos.format.Tap('tap')),
])
global USER_LIST
USER_LIST = ['testuser']
global INTERFACE_DICT
INTERFACE_DICT['myinterface'] = {
socket.AF_INET: [{'addr': '192.168.242.77', 'broadcast': '127.0.0.1',
'netmask': '255.255.255.0'}],
socket.AF_INET6: [{'addr': '2a01:e35:2e27::e59c', 'netmask': 'ffff:ffff:ffff:ffff::'}]
}
computer.format(alter_user=False, alter_network=False, create_tap=False)
test_configuration = TestConfForAPI()
with httmock.HTTMock(self.jio_api_request_handler):
computer.send(test_configuration)
self.assertEqual(self.sequence, ['/getHateoasUrl', '/api/put/'])
self.assertEqual(
self.body_sequence,
[
{
'compute_node_id': 'computer',
'compute_partition_list': [
{
'ip_list': [
{'ip-address': computer.partition_list[0].address_list[0]["addr"], 'network-interface': 'tap'},
{'ip-address': computer.partition_list[0].address_list[1]["addr"], 'network-interface': 'tap'}
],
'partition_id': 'partition'
}
]
}
]
)
def tearDown(self):
shutil.rmtree(self._tempdir, True)
super(TestFormatSendToMaster, self).tearDown()
class SlapGridPartitionMock:
......
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