X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=osm_lcm%2FROclient.py;h=1818e08557230c6d8238c6a1d16035ba52ab682f;hb=3ff2325f97dda222d286d0d8ceb720de8b1a537c;hp=32dd1bf3ac6e6c59cb9edeb1b322c2c1b36c541e;hpb=5697b8b03a3acd17827ce536cb8aff15df8776ad;p=osm%2FLCM.git diff --git a/osm_lcm/ROclient.py b/osm_lcm/ROclient.py index 32dd1bf..1818e08 100644 --- a/osm_lcm/ROclient.py +++ b/osm_lcm/ROclient.py @@ -99,7 +99,7 @@ def remove_envelop(item, indata=None): if len(indata) == 1 and "instance" in indata: clean_indata = indata["instance"] else: - assert False, "remove_envelop with unknown item {}".format(item) + raise ROClientException("remove_envelop with unknown item {}".format(item)) return clean_indata @@ -131,8 +131,7 @@ class ROClient: timeout_large = 120 timeout_short = 30 - def __init__(self, loop, uri, **kwargs): - self.loop = loop + def __init__(self, uri, **kwargs): self.uri = uri self.username = kwargs.get("username") @@ -190,7 +189,7 @@ class ROClient: ) if descriptor_format != "json": try: - return yaml.load(descriptor) + return yaml.safe_load(descriptor) except yaml.YAMLError as exc: error_pos = "" if hasattr(exc, "problem_mark"): @@ -214,7 +213,7 @@ class ROClient: def _parse_error_yaml(descriptor): json_error = None try: - json_error = yaml.load(descriptor, Loader=yaml.Loader) + json_error = yaml.safe_load(descriptor) return json_error["error"]["description"] except Exception: return str(json_error or descriptor) @@ -222,7 +221,7 @@ class ROClient: @staticmethod def _parse_yaml(descriptor, response=False): try: - return yaml.load(descriptor, Loader=yaml.Loader) + return yaml.safe_load(descriptor) except yaml.YAMLError as exc: error_pos = "" if hasattr(exc, "problem_mark"): @@ -273,7 +272,7 @@ class ROClient: elif item == "sdn": return {"sdn_controller": indata} else: - assert False, "_create_envelop with unknown item {}".format(item) + raise ROClientException("remove_envelop with unknown item {}".format(item)) @staticmethod def update_descriptor(desc, kwargs): @@ -549,9 +548,10 @@ class ROClient: if item_id: return item_id desc = content[item] - assert isinstance( - desc, list - ), "_get_item_uuid get a non dict with a list inside {}".format(type(desc)) + if not isinstance(desc, list): + raise ROClientException( + "_get_item_uuid get a non dict with a list inside {}".format(type(desc)) + ) uuid = None for i in desc: if item_id_name and i["name"] != item_id_name: @@ -829,6 +829,11 @@ class ROClient: except asyncio.TimeoutError: raise ROClientException("Timeout", http_code=504) except Exception as e: + self.logger.critical( + "Got invalid version text: '{}'; causing exception {}".format( + response_text, str(e) + ) + ) raise ROClientException( "Got invalid version text: '{}'; causing exception {}".format( response_text, e @@ -849,7 +854,7 @@ class ROClient: raise ROClientException("Invalid item {}".format(item)) if item == "tenant": all_tenants = None - async with aiohttp.ClientSession(loop=self.loop) as session: + async with aiohttp.ClientSession() as session: content = await self._list_item( session, self.client_to_RO[item], @@ -899,7 +904,7 @@ class ROClient: elif item == "vim_account": all_tenants = False - async with aiohttp.ClientSession(loop=self.loop) as session: + async with aiohttp.ClientSession() as session: content = await self._get_item( session, self.client_to_RO[item], @@ -928,7 +933,7 @@ class ROClient: if item in ("tenant", "vim", "wim"): all_tenants = None - async with aiohttp.ClientSession(loop=self.loop) as session: + async with aiohttp.ClientSession() as session: result = await self._del_item( session, self.client_to_RO[item], @@ -980,7 +985,7 @@ class ROClient: create_desc = self._create_envelop(item, desc) - async with aiohttp.ClientSession(loop=self.loop) as session: + async with aiohttp.ClientSession() as session: _all_tenants = all_tenants if item == "vim": _all_tenants = True @@ -1044,7 +1049,7 @@ class ROClient: create_desc = self._create_envelop(item, desc) - async with aiohttp.ClientSession(loop=self.loop) as session: + async with aiohttp.ClientSession() as session: outdata = await self._create_item( session, self.client_to_RO[item], @@ -1099,7 +1104,7 @@ class ROClient: # create_desc = self._create_envelop(item, desc) create_desc = desc - async with aiohttp.ClientSession(loop=self.loop) as session: + async with aiohttp.ClientSession() as session: _all_tenants = all_tenants if item == "vim": _all_tenants = True @@ -1161,7 +1166,7 @@ class ROClient: ) create_desc = self._create_envelop(item, desc) payload_req = yaml.safe_dump(create_desc) - async with aiohttp.ClientSession(loop=self.loop) as session: + async with aiohttp.ClientSession() as session: # check that exist item_id = await self._get_item_uuid( session, self.client_to_RO[item], item_id_name, all_tenants=True @@ -1202,7 +1207,7 @@ class ROClient: async def detach(self, item, item_id_name=None): # TODO replace the code with delete_item(vim_account,...) try: - async with aiohttp.ClientSession(loop=self.loop) as session: + async with aiohttp.ClientSession() as session: # check that exist item_id = await self._get_item_uuid( session, self.client_to_RO[item], item_id_name, all_tenants=False @@ -1268,7 +1273,7 @@ class ROClient: """ if isinstance(descriptor, str): - descriptor = self.parse(descriptor, descriptor_format) + descriptor = self._parse(descriptor, descriptor_format) elif descriptor: pass elif kwargs: @@ -1311,7 +1316,7 @@ class ROClient: """ if isinstance(descriptor, str): - descriptor = self.parse(descriptor, descriptor_format) + descriptor = self._parse(descriptor, descriptor_format) elif descriptor: pass elif kwargs: @@ -1363,7 +1368,7 @@ class ROClient: if all_tenants: tenant_text = "/any" else: - tenant_text = "/" + self._get_tenant() + tenant_text = "/" + self._get_tenant(session) if "datacenter_id" in kwargs or "datacenter_name" in kwargs: datacenter = self._get_item_uuid( @@ -1373,7 +1378,7 @@ class ROClient: all_tenants=all_tenants, ) else: - datacenter = self.get_datacenter(session) + datacenter = self._get_datacenter(session) if action == "list": url = "{}{}/vim/{}/{}".format(self.uri, tenant_text, datacenter, item) @@ -1445,119 +1450,3 @@ class ROClient: raise ROClientException(str(content), http_code=mano_response.status) else: raise ROClientException("Unknown value for action '{}".format(str(action))) - - -if __name__ == "__main__": - RO_URL = "http://localhost:9090/openmano" - TEST_TENANT = "myTenant" - TEST_VIM1 = "myvim" - TEST_URL1 = "https://localhost:5000/v1" - TEST_TYPE1 = "openstack" - TEST_CONFIG1 = {"use_floating_ip": True} - TEST_VIM2 = "myvim2" - TEST_URL2 = "https://localhost:5000/v2" - TEST_TYPE2 = "openvim" - TEST_CONFIG2 = {"config2": "config2", "config3": True} - - streamformat = "%(asctime)s %(name)s %(levelname)s: %(message)s" - logging.basicConfig(format=streamformat) - logger = logging.getLogger("ROClient") - - tenant_id = None - vim_id = False - loop = asyncio.get_event_loop() - myClient = ROClient(uri=RO_URL, loop=loop, loglevel="DEBUG") - try: - # test tenant - content = loop.run_until_complete(myClient.get_list("tenant")) - print("tenants", content) - content = loop.run_until_complete(myClient.create("tenant", name=TEST_TENANT)) - tenant_id = True - content = loop.run_until_complete(myClient.show("tenant", TEST_TENANT)) - print("tenant", TEST_TENANT, content) - content = loop.run_until_complete( - myClient.edit("tenant", TEST_TENANT, description="another description") - ) - content = loop.run_until_complete(myClient.show("tenant", TEST_TENANT)) - print("tenant edited", TEST_TENANT, content) - myClient["tenant"] = TEST_TENANT - - # test VIM - content = loop.run_until_complete( - myClient.create( - "vim", - name=TEST_VIM1, - type=TEST_TYPE1, - vim_url=TEST_URL1, - config=TEST_CONFIG1, - ) - ) - vim_id = True - content = loop.run_until_complete(myClient.get_list("vim")) - print("vim", content) - content = loop.run_until_complete(myClient.show("vim", TEST_VIM1)) - print("vim", TEST_VIM1, content) - content = loop.run_until_complete( - myClient.edit( - "vim", - TEST_VIM1, - description="another description", - name=TEST_VIM2, - type=TEST_TYPE2, - vim_url=TEST_URL2, - config=TEST_CONFIG2, - ) - ) - content = loop.run_until_complete(myClient.show("vim", TEST_VIM2)) - print("vim edited", TEST_VIM2, content) - - # test VIM_ACCOUNT - content = loop.run_until_complete( - myClient.attach_datacenter( - TEST_VIM2, - vim_username="user", - vim_password="pass", - vim_tenant_name="vimtenant1", - config=TEST_CONFIG1, - ) - ) - vim_id = True - content = loop.run_until_complete(myClient.get_list("vim_account")) - print("vim_account", content) - content = loop.run_until_complete(myClient.show("vim_account", TEST_VIM2)) - print("vim_account", TEST_VIM2, content) - content = loop.run_until_complete( - myClient.edit( - "vim_account", - TEST_VIM2, - vim_username="user2", - vim_password="pass2", - vim_tenant_name="vimtenant2", - config=TEST_CONFIG2, - ) - ) - content = loop.run_until_complete(myClient.show("vim_account", TEST_VIM2)) - print("vim_account edited", TEST_VIM2, content) - - myClient["vim"] = TEST_VIM2 - - except Exception as e: - logger.error("Error {}".format(e), exc_info=True) - - for item in ( - ("vim_account", TEST_VIM1), - ("vim", TEST_VIM1), - ("vim_account", TEST_VIM2), - ("vim", TEST_VIM2), - ("tenant", TEST_TENANT), - ): - try: - content = loop.run_until_complete(myClient.delete(item[0], item[1])) - print("{} {} deleted; {}".format(item[0], item[1], content)) - except Exception as e: - if e.http_code == 404: - print("{} {} not present or already deleted".format(item[0], item[1])) - else: - logger.error("Error {}".format(e), exc_info=True) - - loop.close()