Capture end organize exceptions. Fix NotFound 96/8496/3
authortierno <alfonso.tiernosepulveda@telefonica.com>
Tue, 21 Jan 2020 09:27:09 +0000 (09:27 +0000)
committertierno <alfonso.tiernosepulveda@telefonica.com>
Tue, 21 Jan 2020 09:27:15 +0000 (09:27 +0000)
Change-Id: I6fb4e88e16957725c595bd3dcabc6f74c04537ef
Signed-off-by: tierno <alfonso.tiernosepulveda@telefonica.com>
20 files changed:
osmclient/common/exceptions.py
osmclient/scripts/osm.py
osmclient/sol005/client.py
osmclient/sol005/http.py
osmclient/sol005/k8scluster.py
osmclient/sol005/ns.py
osmclient/sol005/nsd.py
osmclient/sol005/nsi.py
osmclient/sol005/nst.py
osmclient/sol005/package.py
osmclient/sol005/pdud.py
osmclient/sol005/project.py
osmclient/sol005/repo.py
osmclient/sol005/role.py
osmclient/sol005/sdncontroller.py
osmclient/sol005/user.py
osmclient/sol005/vim.py
osmclient/sol005/vnf.py
osmclient/sol005/vnfd.py
osmclient/sol005/wim.py

index 92ed0f6..12de003 100644 (file)
@@ -19,8 +19,9 @@ class ClientException(Exception):
     pass
 
 
-class NotFound(ClientException):
+class OsmHttpException(ClientException):
     pass
 
-class OsmHttpException(ClientException):
+
+class NotFound(OsmHttpException):
     pass
