Feature 10476: New client commands and library to manage subscriptions
[osm/osmclient.git] / osmclient / sol005 / wim.py
index b5a03b8..2466113 100644 (file)
@@ -21,7 +21,6 @@ OSM wim API handling
 from osmclient.common import utils
 from osmclient.common import wait as WaitForStatus
 from osmclient.common.exceptions import ClientException
 from osmclient.common import utils
 from osmclient.common import wait as WaitForStatus
 from osmclient.common.exceptions import ClientException
-from osmclient.common.exceptions import OsmHttpException
 from osmclient.common.exceptions import NotFound
 import yaml
 import json
 from osmclient.common.exceptions import NotFound
 import yaml
 import json
@@ -32,74 +31,77 @@ class Wim(object):
     def __init__(self, http=None, client=None):
         self._http = http
         self._client = client
     def __init__(self, http=None, client=None):
         self._http = http
         self._client = client
-        self._logger = logging.getLogger('osmclient')
-        self._apiName = '/admin'
-        self._apiVersion = '/v1'
-        self._apiResource = '/wim_accounts'
-        self._apiBase = '{}{}{}'.format(self._apiName,
-                                        self._apiVersion, self._apiResource)
+        self._logger = logging.getLogger("osmclient")
+        self._apiName = "/admin"
+        self._apiVersion = "/v1"
+        self._apiResource = "/wim_accounts"
+        self._apiBase = "{}{}{}".format(
+            self._apiName, self._apiVersion, self._apiResource
+        )
 
     # WIM '--wait' option
 
     # WIM '--wait' option
-    def _wait(self, id, deleteFlag=False):
+    def _wait(self, id, wait_time, deleteFlag=False):
         self._logger.debug("")
         self._client.get_token()
         # Endpoint to get operation status
         self._logger.debug("")
         self._client.get_token()
         # Endpoint to get operation status
-        apiUrlStatus = '{}{}{}'.format(self._apiName, self._apiVersion, '/wim_accounts')
+        apiUrlStatus = "{}{}{}".format(self._apiName, self._apiVersion, "/wim_accounts")
         # Wait for status for WIM instance creation/deletion
         # Wait for status for WIM instance creation/deletion
+        if isinstance(wait_time, bool):
+            wait_time = WaitForStatus.TIMEOUT_WIM_OPERATION
         WaitForStatus.wait_for_status(
         WaitForStatus.wait_for_status(
-            'WIM',
+            "WIM",
             str(id),
             str(id),
-            WaitForStatus.TIMEOUT_WIM_OPERATION,
+            wait_time,
             apiUrlStatus,
             self._http.get2_cmd,
             apiUrlStatus,
             self._http.get2_cmd,
-            deleteFlag=deleteFlag)
+            deleteFlag=deleteFlag,
+        )
 
     def _get_id_for_wait(self, name):
 
     def _get_id_for_wait(self, name):
-        """Returns id of name, or the id itself if given as argument
-        """
+        """Returns id of name, or the id itself if given as argument"""
         self._logger.debug("")
         for wim in self.list():
         self._logger.debug("")
         for wim in self.list():
-            if name == wim['uuid']:
-                return wim['uuid']
+            if name == wim["uuid"]:
+                return wim["uuid"]
         for wim in self.list():
         for wim in self.list():
-            if name == wim['name']:
-                return wim['uuid']
-        return ''
+            if name == wim["name"]:
+                return wim["uuid"]
+        return ""
 
     def create(self, name, wim_input, wim_port_mapping=None, wait=False):
         self._logger.debug("")
         self._client.get_token()
 
     def create(self, name, wim_input, wim_port_mapping=None, wait=False):
         self._logger.debug("")
         self._client.get_token()
-        if 'wim_type' not in wim_input:
+        if "wim_type" not in wim_input:
             raise Exception("wim type not provided")
 
         wim_account = wim_input
         wim_account["name"] = name
 
         wim_config = {}
             raise Exception("wim type not provided")
 
         wim_account = wim_input
         wim_account["name"] = name
 
         wim_config = {}
-        if 'config' in wim_input and wim_input['config'] is not None:
-            wim_config = yaml.safe_load(wim_input['config'])
+        if "config" in wim_input and wim_input["config"] is not None:
+            wim_config = yaml.safe_load(wim_input["config"])
         if wim_port_mapping:
         if wim_port_mapping:
-            with open(wim_port_mapping, 'r') as f:
-                wim_config['wim_port_mapping'] = yaml.safe_load(f.read())
+            with open(wim_port_mapping, "r") as f:
+                wim_config["wim_port_mapping"] = yaml.safe_load(f.read())
         if wim_config:
         if wim_config:
