def test_tosca_add_hybrid2(self): tosca_data = read_file_as_string('../files/tosca_add_hybrid.yml') tosca = Tosca(tosca_data) inf_info = MagicMock() vm1 = MagicMock() system1 = system("lrms_server", [ Feature("disk.0.image.url", "=", "ost://cloud1.com/image1"), Feature("net_interface.0.connection", "=", "private_net") ]) vm1.info.systems = [system1] vm2 = MagicMock() system2 = system("lrms_wn", [ Feature("disk.0.image.url", "=", "ost://cloud3.com/image1"), Feature("net_interface.0.connection", "=", "private.cloud3.com") ]) vm2.info.systems = [system2] inf_info.get_vm_list_by_system_name.return_value = { "lrms_server": [vm1], "lrms_wn": [vm2] } net = MagicMock() net.isPublic.return_value = False inf_info.radl.get_network_by_id.return_value = net _, radl = tosca.to_radl(inf_info) print(radl) radl = parse_radl(str(radl)) lrms_wn = radl.get_system_by_name("lrms_wn") self.assertEqual("private.cloud2.com", lrms_wn.getValue("net_interface.0.connection"))
def test_merge_yaml(self): """Test TOSCA merge two yamls""" a = { "wn_port": { "requirements": [{ "binding": "lrms_wn" }, { "link": "network1" }] } } b = { "wn_port": { "requirements": [{ "binding": "lrms_wn" }, { "link": "network2" }] } } c = Tosca._merge_yaml(a, b) self.assertEqual(c, b) a = {"requirements": [{"binding": "lrms_wn"}, {"link": "network1"}]} b = { "requirements": [{ "binding": "lrms_wn" }, { "link": "network2" }, { "other": "value" }] } c = Tosca._merge_yaml(a, b) self.assertEqual(c, b)
def test_tosca_to_radl(self): """Test TOSCA RADL translation""" tosca_data = read_file_as_string('../files/tosca_long.yml') tosca = Tosca(tosca_data) _, radl = tosca.to_radl() radl = parse_radl(str(radl)) net = radl.get_network_by_id('public_net') net1 = radl.get_network_by_id('public_net_1') self.assertIn(net.getValue('provider_id'), ['vpc-XX.subnet-XX', None]) if net.getValue('provider_id') is None: self.assertIn('1:4/tcp', net.getValue("outports")) self.assertIn('80/tcp-80/tcp', net.getValue("outports")) self.assertIn('8080/tcp-8080/tcp', net.getValue("outports")) self.assertEqual(net1.getValue("outports"), '8080/tcp-8080/tcp') else: self.assertEqual(net.getValue('provider_id'), 'vpc-XX.subnet-XX') self.assertEqual(net.getValue("outports"), '8080/tcp-8080/tcp') self.assertIn('1:4/tcp', net1.getValue("outports")) self.assertIn('80/tcp-80/tcp', net1.getValue("outports")) self.assertIn('8080/tcp-8080/tcp', net1.getValue("outports")) lrms_wn = radl.get_system_by_name('lrms_wn') self.assertEqual(lrms_wn.getValue('memory.size'), 2000000000) lrms_server = radl.get_system_by_name('lrms_server') self.assertEqual(lrms_server.getValue('memory.size'), 1000000000) self.assertEqual(lrms_server.getValue('net_interface.0.dns_name'), 'slurmserver') self.assertEqual("cloudid", radl.deploys[0].cloud_id) self.assertEqual("cloudid", radl.deploys[1].cloud_id) self.assertEqual("cloudid", radl.deploys[2].cloud_id)
def RESTCreateInfrastructure(): try: auth = get_auth_header() except Exception: return return_error(401, "No authentication data provided") try: content_type = get_media_type('Content-Type') radl_data = bottle.request.body.read().decode("utf-8") tosca_data = None async_call = False if "async" in bottle.request.params.keys(): str_ctxt = bottle.request.params.get("async").lower() if str_ctxt in ['yes', 'true', '1']: async_call = True elif str_ctxt in ['no', 'false', '0']: async_call = False else: return return_error(400, "Incorrect value in async parameter") if content_type: if "application/json" in content_type: radl_data = parse_radl_json(radl_data) elif "text/yaml" in content_type: tosca_data = Tosca(radl_data) _, radl_data = tosca_data.to_radl() elif "text/plain" in content_type or "*/*" in content_type or "text/*" in content_type: content_type = "text/plain" else: return return_error(415, "Unsupported Media Type %s" % content_type) inf_id = InfrastructureManager.CreateInfrastructure( radl_data, auth, async_call) # Store the TOSCA document if tosca_data: sel_inf = InfrastructureManager.get_infrastructure(inf_id, auth) sel_inf.extra_info['TOSCA'] = tosca_data bottle.response.headers['InfID'] = inf_id bottle.response.content_type = "text/uri-list" res = get_full_url('/infrastructures/%s' % inf_id) return format_output(res, "text/uri-list", "uri") except InvaliddUserException as ex: return return_error(401, "Error Getting Inf. info: %s" % get_ex_error(ex)) except DisabledFunctionException as ex: return return_error(403, "Error Destroying Inf: %s" % get_ex_error(ex)) except Exception as ex: logger.exception("Error Creating Inf.") return return_error(400, "Error Creating Inf.: %s" % get_ex_error(ex))
def test_tosca_to_radl(self): """Test TOSCA RADL translation""" tosca_data = read_file_as_string('../files/tosca_long.yml') tosca = Tosca(tosca_data) _, radl = tosca.to_radl() radl = parse_radl(str(radl)) net = radl.get_network_by_id('public_net') net1 = radl.get_network_by_id('public_net_1') net2 = radl.get_network_by_id('private_net') self.assertEqual(net2.getValue('provider_id'), 'provider_id') self.assertIn(net.getValue('provider_id'), ['pool_name', None]) if net.getValue('provider_id') is None: self.assertEqual(net1.getValue('provider_id'), 'pool_name') self.assertIn('1:4/tcp', net.getValue("outports")) self.assertIn('80/tcp-80/tcp', net.getValue("outports")) self.assertIn('8080/tcp-8080/tcp', net.getValue("outports")) self.assertEqual(net1.getValue("outports"), '8080/tcp-8080/tcp') else: self.assertEqual(net.getValue('provider_id'), 'pool_name') self.assertEqual(net.getValue("outports"), '8080/tcp-8080/tcp') self.assertIn('1:4/tcp', net1.getValue("outports")) self.assertIn('80/tcp-80/tcp', net1.getValue("outports")) self.assertIn('8080/tcp-8080/tcp', net1.getValue("outports")) self.assertIn('10000/tcp-10000/tcp', net2.getValue("outports")) lrms_wn = radl.get_system_by_name('lrms_wn') self.assertEqual(lrms_wn.getValue('memory.size'), 2000000000) lrms_server = radl.get_system_by_name('lrms_server') self.assertEqual(lrms_server.getValue('memory.size'), 1000000000) self.assertEqual(lrms_server.getValue('net_interface.0.dns_name'), 'slurmserver') self.assertEqual("cloudid", radl.deploys[0].cloud_id) self.assertEqual("cloudid", radl.deploys[1].cloud_id) self.assertEqual("cloudid", radl.deploys[2].cloud_id) other_server = radl.get_system_by_name('other_server') self.assertEqual(other_server.getValue("availability_zone"), 'some_zone') self.assertEqual(lrms_wn.getValue("disk.1.size"), 10000000000) self.assertEqual(lrms_wn.getValue("disk.1.type"), 'ssd') self.assertEqual(lrms_wn.getValue("spot"), 'no') self.assertEqual(lrms_wn.getValue("instance_type"), 'some_type') lrms_front_end_conf = radl.get_configure_by_name('lrms_front_end_conf') conf = yaml.safe_load(lrms_front_end_conf.recipes)[0] self.assertEqual( conf['vars']['front_end_ip'], "{{ hostvars[groups['lrms_server'][0]]['IM_NODE_PRIVATE_IP'] }}") self.assertEqual( conf['vars']['wn_ips'], "{{ groups['lrms_wn']|map('extract', hostvars,'IM_NODE_PRIVATE_IP')|list" " if 'lrms_wn' in groups else []}}") self.assertEqual([d.id for d in radl.deploys][2], 'lrms_wn')
def deserialize(str_data): newinf = InfrastructureInfo() dic = json.loads(str_data) vm_list = dic['vm_list'] vm_master_id = dic['vm_master'] dic['vm_master'] = None dic['vm_list'] = [] if dic['auth']: dic['auth'] = Authentication.deserialize(dic['auth']) if dic['radl']: dic['radl'] = parse_radl(dic['radl']) if 'extra_info' in dic and dic['extra_info'] and "TOSCA" in dic['extra_info']: try: dic['extra_info']['TOSCA'] = Tosca.deserialize(dic['extra_info']['TOSCA']) except: del dic['extra_info']['TOSCA'] InfrastructureInfo.logger.exception("Error deserializing TOSCA document") newinf.__dict__.update(dic) newinf.cloud_connector = None # Set the ConfManager object and the lock to the data loaded newinf.cm = None newinf.ctxt_tasks = PriorityQueue() newinf.conf_threads = [] for vm_data in vm_list: vm = VirtualMachine.deserialize(vm_data) vm.inf = newinf if vm.im_id == vm_master_id: newinf.vm_master = vm newinf.vm_list.append(vm) return newinf
def test_tosca_get_outputs(self): """Test TOSCA get_outputs function""" tosca_data = read_file_as_string('../files/tosca_create.yml') tosca = Tosca(tosca_data) _, radl = tosca.to_radl() radl.systems[0].setValue("net_interface.0.ip", "158.42.1.1") radl.systems[0].setValue("disk.0.os.credentials.username", "ubuntu") radl.systems[0].setValue("disk.0.os.credentials.password", "pass") inf = InfrastructureInfo() vm = VirtualMachine(inf, "1", None, radl, radl, None) vm.requested_radl = radl inf.vm_list = [vm] outputs = tosca.get_outputs(inf) self.assertEqual(outputs, {'server_url': ['158.42.1.1'], 'server_creds': {'token_type': 'password', 'token': 'pass', 'user': '******'}})
def RESTAlterVM(infid=None, vmid=None): try: auth = get_auth_header() except Exception: return return_error(401, "No authentication data provided") try: content_type = get_media_type('Content-Type') radl_data = bottle.request.body.read().decode("utf-8") if content_type: if "application/json" in content_type: radl_data = parse_radl_json(radl_data) elif "text/yaml" in content_type: tosca_data = Tosca(radl_data) _, radl_data = tosca_data.to_radl() elif "text/plain" in content_type or "*/*" in content_type or "text/*" in content_type: content_type = "text/plain" else: return return_error(415, "Unsupported Media Type %s" % content_type) vm_info = InfrastructureManager.AlterVM(infid, vmid, radl_data, auth) return format_output(vm_info, field_name="radl") except DeletedInfrastructureException as ex: return return_error(404, "Error modifying resources: %s" % get_ex_error(ex)) except IncorrectInfrastructureException as ex: return return_error(404, "Error modifying resources: %s" % get_ex_error(ex)) except UnauthorizedUserException as ex: return return_error(403, "Error modifying resources: %s" % get_ex_error(ex)) except DeletedVMException as ex: return return_error(404, "Error modifying resources: %s" % get_ex_error(ex)) except IncorrectVMException as ex: return return_error(404, "Error modifying resources: %s" % get_ex_error(ex)) except DisabledFunctionException as ex: return return_error(403, "Error Destroying Inf: %s" % get_ex_error(ex)) except Exception as ex: logger.exception("Error modifying resources") return return_error(400, "Error modifying resources: %s" % get_ex_error(ex))
def RESTCreateInfrastructure(): try: auth = get_auth_header() except: return return_error(401, "No authentication data provided") try: content_type = get_media_type('Content-Type') radl_data = bottle.request.body.read() tosca_data = None if content_type: if "application/json" in content_type: radl_data = parse_radl_json(radl_data) elif "text/yaml" in content_type: tosca_data = Tosca(radl_data) _, radl_data = tosca_data.to_radl() elif "text/plain" in content_type or "*/*" in content_type or "text/*" in content_type: content_type = "text/plain" else: return return_error(415, "Unsupported Media Type %s" % content_type) inf_id = InfrastructureManager.CreateInfrastructure(radl_data, auth) # Store the TOSCA document if tosca_data: sel_inf = InfrastructureManager.get_infrastructure(inf_id, auth) sel_inf.extra_info['TOSCA'] = tosca_data bottle.response.content_type = "text/uri-list" protocol = "http://" if Config.REST_SSL: protocol = "https://" res = protocol + \ bottle.request.environ['HTTP_HOST'] + \ "/infrastructures/" + str(inf_id) return format_output(res, "text/uri-list", "uri") except InvaliddUserException, ex: return return_error(401, "Error Getting Inf. info: " + str(ex))
def test_tosca_nets_to_radl(self): """Test TOSCA RADL translation with nets""" tosca_data = read_file_as_string('../files/tosca_nets.yml') tosca = Tosca(tosca_data) _, radl = tosca.to_radl() print(radl) radl = parse_radl(str(radl)) net = radl.get_network_by_id('pub_network') net1 = radl.get_network_by_id('network1') self.assertEqual('1194/udp-1194/udp', net.getValue("outports")) self.assertEqual('192.168.0.0/16,vr1_compute', net1.getValue("router")) self.assertEqual('yes', net1.getValue("create")) self.assertEqual('192.168.10.0/24', net1.getValue("cidr")) lrms_wn = radl.get_system_by_name("lrms_wn") self.assertEqual("network1", lrms_wn.getValue("net_interface.0.connection")) lrms_server = radl.get_system_by_name("lrms_server") self.assertEqual("network1", lrms_server.getValue("net_interface.0.connection")) self.assertEqual("pub_network", lrms_server.getValue("net_interface.1.connection")) self.assertEqual("slurmserver", lrms_server.getValue("net_interface.0.dns_name"))
def test_tosca_get_outputs(self): """Test TOSCA get_outputs function""" tosca_data = read_file_as_string('../files/tosca_create.yml') tosca = Tosca(tosca_data) _, radl = tosca.to_radl() radl1 = radl.clone() radl1.systems = [radl.get_system_by_name('web_server')] radl1.systems[0].setValue("net_interface.1.ip", "158.42.1.1") radl1.systems[0].setValue("disk.0.os.credentials.username", "ubuntu") radl1.systems[0].setValue("disk.0.os.credentials.password", "pass") inf = InfrastructureInfo() vm = VirtualMachine(inf, "1", None, radl1, radl1, None) vm.requested_radl = radl1 inf.vm_list = [vm] outputs = tosca.get_outputs(inf) self.assertEqual( outputs, { 'server_url': ['158.42.1.1'], 'server_creds': { 'token_type': 'password', 'token': 'pass', 'user': '******' } })
def test_tosca_to_radl(self): """Test TOSCA RADL translation""" tosca_data = read_file_as_string('../files/tosca_long.yml') tosca = Tosca(tosca_data) _, radl = tosca.to_radl() parse_radl(str(radl))
def RESTAddResource(infid=None): try: auth = get_auth_header() except: return return_error(401, "No authentication data provided") try: context = True if "context" in bottle.request.params.keys(): str_ctxt = bottle.request.params.get("context").lower() if str_ctxt in ['yes', 'true', '1']: context = True elif str_ctxt in ['no', 'false', '0']: context = False else: return return_error(400, "Incorrect value in context parameter") content_type = get_media_type('Content-Type') radl_data = bottle.request.body.read().decode("utf-8") tosca_data = None remove_list = [] if content_type: if "application/json" in content_type: radl_data = parse_radl_json(radl_data) elif "text/yaml" in content_type: tosca_data = Tosca(radl_data) auth = InfrastructureManager.check_auth_data(auth) sel_inf = InfrastructureManager.get_infrastructure(infid, auth) # merge the current TOSCA with the new one if isinstance(sel_inf.extra_info['TOSCA'], Tosca): tosca_data = sel_inf.extra_info['TOSCA'].merge(tosca_data) remove_list, radl_data = tosca_data.to_radl(sel_inf) elif "text/plain" in content_type or "*/*" in content_type or "text/*" in content_type: content_type = "text/plain" else: return return_error(415, "Unsupported Media Type %s" % content_type) if remove_list: InfrastructureManager.RemoveResource(infid, remove_list, auth, context) vm_ids = InfrastructureManager.AddResource(infid, radl_data, auth, context) # Replace the TOSCA document if tosca_data: sel_inf = InfrastructureManager.get_infrastructure(infid, auth) sel_inf.extra_info['TOSCA'] = tosca_data res = [] for vm_id in vm_ids: res.append( get_full_url("/infrastructures/" + str(infid) + "/vms/" + str(vm_id))) return format_output(res, "text/uri-list", "uri-list", "uri") except DeletedInfrastructureException as ex: return return_error(404, "Error Adding resources: %s" % ex.args[0]) except IncorrectInfrastructureException as ex: return return_error(404, "Error Adding resources: %s" % ex.args[0]) except UnauthorizedUserException as ex: return return_error(403, "Error Adding resources: %s" % ex.args[0]) except Exception as ex: logger.exception("Error Adding resources") return return_error(400, "Error Adding resources: %s" % ex.args[0])
def RESTAddResource(id=None): try: auth = get_auth_header() except: return return_error(401, "No authentication data provided") try: context = True if "context" in bottle.request.params.keys(): str_ctxt = bottle.request.params.get("context").lower() if str_ctxt in ['yes', 'true', '1']: context = True elif str_ctxt in ['no', 'false', '0']: context = False else: return return_error(400, "Incorrect value in context parameter") content_type = get_media_type('Content-Type') radl_data = bottle.request.body.read() tosca_data = None remove_list = [] if content_type: if "application/json" in content_type: radl_data = parse_radl_json(radl_data) elif "text/yaml" in content_type: tosca_data = Tosca(radl_data) auth = InfrastructureManager.check_auth_data(auth) sel_inf = InfrastructureManager.get_infrastructure(id, auth) # merge the current TOSCA with the new one if isinstance(sel_inf.extra_info['TOSCA'], Tosca): tosca_data = sel_inf.extra_info['TOSCA'].merge(tosca_data) remove_list, radl_data = tosca_data.to_radl(sel_inf) elif "text/plain" in content_type or "*/*" in content_type or "text/*" in content_type: content_type = "text/plain" else: return return_error(415, "Unsupported Media Type %s" % content_type) if remove_list: InfrastructureManager.RemoveResource( id, remove_list, auth, context) vm_ids = InfrastructureManager.AddResource( id, radl_data, auth, context) # Replace the TOSCA document if tosca_data: sel_inf = InfrastructureManager.get_infrastructure(id, auth) sel_inf.extra_info['TOSCA'] = tosca_data protocol = "http://" if Config.REST_SSL: protocol = "https://" res = [] for vm_id in vm_ids: res.append(protocol + bottle.request.environ[ 'HTTP_HOST'] + "/infrastructures/" + str(id) + "/vms/" + str(vm_id)) return format_output(res, "text/uri-list", "uri-list", "uri") except DeletedInfrastructureException, ex: return return_error(404, "Error Adding resources: " + str(ex))