index af8cab9..f61373e 100755 (executable)
@@ -3805,13 +3805,17 @@ def package_build(ctx,
 def cli():
     try:
         cli_osm()
+        exit(0)
     except pycurl.error as exc:
         print(exc)
         print('Maybe "--hostname" option or OSM_HOSTNAME environment variable needs to be specified')
-        exit(1)
     except ClientException as exc:
         print("ERROR: {}".format(exc))
-        exit(1)
+    except (FileNotFoundError, PermissionError) as exc:
+        print("Cannot open file: {}".format(exc))
+    except yaml.YAMLError as exc:
+        print("Invalid YAML format: {}".format(exc))
+    exit(1)
     # TODO capture other controlled exceptions here
     # TODO remove the ClientException captures from all places, unless they do something different
 
index 3f397f7..3ceb1b3 100644 (file)
@@ -123,4 +123,3 @@ class Client(object):
         _, resp = self._http_client.get2_cmd(endpoint="/version")
         resp = json.loads(resp)
         return "{} {}".format(resp.get("version"), resp.get("date"))
-
index 9bdf94e..3d21465 100644 (file)
@@ -20,7 +20,7 @@ import json
 import logging
 import copy
 from osmclient.common import http
-from osmclient.common.exceptions import OsmHttpException
+from osmclient.common.exceptions import OsmHttpException, NotFound
 
 
 class Http(http.Http):
@@ -58,8 +58,9 @@ class Http(http.Http):
         self.check_http_response(http_code, data)
         # TODO 202 accepted should be returned somehow
         if data.getvalue():
-            self._logger.verbose("Response DATA: {}".format(json.loads(data.getvalue().decode())))
-            return http_code, data.getvalue().decode()
+            data_text = data.getvalue().decode()
+            self._logger.verbose("Response DATA: {}".format(data_text))
+            return http_code, data_text
         else:
             return http_code, None
 
@@ -110,8 +111,9 @@ class Http(http.Http):
         curl_cmd.close()
         self.check_http_response(http_code, data)
         if data.getvalue():
-            self._logger.verbose("Response DATA: {}".format(json.loads(data.getvalue().decode())))
-            return http_code, data.getvalue().decode()
+            data_text = data.getvalue().decode()
+            self._logger.verbose("Response DATA: {}".format(data_text))
+            return http_code, data_text
         else:
             return http_code, None
 
@@ -155,13 +157,20 @@ class Http(http.Http):
         curl_cmd.close()
         self.check_http_response(http_code, data)
         if data.getvalue():
-            self._logger.debug("Response DATA: {}".format(json.loads(data.getvalue().decode())))
-            return http_code, data.getvalue().decode()
+            data_text = data.getvalue().decode()
+            self._logger.verbose("Response DATA: {}".format(data_text))
+            return http_code, data_text
         return http_code, None
 
     def check_http_response(self, http_code, data):
         if http_code >= 300:
             resp = ""
             if data.getvalue():
-                resp=": " + data.getvalue().decode()
+                data_text = data.getvalue().decode()
+                self._logger.verbose("Response {} DATA: {}".format(http_code, data_text))
+                resp = ": " + data_text
+            else:
+                self._logger.verbose("Response {}".format(http_code))
+            if http_code == 404:
+                raise NotFound("Error {}{}".format(http_code, resp))
             raise OsmHttpException("Error {}{}".format(http_code, resp))
index f001dee..0cdb089 100644 (file)
@@ -18,7 +18,7 @@ OSM K8s cluster API handling
 
 from osmclient.common import utils
 from osmclient.common.exceptions import NotFound
-from osmclient.common.exceptions import OsmHttpException
+from osmclient.common.exceptions import ClientException
 import json
 
 class K8scluster(object):
@@ -48,10 +48,8 @@ class K8scluster(object):
         #if http_code in (200, 201, 202, 204):
         if resp:
             resp = json.loads(resp)
-            print(resp['id'])
         if not resp or 'id' not in resp:
-            raise OsmHttpException('unexpected response from server - {}'.format(
-                                      resp))
+            raise ClientException('unexpected response from server - {}'.format(resp))
         print(resp['id'])
         #else:
         #    msg = ""
@@ -70,7 +68,7 @@ class K8scluster(object):
         # print 'HTTP CODE: {}'.format(http_code)
         # print 'RESP: {}'.format(resp)
         #if http_code in (200, 201, 202, 204):
-        #pass
+        #    pass
         #else:
         #    msg = ""
         #    if resp:
@@ -105,13 +103,13 @@ class K8scluster(object):
         elif http_code == 204:
             print('Deleted')
         else:
-            msg = ""
-            if resp:
-                try:
-                    msg = json.loads(resp)
-                except ValueError:
-                    msg = resp
-            raise OsmHttpException("failed to delete K8s cluster {} - {}".format(name, msg))
+            msg = resp or ""
+            if resp:
+                try:
+                    msg = json.loads(resp)
+                except ValueError:
+                    msg = resp
+            raise ClientException("failed to delete K8s cluster {} - {}".format(name, msg))
 
     def list(self, filter=None):
         """Returns a list of K8s clusters
@@ -132,11 +130,13 @@ class K8scluster(object):
         cluster_id = name
         if not utils.validate_uuid4(name):
             cluster_id = self.get_id(name)
-        _, resp = self._http.get2_cmd('{}/{}'.format(self._apiBase,cluster_id))
-#        if not resp or '_id' not in resp:
-#            raise ClientException('failed to get K8s cluster info: '.format(resp))
-#        else:
-        if resp:
-            return json.loads(resp) 
-        raise NotFound("K8s cluster {} not found".format(name))
+        try:
+            _, resp = self._http.get2_cmd('{}/{}'.format(self._apiBase,cluster_id))
+            if resp:
+                resp = json.loads(resp)
+            if not resp or '_id' not in resp:
+                raise ClientException('failed to get K8s cluster info: '.format(resp))
+            return resp
+        except NotFound:
+            raise NotFound("K8s cluster {} not found".format(name))
 
index c8e2a77..f0877fc 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
@@ -80,7 +79,7 @@ class Ns(object):
             for ns in self.list():
                 if name == ns['name']:
                     return ns
-        raise NotFound("ns {} not found".format(name))
+        raise NotFound("ns '{}' not found".format(name))
 
     def get_individual(self, name):
         self._logger.debug("")
@@ -91,12 +90,15 @@ class Ns(object):
                 if name == ns['name']:
                     ns_id = ns['_id']
                     break
-        _, resp = self._http.get2_cmd('{}/{}'.format(self._apiBase, ns_id))
-        #resp = self._http.get_cmd('{}/{}/nsd_content'.format(self._apiBase, ns_id))
-        #print(yaml.safe_dump(resp))
-        if resp:
-            return json.loads(resp)
-        raise NotFound("ns {} not found".format(name))
+        try:
+            _, resp = self._http.get2_cmd('{}/{}'.format(self._apiBase, ns_id))
+            #resp = self._http.get_cmd('{}/{}/nsd_content'.format(self._apiBase, ns_id))
+            #print(yaml.safe_dump(resp))
+            if resp:
+                return json.loads(resp)
+        except NotFound:
+            raise NotFound("ns '{}' not found".format(name))
+        raise NotFound("ns '{}' not found".format(name))
 
     def delete(self, name, force=False, wait=False):
         self._logger.debug("")
@@ -118,13 +120,13 @@ class Ns(object):
         elif http_code == 204:
             print('Deleted')
         else:
-            msg = ""
-            if resp:
-                try:
-                    msg = json.loads(resp)
-                except ValueError:
-                    msg = resp
-            raise OsmHttpException("failed to delete ns {} - {}".format(name, msg))
+            msg = resp or ""
+            if resp:
+                try:
+                    msg = json.loads(resp)
+                except ValueError:
+                    msg = resp
+            raise ClientException("failed to delete ns {} - {}".format(name, msg))
 
     def create(self, nsd_name, nsr_name, account, config=None,
                ssh_keys=None, description='default description',
@@ -140,7 +142,6 @@ class Ns(object):
             self._logger.debug("")
             if vim_account_id.get(vim_account):
                 return vim_account_id[vim_account]
-
             vim = self._client.vim.get(vim_account)
             if vim is None:
                 raise NotFound("cannot find vim account '{}'".format(vim_account))
@@ -149,11 +150,11 @@ class Ns(object):
 
         def get_wim_account_id(wim_account):
             self._logger.debug("")
+            # wim_account can be False (boolean) to indicate not use wim account
             if not isinstance(wim_account, str):
                 return wim_account
             if wim_account_id.get(wim_account):
                 return wim_account_id[wim_account]
-
             wim = self._client.wim.get(wim_account)
             if wim is None:
                 raise NotFound("cannot find wim account '{}'".format(wim_account))
@@ -239,6 +240,7 @@ class Ns(object):
             if wait:
                 # Wait for status for NS instance creation
                 self._wait(resp.get('nslcmop_id'))
+            print(resp['id'])
             return resp['id']
             #else:
             #    msg = ""
@@ -253,7 +255,7 @@ class Ns(object):
                     nsr_name,
                     nsd_name,
                     str(exc))
-            raise OsmHttpException(message)
+            raise ClientException(message)
 
     def list_op(self, name, filter=None):
         """Returns the list of operations of a NS
@@ -272,26 +274,26 @@ class Ns(object):
                                                        filter_string) )
             #print('HTTP CODE: {}'.format(http_code))
             #print('RESP: {}'.format(resp))