-            wim_account['config'] = wim_config
-            #wim_account['config'] = json.dumps(wim_config)
-
-        http_code, resp = self._http.post_cmd(endpoint=self._apiBase,
-                                       postfields_dict=wim_account)
-        #print('HTTP CODE: {}'.format(http_code))
-        #print('RESP: {}'.format(resp))
-        #if http_code in (200, 201, 202, 204):
+            wim_account["config"] = wim_config
+            # wim_account['config'] = json.dumps(wim_config)
+
+        http_code, resp = self._http.post_cmd(
+            endpoint=self._apiBase, postfields_dict=wim_account
+        )
+        # print('HTTP CODE: {}'.format(http_code))
+        # print('RESP: {}'.format(resp))
+        # if http_code in (200, 201, 202, 204):
         if resp:
             resp = json.loads(resp)
         if resp:
             resp = json.loads(resp)
-        if not resp or 'id' not in resp:
-            raise OsmHttpException('unexpected response from server - {}'.format(
-                                  resp))
+        if not resp or "id" not in resp:
+            raise ClientException("unexpected response from server - {}".format(resp))
         if wait:
             # Wait for status for WIM instance creation
         if wait:
             # Wait for status for WIM instance creation
-            self._wait(resp.get('id'))
-        print(resp['id'])
-        #else:
+            self._wait(resp.get("id"), wait)
+        print(resp["id"])
+        # else:
         #    msg = ""
         #    if resp:
         #        try:
         #    msg = ""
         #    if resp:
         #        try:
@@ -114,32 +116,36 @@ class Wim(object):
         wim = self.get(wim_name)
         wim_id_for_wait = self._get_id_for_wait(wim_name)
         wim_config = {}
         wim = self.get(wim_name)
         wim_id_for_wait = self._get_id_for_wait(wim_name)
         wim_config = {}
-        if 'config' in wim_account:
-            if wim_account.get('config')=="" and (wim_port_mapping):
-                raise ClientException("clearing config is incompatible with updating SDN info")
-            if wim_account.get('config')=="":
+        if "config" in wim_account:
+            if wim_account.get("config") == "" and (wim_port_mapping):
+                raise ClientException(
+                    "clearing config is incompatible with updating SDN info"
+                )
+            if wim_account.get("config") == "":
                 wim_config = None
             else:
                 wim_config = None
             else:
-                wim_config = yaml.safe_load(wim_account['config'])
+                wim_config = yaml.safe_load(wim_account["config"])
         if wim_port_mapping:
         if wim_port_mapping:
-            with open(wim_port_mapping, 'r') as f:
-                wim_config['wim_port_mapping'] = yaml.safe_load(f.read())
-        wim_account['config'] = wim_config
-        #wim_account['config'] = json.dumps(wim_config)
-        http_code, resp = self._http.patch_cmd(endpoint='{}/{}'.format(self._apiBase,wim['_id']),
-                                       postfields_dict=wim_account)
-        #print('HTTP CODE: {}'.format(http_code))
-        #print('RESP: {}'.format(resp))
-        #if http_code in (200, 201, 202, 204):
+            with open(wim_port_mapping, "r") as f:
+                wim_config["wim_port_mapping"] = yaml.safe_load(f.read())
+        wim_account["config"] = wim_config
+        # wim_account['config'] = json.dumps(wim_config)
+        http_code, resp = self._http.patch_cmd(
+            endpoint="{}/{}".format(self._apiBase, wim["_id"]),
+            postfields_dict=wim_account,
+        )
+        # print('HTTP CODE: {}'.format(http_code))
+        # print('RESP: {}'.format(resp))
+        # if http_code in (200, 201, 202, 204):
         if wait:
             # In this case, 'resp' always returns None, so 'resp['id']' cannot be used.
             # Use the previously obtained id instead.
             wait_id = wim_id_for_wait
             # Wait for status for WIM instance update
         if wait:
             # In this case, 'resp' always returns None, so 'resp['id']' cannot be used.
             # Use the previously obtained id instead.
             wait_id = wim_id_for_wait
             # Wait for status for WIM instance update
-            self._wait(wait_id)
-        else:
-            pass
-        #else:
+            self._wait(wait_id, wait)
+        else:
+            pass
+        # else:
         #    msg = ""
         #    if resp:
         #        try:
         #    msg = ""
         #    if resp:
         #        try:
@@ -151,20 +157,19 @@ class Wim(object):
     def update_wim_account_dict(self, wim_account, wim_input):
         self._logger.debug("")
         self._logger.debug(str(wim_input))
     def update_wim_account_dict(self, wim_account, wim_input):
         self._logger.debug("")
         self._logger.debug(str(wim_input))
-        wim_account['wim_type'] = wim_input['wim_type']
-        wim_account['description'] = wim_input['description']
-        wim_account['wim_url'] = wim_input['url']
-        wim_account['user'] = wim_input.get('wim-username')
-        wim_account['password'] = wim_input.get('wim-password')
+        wim_account["wim_type"] = wim_input["wim_type"]
+        wim_account["description"] = wim_input["description"]
+        wim_account["wim_url"] = wim_input["url"]
+        wim_account["user"] = wim_input.get("wim-username")
+        wim_account["password"] = wim_input.get("wim-password")
         return wim_account
 
     def get_id(self, name):
         return wim_account
 
     def get_id(self, name):
-        """Returns a WIM id from a WIM name
-        """
+        """Returns a WIM id from a WIM name"""
         self._logger.debug("")
         for wim in self.list():
         self._logger.debug("")
         for wim in self.list():
