fix wait option when operation fails
[osm/osmclient.git] / osmclient / sol005 / vim.py
index b6f1614..3441161 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.exceptions import OsmHttpException
 import yaml
 import json
 import logging
@@ -40,16 +39,18 @@ class Vim(object):
                                         self._apiVersion, self._apiResource)
 
     # 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
         apiUrlStatus = '{}{}{}'.format(self._apiName, self._apiVersion, '/vim_accounts')
         # Wait for status for VIM instance creation/deletion
+        if isinstance(wait_time, bool):
+            wait_time = WaitForStatus.TIMEOUT_VIM_OPERATION
         WaitForStatus.wait_for_status(
             'VIM',
             str(id),
-            WaitForStatus.TIMEOUT_VIM_OPERATION,
+            wait_time,
             apiUrlStatus,
             self._http.get2_cmd,
             deleteFlag=deleteFlag)
@@ -99,11 +100,11 @@ class Vim(object):
         if resp:
             resp = json.loads(resp)
         if not resp or 'id' not in resp:
-            raise OsmHttpException('unexpected response from server - {}'.format(
+            raise ClientException('unexpected response from server - {}'.format(
                                   resp))
         if wait:
             # Wait for status for VIM instance creation
-            self._wait(resp.get('id'))
+            self._wait(resp.get('id'), wait)
         print(resp['id'])
         #else:
         #    msg = ""
@@ -127,12 +128,16 @@ class Vim(object):
                 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())
+        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']),
@@ -145,9 +150,9 @@ class Vim(object):
             # 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
+            self._wait(wait_id, wait)
+        else:
+            pass
         #else:
         #    msg = ""
         #    if resp:
@@ -198,19 +203,19 @@ class Vim(object):
                     resp = json.loads(resp)
                     wait_id = resp.get('id')
                 # Wait for status for VIM account deletion
-                self._wait(wait_id, deleteFlag=True)
+                self._wait(wait_id, wait, deleteFlag=True)
             else:
                 print('Deletion in progress')
         elif http_code == 204:
             print('Deleted')
         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):
         """Returns a list of VIM accounts
@@ -237,12 +242,13 @@ class Vim(object):
         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))