-            #if http_code == 200:
-            if resp:
-                resp = json.loads(resp)
-                return resp
+            if http_code == 200:
+                if resp:
+                    resp = json.loads(resp)
+                    return resp
+                else:
+                    raise ClientException('unexpected response from server')
             else:
-                raise ClientException('unexpected response from server')
-            #else:
-            #    msg = ""
+                msg = resp or ""
             #    if resp:
             #        try:
             #            resp = json.loads(resp)
             #            msg = resp['detail']
             #        except ValueError:
             #            msg = resp
-            #    raise ClientException(msg)
+                raise ClientException(msg)
         except ClientException as exc:
             message="failed to get operation list of NS {}:\nerror:\n{}".format(
                     name,
                     str(exc))
-            raise OsmHttpException(message)
+            raise ClientException(message)
 
     def get_op(self, operationId):
         """Returns the status of an operation
@@ -305,26 +307,26 @@ class Ns(object):
             http_code, resp = self._http.get2_cmd('{}/{}'.format(self._apiBase, operationId))
             #print('HTTP CODE: {}'.format(http_code))
             #print('RESP: {}'.format(resp))
-            #if http_code == 200:
-            if resp:
-                resp = json.loads(resp)
-                return resp
+            if http_code == 200:
+                if resp:
+                    resp = json.loads(resp)
+                    return resp
+                else:
+                    raise ClientException('unexpected response from server')
             else:
-                raise ClientException('unexpected response from server')
-            #else:
-            #    msg = ""
+                msg = resp or ""
             #    if resp:
             #        try:
             #            resp = json.loads(resp)
             #            msg = resp['detail']
             #        except ValueError:
             #            msg = resp
-            #    raise ClientException(msg)
+                raise ClientException(msg)
         except ClientException as 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, wait=False, ):
         """Executes an operation on a NS
@@ -365,7 +367,7 @@ class Ns(object):
             message="failed to exec operation {}:\nerror:\n{}".format(
                     name,
                     str(exc))