-            if name == wim['name']:
-                return wim['uuid']
+            if name == wim["name"]:
+                return wim["uuid"]
         raise NotFound("wim {} not found".format(name))
 
     def delete(self, wim_name, force=False, wait=False):
         raise NotFound("wim {} not found".format(name))
 
     def delete(self, wim_name, force=False, wait=False):
@@ -174,11 +179,12 @@ class Wim(object):
         wim_id_for_wait = self._get_id_for_wait(wim_name)
         if not utils.validate_uuid4(wim_name):
             wim_id = self.get_id(wim_name)
         wim_id_for_wait = self._get_id_for_wait(wim_name)
         if not utils.validate_uuid4(wim_name):
             wim_id = self.get_id(wim_name)
-        querystring = ''
+        querystring = ""
         if force:
         if force:
-            querystring = '?FORCE=True'
-        http_code, resp = self._http.delete_cmd('{}/{}{}'.format(self._apiBase,
-                                         wim_id, querystring))
+            querystring = "?FORCE=True"
+        http_code, resp = self._http.delete_cmd(
+            "{}/{}{}".format(self._apiBase, wim_id, querystring)
+        )
         # print('HTTP CODE: {}'.format(http_code))
         # print('RESP: {}'.format(resp))
         # print('WIM_ID: {}'.format(wim_id))
         # print('HTTP CODE: {}'.format(http_code))
         # print('RESP: {}'.format(resp))
         # print('WIM_ID: {}'.format(wim_id))
@@ -189,53 +195,55 @@ class Wim(object):
                 wait_id = wim_id_for_wait
                 if resp:
                     resp = json.loads(resp)
                 wait_id = wim_id_for_wait
                 if resp:
                     resp = json.loads(resp)
-                    wait_id = resp.get('id')
+                    wait_id = resp.get("id")
                 # Wait for status for WIM account deletion
                 # Wait for status for WIM account deletion
-                self._wait(wait_id, deleteFlag=True)
+                self._wait(wait_id, wait, deleteFlag=True)
             else:
             else:
-                print('Deletion in progress')
+                print("Deletion in progress")
         elif http_code == 204:
         elif http_code == 204:
-            print('Deleted')
+            print("Deleted")
         else:
         else:
-            msg = ""
-            if resp:
-                try:
-                    msg = json.loads(resp)
-                except ValueError:
-                    msg = resp
-            raise OsmHttpException("failed to delete wim {} - {}".format(wim_name, msg))
+            msg = resp or ""
+            if resp:
+                try:
+                    msg = json.loads(resp)
+                except ValueError:
+                    msg = resp
+            raise ClientException("failed to delete wim {} - {}".format(wim_name, msg))
 
     def list(self, filter=None):
 
     def list(self, filter=None):
-        """Returns a list of VIM accounts
-        """
+        """Returns a list of VIM accounts"""
         self._logger.debug("")
         self._client.get_token()
         self._logger.debug("")
         self._client.get_token()
-        filter_string = ''
+        filter_string = ""
         if filter:
         if filter:
-            filter_string = '?{}'.format(filter)
-        _, resp = self._http.get2_cmd('{}{}'.format(self._apiBase,filter_string))
+            filter_string = "?{}".format(filter)
+        _, resp = self._http.get2_cmd("{}{}".format(self._apiBase, filter_string))
         if not resp:
             return list()
         wim_accounts = []
         for datacenter in json.loads(resp):
         if not resp:
             return list()
         wim_accounts = []
         for datacenter in json.loads(resp):
-            wim_accounts.append({"name": datacenter['name'], "uuid": datacenter['_id']
-                        if '_id' in datacenter else None})
+            wim_accounts.append(
+                {
+                    "name": datacenter["name"],
+                    "uuid": datacenter["_id"] if "_id" in datacenter else None,
+                }
+            )
         return wim_accounts
 
     def get(self, name):
         return wim_accounts
 
     def get(self, name):
-        """Returns a VIM account based on name or id
-        """
+        """Returns a VIM account based on name or id"""
         self._logger.debug("")
         self._client.get_token()
         wim_id = name
         if not utils.validate_uuid4(name):
             wim_id = self.get_id(name)
         self._logger.debug("")
         self._client.get_token()
         wim_id = name
         if not utils.validate_uuid4(name):
             wim_id = self.get_id(name)
-        _, resp = self._http.get2_cmd('{}/{}'.format(self._apiBase,wim_id))
-#        if not resp or '_id' not in resp:
-#            raise ClientException('failed to get wim info: '.format(
-#                                  resp))
-#        else:
-        if resp:
-            return json.loads(resp)
-        raise NotFound("wim {} not found".format(name))
-
+        try:
+            _, resp = self._http.get2_cmd("{}/{}".format(self._apiBase, wim_id))
+            if resp:
+                resp = json.loads(resp)
+            if not resp or "_id" not in resp:
+                raise ClientException("failed to get wim info: {}".format(resp))
+            return resp
+        except NotFound:
+            raise NotFound("wim '{}' not found".format(name))