Ubuntu 22.04 and Python 3.10 preparation
[osm/LCM.git] / osm_lcm / ROclient.py
index 8d6f510..b924906 100644 (file)
@@ -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")
@@ -829,6 +828,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 +853,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 +903,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 +932,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 +984,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 +1048,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 +1103,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 +1165,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 +1206,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
@@ -1445,119 +1449,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()