-            raise OsmHttpException(message)
+            raise ClientException(message)
 
     def scale_vnf(self, ns_name, vnf_name, scaling_group, scale_in, scale_out, wait=False):
         """Scales a VNF by adding/removing VDUs
@@ -397,14 +399,14 @@ class Ns(object):
         data = {}
         data["create_alarm_request"] = {}
         data["create_alarm_request"]["alarm_create_request"] = alarm
-        #try:
-        http_code, resp = self._http.post_cmd(endpoint='/test/message/alarm_request',
-                                   postfields_dict=data)
+        try:
+            http_code, resp = self._http.post_cmd(endpoint='/test/message/alarm_request',
+                                       postfields_dict=data)
             #print('HTTP CODE: {}'.format(http_code))
             #print('RESP: {}'.format(resp))
-            #if http_code in (200, 201, 202, 204):
-            #resp = json.loads(resp)
-        print('Alarm created')
+            # if http_code in (200, 201, 202, 204):
+            #     resp = json.loads(resp)
+            print('Alarm created')
             #else:
             #    msg = ""
             #    if resp:
@@ -414,11 +416,11 @@ class Ns(object):
             #            msg = resp
             #    raise ClientException('error: code: {}, resp: {}'.format(
             #                          http_code, msg))
-        #except ClientException as exc:
-        #    message="failed to create alarm: alarm {}\n{}".format(
-        #            alarm,
-        #            str(exc))
-        #    raise ClientException(message)
+        except ClientException as exc:
+            message="failed to create alarm: alarm {}\n{}".format(
+                    alarm,
+                    str(exc))
+            raise ClientException(message)
 
     def delete_alarm(self, name):
         self._logger.debug("")
@@ -427,14 +429,14 @@ class Ns(object):
         data["delete_alarm_request"] = {}
         data["delete_alarm_request"]["alarm_delete_request"] = {}
         data["delete_alarm_request"]["alarm_delete_request"]["alarm_uuid"] = name
-        #try:
-        http_code, resp = self._http.post_cmd(endpoint='/test/message/alarm_request',
-                                   postfields_dict=data)
+        try:
+            http_code, resp = self._http.post_cmd(endpoint='/test/message/alarm_request',
+                                       postfields_dict=data)
             #print('HTTP CODE: {}'.format(http_code))
             #print('RESP: {}'.format(resp))
-            #if http_code in (200, 201, 202, 204):
-            #resp = json.loads(resp)
-        print('Alarm deleted')
+            # if http_code in (200, 201, 202, 204):
+            #    resp = json.loads(resp)
+            print('Alarm deleted')
             #else:
             #    msg = ""
             #    if resp:
@@ -444,25 +446,25 @@ class Ns(object):
             #            msg = resp
             #    raise ClientException('error: code: {}, resp: {}'.format(
             #                          http_code, msg))
-        #except ClientException as exc:
-        #    message="failed to delete alarm: alarm {}\n{}".format(
-        #            name,
-        #            str(exc))
-        #    raise ClientException(message)
+        except ClientException as exc:
+            message="failed to delete alarm: alarm {}\n{}".format(
+                    name,
+                    str(exc))
+            raise ClientException(message)
 
     def export_metric(self, metric):
         self._logger.debug("")
         self._client.get_token()
         data = {}
         data["read_metric_data_request"] = metric
-        #try:
-        http_code, resp = self._http.post_cmd(endpoint='/test/message/metric_request',
-                                   postfields_dict=data)
+        try:
+            http_code, resp = self._http.post_cmd(endpoint='/test/message/metric_request',
+                                       postfields_dict=data)
             #print('HTTP CODE: {}'.format(http_code))
             #print('RESP: {}'.format(resp))
-            #if http_code in (200, 201, 202, 204):
-            #resp = json.loads(resp)
-        return 'Metric exported'
+            # if http_code in (200, 201, 202, 204):
+            #    resp = json.loads(resp)
+            return 'Metric exported'
             #else:
             #    msg = ""
             #    if resp:
@@ -472,11 +474,11 @@ class Ns(object):
             #            msg = resp
             #    raise ClientException('error: code: {}, resp: {}'.format(
             #                          http_code, msg))
-        #except ClientException as exc:
-        #    message="failed to export metric: metric {}\n{}".format(
-        #            metric,
-        #            str(exc))
-        #    raise ClientException(message)
+        except ClientException as exc:
+            message="failed to export metric: metric {}\n{}".format(
+                    metric,
+                    str(exc))
+            raise ClientException(message)
 
     def get_field(self, ns_name, field):
         self._logger.debug("")
index a2a1902..5979bc0 100644 (file)
@@ -20,7 +20,6 @@ OSM nsd API handling
 
 from osmclient.common.exceptions import NotFound
 from osmclient.common.exceptions import ClientException
-from osmclient.common.exceptions import OsmHttpException
 from osmclient.common import utils
 import json
 import magic
@@ -70,14 +69,17 @@ class Nsd(object):
     def get_individual(self, name):
         self._logger.debug("")
         # Call to get_token not required, because will be implicitly called by get.
-        nsd = self.get(name)
-        # It is redundant, since the previous one already gets the whole nsdinfo
-        # The only difference is that a different primitive is exercised
-        _, resp = self._http.get2_cmd('{}/{}'.format(self._apiBase, nsd['_id']))
-        #print(yaml.safe_dump(resp))
-        if resp:
-            return json.loads(resp)
-        raise NotFound("nsd {} not found".format(name))
+        try:
+            nsd = self.get(name)
+            # It is redundant, since the previous one already gets the whole nsdinfo
+            # The only difference is that a different primitive is exercised
+            _, resp = self._http.get2_cmd('{}/{}'.format(self._apiBase, nsd['_id']))
+            #print(yaml.safe_dump(resp))
+            if resp:
+                return json.loads(resp)
+        except NotFound:
+            raise NotFound("nsd '{}' not found".format(name))
+        raise NotFound("nsd '{}' not found".format(name))
 
     def get_thing(self, name, thing, filename):
         self._logger.debug("")
@@ -92,14 +94,14 @@ class Nsd(object):
         if resp:
             #store in a file
             return json.loads(resp)
-        #else:
-        #    msg = ""
+        else:
+            msg = resp or ""
         #    if resp:
         #        try:
         #            msg = json.loads(resp)
         #        except ValueError:
         #            msg = resp
-        #    raise ClientException("failed to get {} from {} - {}".format(thing, name, msg))
+            raise ClientException("failed to get {} from {} - {}".format(thing, name, msg))
 
     def get_descriptor(self, name, filename):
         self._logger.debug("")
@@ -128,13 +130,13 @@ class Nsd(object):
         elif http_code == 204:
             print('Deleted')
         else:
-            msg = ""
-            if resp:
-                try:
-                    msg = json.loads(resp)
-                except ValueError:
-                    msg = resp
-            raise OsmHttpException("failed to delete nsd {} - {}".format(name, msg))
+            msg = resp or ""
+            if resp:
+                try:
+                    msg = json.loads(resp)
+                except ValueError:
+                    msg = resp
+            raise ClientException("failed to delete nsd {} - {}".format(name, msg))
 
     def create(self, filename, overwrite=None, update_endpoint=None):
         self._logger.debug("")
@@ -180,7 +182,7 @@ class Nsd(object):
             if resp:
                 resp = json.loads(resp)
             if not resp or 'id' not in resp:
-                raise OsmHttpException('unexpected response from server - {}'.format(resp))
+                raise ClientException('unexpected response from server - {}'.format(resp))
             print(resp['id'])
         elif http_code == 204:
             print('Updated')
index b582ae1..4b522a8 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
@@ -92,11 +91,14 @@ class Nsi(object):
                 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):
@@ -120,13 +122,13 @@ class Nsi(object):
         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',
@@ -253,7 +255,7 @@ class Nsi(object):
                     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
@@ -291,7 +293,7 @@ class Nsi(object):
             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
@@ -324,7 +326,7 @@ class Nsi(object):
             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
@@ -360,5 +362,5 @@ class Nsi(object):
             message="failed to exec operation {}:\nerror:\n{}".format(
                     name,
                     str(exc))
-            raise OsmHttpException(message)
+            raise ClientException(message)
 
index f6a03f1..3b15e96 100644 (file)
@@ -20,7 +20,6 @@ OSM NST (Network Slice Template) API handling
 
 from osmclient.common.exceptions import NotFound
 from osmclient.common.exceptions import ClientException
-from osmclient.common.exceptions import OsmHttpException
 from osmclient.common import utils
 import json
 import magic
@@ -70,18 +69,24 @@ class Nst(object):
         nst = self.get(name)
         # It is redundant, since the previous one already gets the whole nstinfo
         # The only difference is that a different primitive is exercised
-        _, resp = self._http.get2_cmd('{}/{}'.format(self._apiBase, nst['_id']))
-        #print(yaml.safe_dump(resp))
-        if resp:
-            return json.loads(resp)
-        raise NotFound("nst {} not found".format(name))
+        try:
+            _, resp = self._http.get2_cmd('{}/{}'.format(self._apiBase, nst['_id']))
+            #print(yaml.safe_dump(resp))
+            if resp:
+                return json.loads(resp)
+        except NotFound:
+            raise NotFound("nst '{}' not found".format(name))
+        raise NotFound("nst '{}' not found".format(name))
 
     def get_thing(self, name, thing, filename):
         self._logger.debug("")
         nst = self.get(name)
         headers = self._client._headers
         headers['Accept'] = 'application/binary'
-        http_code, resp = self._http.get2_cmd('{}/{}/{}'.format(self._apiBase, nst['_id'], thing))
+        try:
+            http_code, resp = self._http.get2_cmd('{}/{}/{}'.format(self._apiBase, nst['_id'], thing))
+        except NotFound:
+            raise NotFound("nst '{} 'not found".format(name))
         #print('HTTP CODE: {}'.format(http_code))
         #print('RESP: {}'.format(resp))
         #if http_code in (200, 201, 202, 204):
@@ -124,13 +129,13 @@ class Nst(object):
         elif http_code == 204:
             print('Deleted')
         else:
-            msg = ""
-            if resp:
-                try:
-                    resp = json.loads(resp)
-                except ValueError:
-                    msg = resp
-            raise OsmHttpException("failed to delete nst {} - {}".format(name, msg))
+            msg = resp or ""
+            if resp:
+                try:
+                    resp = json.loads(resp)
+                except ValueError:
+                    msg = resp
+            raise ClientException("failed to delete nst {} - {}".format(name, msg))
 
     def create(self, filename, overwrite=None, update_endpoint=None):
         self._logger.debug("")
@@ -175,7 +180,7 @@ class Nst(object):
         if resp:
             resp = json.loads(resp)
         if not resp or 'id' not in resp:
-            raise OsmHttpException('unexpected response from server - {}'.format(resp))
+            raise ClientException('unexpected response from server - {}'.format(resp))
         print(resp['id'])
         # else:
         #     msg = "Error {}".format(http_code)
index 27b3ad4..1ca3864 100644 (file)
@@ -20,7 +20,6 @@ OSM package API handling
 
 #from os import stat
 #from os.path import basename
-from osmclient.common.exceptions import OsmHttpException
 from osmclient.common.exceptions import ClientException
 from osmclient.common.exceptions import NotFound
 from osmclient.common import utils
@@ -105,7 +104,7 @@ class Package(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))
         print(resp['id'])
         # else:
index 9b15ca7..3abb78c 100644 (file)
@@ -19,7 +19,7 @@ OSM pdud API handling
 """
 
 from osmclient.common.exceptions import NotFound
