Updating python dependencies
[osm/osmclient.git] / osmclient / sol005 / vim.py
index b6f1614..4962a91 100644 (file)
@@ -22,7 +22,6 @@ from osmclient.common import utils
 from osmclient.common import wait as WaitForStatus
 from osmclient.common.exceptions import ClientException
 from osmclient.common.exceptions import NotFound
 from osmclient.common import wait as WaitForStatus
 from osmclient.common.exceptions import ClientException
 from osmclient.common.exceptions import NotFound
-from osmclient.common.exceptions import OsmHttpException
 import yaml
 import json
 import logging
 import yaml
 import json
 import logging
@@ -32,80 +31,99 @@ class Vim(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 = '/vim_accounts'
-        self._apiBase = '{}{}{}'.format(self._apiName,
-                                        self._apiVersion, self._apiResource)
+        self._logger = logging.getLogger("osmclient")
+        self._apiName = "/admin"
+        self._apiVersion = "/v1"
+        self._apiResource = "/vim_accounts"
+        self._apiBase = "{}{}{}".format(
+            self._apiName, self._apiVersion, self._apiResource
+        )
 
     # VIM '--wait' option
 
     # VIM '--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, '/vim_accounts')
+        apiUrlStatus = "{}{}{}".format(self._apiName, self._apiVersion, "/vim_accounts")
         # Wait for status for VIM instance creation/deletion
         # Wait for status for VIM instance creation/deletion
+        if isinstance(wait_time, bool):
+            wait_time = WaitForStatus.TIMEOUT_VIM_OPERATION
         WaitForStatus.wait_for_status(
         WaitForStatus.wait_for_status(
-            'VIM',
+            "VIM",
             str(id),
             str(id),
-            WaitForStatus.TIMEOUT_VIM_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("")
         self._client.get_token()
         for vim in self.list():
         self._logger.debug("")
         self._client.get_token()
         for vim in self.list():
-            if name == vim['uuid']:
-                return vim['uuid']
+            if name == vim["uuid"]:
+                return vim["uuid"]
         for vim in self.list():
         for vim in self.list():
-            if name == vim['name']:
-                return vim['uuid']
-        return ''
+            if name == vim["name"]:
+                return vim["uuid"]
+        return ""
+
+    def create(
+        self,
+        name,
+        vim_access,
+        config={},
+        sdn_controller=None,
+        sdn_port_mapping=None,
+        wait=False,
+    ):
+        vca_id = None
+
+        def get_vca_id(vca):
+            vca = self._client.vca.get(vca)
+            if vca is None:
+                raise NotFound("cannot find vca '{}'".format(vca))
+            return vca["_id"]
 
 
-    def create(self, name, vim_access, sdn_controller=None, sdn_port_mapping=None, wait=False):
         self._logger.debug("")
         self._client.get_token()
         self._logger.debug("")
         self._client.get_token()
-        if 'vim-type' not in vim_access:
-            #'openstack' not in vim_access['vim-type']):
+        if "vca" in vim_access:
+            vca_id = get_vca_id(vim_access["vca"])
+        if "vim-type" not in vim_access:
+            # 'openstack' not in vim_access['vim-type']):
             raise Exception("vim type not provided")
             raise Exception("vim type not provided")
-
         vim_account = {}
         vim_account = {}
-        vim_account['name'] = name
+        vim_account["name"] = name
         vim_account = self.update_vim_account_dict(vim_account, vim_access)
         vim_account = self.update_vim_account_dict(vim_account, vim_access)
-
-        vim_config = {}
-        if 'config' in vim_access and vim_access['config'] is not None:
-            vim_config = yaml.safe_load(vim_access['config'])
+        if vca_id:
+            vim_account["vca"] = vca_id
+        vim_config = config
         if sdn_controller:
             sdnc = self._client.sdnc.get(sdn_controller)
         if sdn_controller:
             sdnc = self._client.sdnc.get(sdn_controller)
-            vim_config['sdn-controller'] = sdnc['_id']
+            vim_config["sdn-controller"] = sdnc["_id"]
         if sdn_port_mapping:
         if sdn_port_mapping:
-            with open(sdn_port_mapping, 'r') as f:
-                vim_config['sdn-port-mapping'] = yaml.safe_load(f.read())
+            with open(sdn_port_mapping, "r") as f:
+                vim_config["sdn-port-mapping"] = yaml.safe_load(f.read())
         if vim_config:
         if vim_config:
-            vim_account['config'] = vim_config
-            #vim_account['config'] = json.dumps(vim_config)
-
-        http_code, resp = self._http.post_cmd(endpoint=self._apiBase,
-                                       postfields_dict=vim_account)
-        #print('HTTP CODE: {}'.format(http_code))
-        #print('RESP: {}'.format(resp))
-        #if http_code in (200, 201, 202, 204):
+            vim_account["config"] = vim_config
+            # vim_account['config'] = json.dumps(vim_config)
+
+        http_code, resp = self._http.post_cmd(
+            endpoint=self._apiBase, postfields_dict=vim_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 VIM instance creation
         if wait:
             # Wait for status for VIM 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,41 +132,55 @@ class Vim(object):
         #            msg = resp
         #    raise ClientException("failed to create vim {} - {}".format(name, msg))
 
         #            msg = resp
         #    raise ClientException("failed to create vim {} - {}".format(name, msg))
 
-    def update(self, vim_name, vim_account, sdn_controller, sdn_port_mapping, wait=False):
+    def update(
+        self,
+        vim_name,
+        vim_account,
+        config,
+        sdn_controller,
+        sdn_port_mapping,
+        wait=False,
+    ):
         self._logger.debug("")
         self._client.get_token()
         vim = self.get(vim_name)
         vim_id_for_wait = self._get_id_for_wait(vim_name)
         vim_config = {}
         self._logger.debug("")
         self._client.get_token()
         vim = self.get(vim_name)
         vim_id_for_wait = self._get_id_for_wait(vim_name)
         vim_config = {}
-        if 'config' in vim_account:
-            if vim_account.get('config')=="" and (sdn_controller or sdn_port_mapping):
-                raise ClientException("clearing config is incompatible with updating SDN info")
-            if vim_account.get('config')=="":
-                vim_config = None
-            else:
-                vim_config = yaml.safe_load(vim_account['config'])
-        if sdn_controller:
-            sdnc = self._client.sdnc.get(sdn_controller)
-            vim_config['sdn-controller'] = sdnc['_id']
-        if sdn_port_mapping:
-            with open(sdn_port_mapping, 'r') as f:
-                vim_config['sdn-port-mapping'] = yaml.safe_load(f.read())
-        vim_account['config'] = vim_config
-        #vim_account['config'] = json.dumps(vim_config)
-        http_code, resp = self._http.patch_cmd(endpoint='{}/{}'.format(self._apiBase,vim['_id']),
-                                       postfields_dict=vim_account)
+        if config is not None:
+            if not config and (sdn_controller or sdn_port_mapping):
+                # If config is empty (clearing config)
+                raise ClientException(
+                    "clearing config is incompatible with updating SDN info"
+                )
+            vim_config = config
+        if sdn_controller == "":
+            vim_config["sdn-controller"] = None
+            vim_config["sdn-port-mapping"] = None
+        else:
+            if sdn_controller:
+                sdnc = self._client.sdnc.get(sdn_controller)
+                vim_config["sdn-controller"] = sdnc["_id"]
+            if sdn_port_mapping:
+                with open(sdn_port_mapping, "r") as f:
+                    vim_config["sdn-port-mapping"] = yaml.safe_load(f.read())
+        vim_account["config"] = vim_config
+        # vim_account['config'] = json.dumps(vim_config)
+        http_code, resp = self._http.patch_cmd(
+            endpoint="{}/{}".format(self._apiBase, vim["_id"]),
+            postfields_dict=vim_account,
+        )
         # print('HTTP CODE: {}'.format(http_code))
         # print('RESP: {}'.format(resp))
         # print('HTTP CODE: {}'.format(http_code))
         # print('RESP: {}'.format(resp))
-        #if http_code in (200, 201, 202, 204):
+        # 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 = vim_id_for_wait
             # Wait for status for VI 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 = vim_id_for_wait
             # Wait for status for VI 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:
@@ -159,21 +191,22 @@ class Vim(object):
 
     def update_vim_account_dict(self, vim_account, vim_access):
         self._logger.debug("")
 
     def update_vim_account_dict(self, vim_account, vim_access):
         self._logger.debug("")
-        vim_account['vim_type'] = vim_access['vim-type']
-        vim_account['description'] = vim_access['description']
-        vim_account['vim_url'] = vim_access['vim-url']
-        vim_account['vim_user'] = vim_access['vim-username']
-        vim_account['vim_password'] = vim_access['vim-password']
-        vim_account['vim_tenant_name'] = vim_access['vim-tenant-name']
+        vim_account["vim_type"] = vim_access["vim-type"]
+        vim_account["description"] = vim_access["description"]
+        vim_account["vim_url"] = vim_access["vim-url"] or "null"
+        vim_account["vim_user"] = vim_access["vim-username"] or "null"
+        vim_account["vim_password"] = vim_access["vim-password"] or "null"
+        vim_account["vim_tenant_name"] = vim_access["vim-tenant-name"] or "null"
+        if "prometheus-config" in vim_access:
+            vim_account["prometheus-config"] = vim_access["prometheus-config"]
         return vim_account
 
     def get_id(self, name):
         return vim_account
 
     def get_id(self, name):
-        """Returns a VIM id from a VIM name
-        """
+        """Returns a VIM id from a VIM name"""
         self._logger.debug("")
         for vim in self.list():
         self._logger.debug("")
         for vim in self.list():
-            if name == vim['name']:
-                return vim['uuid']
+            if name == vim["name"]:
+                return vim["uuid"]
         raise NotFound("vim {} not found".format(name))
 
     def delete(self, vim_name, force=False, wait=False):
         raise NotFound("vim {} not found".format(name))
 
     def delete(self, vim_name, force=False, wait=False):
@@ -182,13 +215,14 @@ class Vim(object):
         vim_id = vim_name
         if not utils.validate_uuid4(vim_name):
             vim_id = self.get_id(vim_name)
         vim_id = vim_name
         if not utils.validate_uuid4(vim_name):
             vim_id = self.get_id(vim_name)
-        querystring = ''
+        querystring = ""
         if force:
         if force:
-            querystring = '?FORCE=True'
-        http_code, resp = self._http.delete_cmd('{}/{}{}'.format(self._apiBase,
-                                         vim_id, querystring))
-        #print('HTTP CODE: {}'.format(http_code))
-        #print('RESP: {}'.format(resp))
+            querystring = "?FORCE=True"
+        http_code, resp = self._http.delete_cmd(
+            "{}/{}{}".format(self._apiBase, vim_id, querystring)
+        )
+        # print('HTTP CODE: {}'.format(http_code))
+        # print('RESP: {}'.format(resp))
         if http_code == 202:
             if wait:
                 # When deleting an account, 'resp' may be None.
         if http_code == 202:
             if wait:
                 # When deleting an account, 'resp' may be None.
@@ -196,53 +230,50 @@ class Vim(object):
                 wait_id = vim_id
                 if resp:
                     resp = json.loads(resp)
                 wait_id = vim_id
                 if resp:
                     resp = json.loads(resp)
-                    wait_id = resp.get('id')
+                    wait_id = resp.get("id")
                 # Wait for status for VIM account deletion
                 # Wait for status for VIM 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 vim {} - {}".format(vim_name, msg))
+            msg = resp or ""
+            if resp:
+                try:
+                    msg = json.loads(resp)
+                except ValueError:
+                    msg = resp
+            raise ClientException("failed to delete vim {} - {}".format(vim_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()
         if not resp:
             return list()
-        vim_accounts = []
-        for datacenter in json.loads(resp):
-            vim_accounts.append({"name": datacenter['name'], "uuid": datacenter['_id']
-                        if '_id' in datacenter else None})
+        vim_accounts = json.loads(resp)
+        for datacenter in vim_accounts:
+            datacenter["uuid"] = datacenter.get("_id")  # backward compatibility?
         return vim_accounts
 
     def get(self, name):
         return vim_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()
         vim_id = name
         if not utils.validate_uuid4(name):
             vim_id = self.get_id(name)
         self._logger.debug("")
         self._client.get_token()
         vim_id = name
         if not utils.validate_uuid4(name):
             vim_id = self.get_id(name)
-        _, resp = self._http.get2_cmd('{}/{}'.format(self._apiBase,vim_id))
-#        if not resp or '_id' not in resp:
-#            raise ClientException('failed to get vim info: '.format(
-#                                  resp))
-#        else:
-        if resp:
-            return json.loads(resp)
-        raise NotFound("vim {} not found".format(name))
-
+        try:
+            _, resp = self._http.get2_cmd("{}/{}".format(self._apiBase, vim_id))
+            if resp:
+                resp = json.loads(resp)
+            if not resp or "_id" not in resp:
+                raise ClientException("failed to get vim info: {}".format(resp))
+            return resp
+        except NotFound:
+            raise NotFound("vim '{}' not found".format(name))