Merge changes Iff7ec8a9,I661d8abd
[osm/osmclient.git] / osmclient / sol005 / nsi.py
index b582ae1..4671441 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
@@ -41,16 +40,18 @@ class Nsi(object):
                                         self._apiVersion, self._apiResource)
 
     # NSI '--wait' option
                                         self._apiVersion, self._apiResource)
 
     # NSI '--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, '/nsi_lcm_op_occs')
         # Wait for status for NSI instance creation/update/deletion
         self._logger.debug("")
         self._client.get_token()
         # Endpoint to get operation status
         apiUrlStatus = '{}{}{}'.format(self._apiName, self._apiVersion, '/nsi_lcm_op_occs')
         # Wait for status for NSI instance creation/update/deletion
+        if isinstance(wait_time, bool):
+            wait_time = WaitForStatus.TIMEOUT_NSI_OPERATION
         WaitForStatus.wait_for_status(
             'NSI',
             str(id),
         WaitForStatus.wait_for_status(
             'NSI',
             str(id),
-            WaitForStatus.TIMEOUT_NSI_OPERATION,
+            wait_time,
             apiUrlStatus,
             self._http.get2_cmd,
             deleteFlag=deleteFlag)
             apiUrlStatus,
             self._http.get2_cmd,
             deleteFlag=deleteFlag)
@@ -92,11 +93,14 @@ class Nsi(object):
                 if name == nsi['name']:
                     nsi_id = nsi['_id']
                     break
                 if name == nsi['name']:
                     nsi_id = nsi['_id']
                     break
-        _, resp = self._http.get2_cmd('{}/{}'.format(self._apiBase, nsi_id))
-        #resp = self._http.get_cmd('{}/{}/nsd_content'.format(self._apiBase, nsi_id))
-        #print(yaml.safe_dump(resp))
-        if resp:
-            return json.loads(resp)
+        try:
+            _, resp = self._http.get2_cmd('{}/{}'.format(self._apiBase, nsi_id))
+            #resp = self._http.get_cmd('{}/{}/nsd_content'.format(self._apiBase, nsi_id))
+            #print(yaml.safe_dump(resp))
+            if resp:
+                return json.loads(resp)
+        except NotFound:
+            raise NotFound("nsi '{}' not found".format(name))
         raise NotFound("nsi {} not found".format(name))
 
     def delete(self, name, force=False, wait=False):
         raise NotFound("nsi {} not found".format(name))
 
     def delete(self, name, force=False, wait=False):
@@ -114,19 +118,19 @@ class Nsi(object):
                 resp = json.loads(resp)
                 # Wait for status for NSI instance deletion
                 # For the 'delete' operation, '_id' is used
                 resp = json.loads(resp)
                 # Wait for status for NSI instance deletion
                 # For the 'delete' operation, '_id' is used
-                self._wait(resp.get('_id'), deleteFlag=True)
+                self._wait(resp.get('_id'), wait, deleteFlag=True)
             else:
                 print('Deletion in progress')
         elif http_code == 204:
             print('Deleted')
         else:
             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 nsi {} - {}".format(name, msg))
+            msg = resp or ""
+            if resp:
+                try:
+                    msg = json.loads(resp)
+                except ValueError:
+                    msg = resp
+            raise ClientException("failed to delete nsi {} - {}".format(name, msg))
 
     def create(self, nst_name, nsi_name, account, config=None,
                ssh_keys=None, description='default description',
 
     def create(self, nst_name, nsi_name, account, config=None,
                ssh_keys=None, description='default description',
@@ -238,7 +242,7 @@ class Nsi(object):
                                   resp))
             if wait:
                 # Wait for status for NSI instance creation
                                   resp))
             if wait:
                 # Wait for status for NSI instance creation
-                self._wait(resp.get('nsilcmop_id'))
+                self._wait(resp.get('nsilcmop_id'), wait)
             print(resp['id'])
             #else:
             #    msg = ""
             print(resp['id'])
             #else:
             #    msg = ""
@@ -253,7 +257,7 @@ class Nsi(object):
                     nsi_name,
                     nst_name,
                     str(exc))
                     nsi_name,
                     nst_name,
                     str(exc))
-            raise OsmHttpException(message)
+            raise ClientException(message)
 
     def list_op(self, name, filter=None):
         """Returns the list of operations of a NSI
 
     def list_op(self, name, filter=None):
         """Returns the list of operations of a NSI
@@ -291,7 +295,7 @@ class Nsi(object):
             message="failed to get operation list of NSI {}:\nerror:\n{}".format(
                     name,
                     str(exc))
             message="failed to get operation list of NSI {}:\nerror:\n{}".format(
                     name,
                     str(exc))
-            raise OsmHttpException(message)
+            raise ClientException(message)
 
     def get_op(self, operationId):
         """Returns the status of an operation
 
     def get_op(self, operationId):
         """Returns the status of an operation
@@ -324,7 +328,7 @@ class Nsi(object):
             message="failed to get status of operation {}:\nerror:\n{}".format(
                     operationId,
                     str(exc))
             message="failed to get status of operation {}:\nerror:\n{}".format(
                     operationId,
                     str(exc))
-            raise OsmHttpException(message)
+            raise ClientException(message)
 
     def exec_op(self, name, op_name, op_data=None):
         """Executes an operation on a NSI
 
     def exec_op(self, name, op_name, op_data=None):
         """Executes an operation on a NSI
@@ -360,5 +364,5 @@ class Nsi(object):
             message="failed to exec operation {}:\nerror:\n{}".format(
                     name,
                     str(exc))
             message="failed to exec operation {}:\nerror:\n{}".format(
                     name,
                     str(exc))
-            raise OsmHttpException(message)
+            raise ClientException(message)