-from osmclient.common.exceptions import OsmHttpException
+from osmclient.common.exceptions import ClientException
 from osmclient.common import utils
 import json
 import logging
@@ -66,11 +66,14 @@ class Pdu(object):
         pdud = self.get(name)
         # It is redundant, since the previous one already gets the whole pdudInfo
         # The only difference is that a different primitive is exercised
-        _, resp = self._http.get2_cmd('{}/{}'.format(self._apiBase, pdud['_id']))
+        try:
+            _, resp = self._http.get2_cmd('{}/{}'.format(self._apiBase, pdud['_id']))
+        except NotFound:
+            raise NotFound("pdu '{}' not found".format(name))
         #print(yaml.safe_dump(resp))
         if resp:
             return json.loads(resp)
-        raise NotFound("pdu {} not found".format(name))
+        raise NotFound("pdu '{}' not found".format(name))
 
     def delete(self, name, force=False):
         self._logger.debug("")
@@ -87,13 +90,13 @@ class Pdu(object):
         elif http_code == 204:
             print('Deleted')
         else:
-            msg = ""
-            if resp:
-                try:
-                    msg = json.loads(resp)
-                except ValueError:
-                    msg = resp
-            raise OsmHttpException("failed to delete pdu {} - {}".format(name, msg))
+            msg = resp or ""
+            if resp:
+                try:
+                    msg = json.loads(resp)
+                except ValueError:
+                    msg = resp
+            raise ClientException("failed to delete pdu {} - {}".format(name, msg))
 
     def create(self, pdu, update_endpoint=None):
         self._logger.debug("")
@@ -115,7 +118,7 @@ class Pdu(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))
         print(resp['id'])
         #else:
index 39d2453..dc2e9a5 100644 (file)
@@ -20,8 +20,8 @@ OSM project mgmt API
 """
 
 from osmclient.common import utils
+from osmclient.common.exceptions import ClientException
 from osmclient.common.exceptions import NotFound
-from osmclient.common.exceptions import OsmHttpException
 import json
 import logging
 
@@ -50,7 +50,7 @@ class Project(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))
         print(resp['id'])
         #else:
@@ -76,7 +76,7 @@ class Project(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))
             print(resp['id'])
         elif http_code == 204:
@@ -110,13 +110,13 @@ class Project(object):
         elif resp and 'result' in resp:
             print('Deleted')
         else:
-            msg = ""
-            if resp:
-                try:
-                    msg = json.loads(resp)
-                except ValueError:
-                    msg = resp
-            raise OsmHttpException("failed to delete project {} - {}".format(name, msg))
+            msg = resp or ""
+            if resp:
+                try:
+                    msg = json.loads(resp)
+                except ValueError:
+                    msg = resp
+            raise ClientException("failed to delete project {} - {}".format(name, msg))
 
     def list(self, filter=None):
         """Returns the list of OSM projects
index 1f487de..7a31397 100644 (file)
@@ -17,7 +17,6 @@ OSM Repo API handling
 """
 
 from osmclient.common import utils
-from osmclient.common.exceptions import OsmHttpException
 from osmclient.common.exceptions import ClientException
 from osmclient.common.exceptions import NotFound
 import json
@@ -42,8 +41,8 @@ class Repo(object):
         if resp:
             resp = json.loads(resp)
         if not resp or 'id' not in resp:
-            raise OsmHttpException('unexpected response from server - {}'.format(
-                                  resp))
+            raise ClientException('unexpected response from server - {}'.format(
+                resp))
         print(resp['id'])
         #else:
         #    msg = ""
@@ -98,12 +97,12 @@ class Repo(object):
         elif http_code == 204:
             print('Deleted')
         else:
-            msg = ""
-            if resp:
-                try:
-                    msg = json.loads(resp)
-                except ValueError:
-                    msg = resp
+            msg = resp or ""
+            if resp:
+                try:
+                    msg = json.loads(resp)
+                except ValueError:
+                    msg = resp
             raise ClientException("failed to delete repo {} - {}".format(name, msg))
 
     def list(self, filter=None):
@@ -125,11 +124,13 @@ class Repo(object):
         repo_id = name
         if not utils.validate_uuid4(name):
             repo_id = self.get_id(name)
-        _, resp = self._http.get2_cmd('{}/{}'.format(self._apiBase,repo_id))
-#        if not resp or '_id' not in resp:
-#            raise ClientException('failed to get repo info: '.format(resp))
-#        else:
-        if resp:
-            return json.loads(resp)
-        raise NotFound("Repo {} not found".format(name))
+        try:
+            _, resp = self._http.get2_cmd('{}/{}'.format(self._apiBase,repo_id))
+            if resp:
+                resp = json.loads(resp)
+            if not resp or '_id' not in resp:
+                raise ClientException('failed to get repo info: '.format(resp))
+            return resp
+        except NotFound:
+            raise NotFound("Repo {} not found".format(name))
 
index 733e00f..c80e50e 100644 (file)
@@ -23,7 +23,6 @@ OSM role mgmt API
 from osmclient.common import utils
 from osmclient.common.exceptions import ClientException
 from osmclient.common.exceptions import NotFound
-from osmclient.common.exceptions import OsmHttpException
 import json
 import yaml
 import logging
@@ -73,8 +72,8 @@ class Role(object):
         if resp:
             resp = json.loads(resp)
         if not resp or 'id' not in resp:
-            raise OsmHttpException('Unexpected response from server - {}'.format(
-                                  resp))
+            raise ClientException('Unexpected response from server - {}'.format(
+                resp))
         print(resp['id'])
         #else:
         #    msg = ""
@@ -156,8 +155,8 @@ class Role(object):
             if resp:
                 resp = json.loads(resp)
             if not resp or 'id' not in resp:
-                raise OsmHttpException('Unexpected response from server - {}'.format(
-                                      resp))
+                raise ClientException('Unexpected response from server - {}'.format(
+                    resp))
             print(resp['id'])
         elif http_code == 204:
             print("Updated")
@@ -195,13 +194,13 @@ class Role(object):
         elif resp and 'result' in resp:
             print('Deleted')
         else:
-            msg = ""
-            if resp:
-                try:
-                    msg = json.loads(resp)
-                except ValueError:
-                    msg = resp
-            raise OsmHttpException("Failed to delete role {} - {}".format(name, msg))
+            msg = resp or ""
+            if resp:
+                try:
+                    msg = json.loads(resp)
+                except ValueError:
+                    msg = resp
+            raise ClientException("Failed to delete role {} - {}".format(name, msg))
 
     def list(self, filter=None):
         """
index 2a4d409..35ad9ee 100644 (file)
@@ -20,8 +20,8 @@ OSM SDN controller API handling
 
 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 json
 import logging
 
@@ -75,7 +75,7 @@ class SdnController(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 SDNC instance creation
@@ -106,8 +106,8 @@ class SdnController(object):
             wait_id = sdnc_id_for_wait
             # Wait for status for VI instance update
             self._wait(wait_id)
-        else:
-            pass
+        else:
+            pass
         #else:
         #    msg = ""
         #    if resp:
@@ -140,13 +140,13 @@ class SdnController(object):
         elif resp and 'result' in resp:
             print('Deleted')
         else:
-            msg = ""
-            if resp:
-                try:
-                    msg = json.loads(resp)
-                except ValueError:
-                    msg = resp
-            raise OsmHttpException("failed to delete SDN controller {} - {}".format(name, msg))
+            msg = resp or ""
+            if resp:
+                try:
+                    msg = json.loads(resp)
+                except ValueError:
+                    msg = resp
+            raise ClientException("failed to delete SDN controller {} - {}".format(name, msg))
 
     def list(self, filter=None):
         """Returns a list of SDN controllers
index 276c450..fac26cd 100644 (file)
@@ -22,7 +22,6 @@ OSM user mgmt API
 from osmclient.common import utils
 from osmclient.common.exceptions import ClientException
 from osmclient.common.exceptions import NotFound
-from osmclient.common.exceptions import OsmHttpException
 import json
 import logging
 
@@ -73,8 +72,8 @@ class User(object):
         if resp:
             resp = json.loads(resp)
         if not resp or 'id' not in resp:
-            raise OsmHttpException('unexpected response from server - {}'.format(
-                                  resp))
+            raise ClientException('unexpected response from server - {}'.format(
+                resp))
         print(resp['id'])
         #else:
         #    msg = ""
@@ -153,7 +152,7 @@ class User(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))
             print(resp['id'])
         elif http_code == 204:
@@ -187,13 +186,13 @@ class User(object):
         elif resp and 'result' in resp:
             print('Deleted')
         else:
-            msg = ""
-            if resp:
-                try:
-                    msg = json.loads(resp)
-                except ValueError:
-                    msg = resp
-            raise OsmHttpException("failed to delete user {} - {}".format(name, msg))
+            msg = resp or ""
+            if resp:
+                try:
+                    msg = json.loads(resp)
+                except ValueError:
+                    msg = resp
+            raise ClientException("failed to delete user {} - {}".format(name, msg))
 
     def list(self, filter=None):
         """Returns the list of OSM users
index b6f1614..9cd882c 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
@@ -99,7 +98,7 @@ 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
@@ -146,8 +145,8 @@ class Vim(object):
             wait_id = vim_id_for_wait
             # Wait for status for VI instance update
             self._wait(wait_id)
-        else:
-            pass
+        else:
+            pass
         #else:
         #    msg = ""
         #    if resp:
@@ -204,13 +203,13 @@ class Vim(object):
         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 +236,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))
 
index d0c87b8..0a72c1a 100644 (file)
@@ -79,9 +79,11 @@ class Vnf(object):
                 if name == vnf['name']:
                     vnf_id = vnf['_id']
                     break
-        _, resp = self._http.get2_cmd('{}/{}'.format(self._apiBase, vnf_id))
-        #print('RESP: {}'.format(resp))
-        if resp:
-            return json.loads(resp)
-        raise NotFound("vnf {} not found".format(name))
-
+        try:
+            _, resp = self._http.get2_cmd('{}/{}'.format(self._apiBase, vnf_id))
+            #print('RESP: {}'.format(resp))
+            if resp:
+                return json.loads(resp)
+        except NotFound:
+            raise NotFound("vnf '{}' not found".format(name))
+        raise NotFound("vnf '{}' not found".format(name))
index 8f0f3c6..8bf3552 100644 (file)
@@ -20,7 +20,6 @@ OSM vnfd API handling
 
 from osmclient.common.exceptions import NotFound
 from osmclient.common.exceptions import ClientException
-from osmclient.common.exceptions import OsmHttpException
 from osmclient.common import utils
 import json
 import magic
@@ -71,11 +70,14 @@ class Vnfd(object):
         vnfd = self.get(name)
         # It is redundant, since the previous one already gets the whole vnfpkginfo
         # The only difference is that a different primitive is exercised
-        _, resp = self._http.get2_cmd('{}/{}'.format(self._apiBase, vnfd['_id']))
-        #print(yaml.safe_dump(resp))
-        if resp:
-            return json.loads(resp)
-        raise NotFound("vnfd {} not found".format(name))
+        try:
+            _, resp = self._http.get2_cmd('{}/{}'.format(self._apiBase, vnfd['_id']))
+            #print(yaml.safe_dump(resp))
+            if resp:
+                return json.loads(resp)
+        except NotFound:
+            raise NotFound("vnfd '{}' not found".format(name))
+        raise NotFound("vnfd '{}' not found".format(name))
 
     def get_thing(self, name, thing, filename):
         self._logger.debug("")
@@ -126,13 +128,13 @@ class Vnfd(object):
         elif http_code == 204:
             print('Deleted')
         else:
-            msg = ""
-            if resp:
-                try:
-                    msg = json.loads(resp)
-                except ValueError:
-                    msg = resp
-            raise OsmHttpException("failed to delete vnfd {} - {}".format(name, msg))
+            msg = resp or ""
+            if resp:
+                try:
+                    msg = json.loads(resp)
+                except ValueError:
+                    msg = resp
+            raise ClientException("failed to delete vnfd {} - {}".format(name, msg))
 
     def create(self, filename, overwrite=None, update_endpoint=None):
         self._logger.debug("")
@@ -178,7 +180,7 @@ class Vnfd(object):
             if resp:
                 resp = json.loads(resp)
             if not resp or 'id' not in resp:
-                raise OsmHttpException('unexpected response from server: '.format(resp))
+                raise ClientException('unexpected response from server: '.format(resp))
             print(resp['id'])
         elif http_code == 204:
             print('Updated')
index b5a03b8..90632c6 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.exceptions import OsmHttpException
 from osmclient.common.exceptions import NotFound
 import yaml
 import json
@@ -93,7 +92,7 @@ class Wim(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 WIM instance creation
@@ -137,8 +136,8 @@ class Wim(object):
             wait_id = wim_id_for_wait
             # Wait for status for WIM instance update
             self._wait(wait_id)
-        else:
-            pass
+        else:
+            pass
         #else:
         #    msg = ""
         #    if resp:
@@ -197,13 +196,13 @@ class Wim(object):
         elif http_code == 204:
             print('Deleted')
         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):
         """Returns a list of VIM accounts
@@ -230,12 +229,13 @@ class Wim(object):
         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:
+        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 json.loads(resp)
-        raise NotFound("wim {} not found".format(name))
+        except NotFound:
+            raise NotFound("wim '{}' not found".format(name))