From: garciadeblas Date: Mon, 21 May 2018 22:49:13 +0000 (+0200) Subject: using http codes in sol005 client whenever possible X-Git-Url: https://osm.etsi.org/gitweb/?a=commitdiff_plain;h=refs%2Fchanges%2F86%2F6186%2F2;p=osm%2Fosmclient.git using http codes in sol005 client whenever possible Change-Id: I30d5c9a4231c557468ba25d5dac32317eb095b04 Signed-off-by: garciadeblas --- diff --git a/osmclient/sol005/ns.py b/osmclient/sol005/ns.py index 9ce1fcd..75cdf3f 100644 --- a/osmclient/sol005/ns.py +++ b/osmclient/sol005/ns.py @@ -81,15 +81,20 @@ class Ns(object): querystring = '?FORCE=True' http_code, resp = self._http.delete_cmd('{}/{}{}'.format(self._apiBase, ns['_id'], querystring)) - if resp: - resp = json.loads(resp) + #print 'HTTP CODE: {}'.format(http_code) #print 'RESP: {}'.format(resp) if http_code == 202: print 'Deletion in progress' elif http_code == 204: print 'Deleted' else: - raise ClientException("failed to delete ns {}: {}".format(name, resp)) + msg = "" + 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', @@ -156,14 +161,23 @@ class Ns(object): self._apiVersion, self._apiResource) http_code, resp = self._http.post_cmd(endpoint=self._apiBase, postfields_dict=ns) - if resp: - resp = json.loads(resp) + #print 'HTTP CODE: {}'.format(http_code) #print 'RESP: {}'.format(resp) - if not resp or 'id' not in resp: - raise ClientException('unexpected response from server: '.format( + if http_code in (200, 201, 202, 204): + if resp: + resp = json.loads(resp) + if not resp or 'id' not in resp: + raise ClientException('unexpected response from server - {} '.format( resp)) - else: print resp['id'] + else: + msg = "" + if resp: + try: + msg = json.loads(resp) + except ValueError: + msg = resp + raise ClientException(msg) except ClientException as exc: message="failed to create ns: {} nsd: {}\nerror:\n{}".format( nsr_name, @@ -182,15 +196,26 @@ class Ns(object): filter_string = '' if filter: filter_string = '&{}'.format(filter) - http_code, resp = self._http.get2_cmd('{}?nsInstanceId={}'.format(self._apiBase, ns['_id'], - filter_string) ) - resp = json.loads(resp) + http_code, resp = self._http.get2_cmd('{}?nsInstanceId={}'.format( + self._apiBase, ns['_id'], + filter_string) ) + #print 'HTTP CODE: {}'.format(http_code) #print 'RESP: {}'.format(resp) if http_code == 200: - return resp + if resp: + resp = json.loads(resp) + return resp + else: + raise ClientException('unexpected response from server') else: - raise ClientException('{}'.format(resp['detail'])) - + msg = "" + if resp: + try: + resp = json.loads(resp) + msg = resp['detail'] + except ValueError: + msg = resp + raise ClientException(msg) except ClientException as exc: message="failed to get operation list of NS {}:\nerror:\n{}".format( name, @@ -205,12 +230,23 @@ class Ns(object): self._apiBase = '{}{}{}'.format(self._apiName, self._apiVersion, self._apiResource) http_code, resp = self._http.get2_cmd('{}/{}'.format(self._apiBase, operationId)) - resp = json.loads(resp) + #print 'HTTP CODE: {}'.format(http_code) #print 'RESP: {}'.format(resp) if http_code == 200: - return resp + if resp: + resp = json.loads(resp) + return resp + else: + raise ClientException('unexpected response from server') else: - raise ClientException("{}".format(resp['detail'])) + msg = "" + if resp: + try: + resp = json.loads(resp) + msg = resp['detail'] + except ValueError: + msg = resp + raise ClientException(msg) except ClientException as exc: message="failed to get status of operation {}:\nerror:\n{}".format( operationId, @@ -229,14 +265,23 @@ class Ns(object): #print 'OP_NAME: {}'.format(op_name) #print 'OP_DATA: {}'.format(json.dumps(op_data)) http_code, resp = self._http.post_cmd(endpoint=endpoint, postfields_dict=op_data) - if resp: - resp = json.loads(resp) + #print 'HTTP CODE: {}'.format(http_code) #print 'RESP: {}'.format(resp) - if not resp or 'id' not in resp: - raise ClientException('unexpected response from server: '.format( + if http_code in (200, 201, 202, 204): + if resp: + resp = json.loads(resp) + if not resp or 'id' not in resp: + raise ClientException('unexpected response from server - {}'.format( resp)) - else: print resp['id'] + else: + msg = "" + if resp: + try: + msg = json.loads(resp) + except ValueError: + msg = resp + raise ClientException(msg) except ClientException as exc: message="failed to exec operation {}:\nerror:\n{}".format( name, @@ -250,15 +295,22 @@ class Ns(object): 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 'RESP: {}'.format(resp) print 'Alarm created' else: - raise ClientException('unexpected response from server: code: {}, resp: {}'.format( - http_code, resp)) + msg = "" + if resp: + try: + msg = json.loads(resp) + except ValueError: + msg = resp + raise ClientException('error: code: {}, resp: {}'.format( + http_code, msg)) except ClientException as exc: - message="failed to create alarm: alarm {}\nerror:\n{}".format( + message="failed to create alarm: alarm {}\n{}".format( alarm, exc.message) raise ClientException(message) @@ -271,15 +323,22 @@ class Ns(object): 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 'RESP: {}'.format(resp) print 'Alarm deleted' else: - raise ClientException('unexpected response from server: code: {}, resp: {}'.format( - http_code, resp)) + msg = "" + if resp: + try: + msg = json.loads(resp) + except ValueError: + msg = resp + raise ClientException('error: code: {}, resp: {}'.format( + http_code, msg)) except ClientException as exc: - message="failed to delete alarm: alarm {}\nerror:\n{}".format( + message="failed to delete alarm: alarm {}\n{}".format( alarm, exc.message) raise ClientException(message) @@ -290,15 +349,22 @@ class Ns(object): 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) - #print 'RESP: {}'.format(resp) return 'Metric exported' else: - raise ClientException('unexpected response from server: code: {}, resp: {}'.format( - http_code, resp)) + msg = "" + if resp: + try: + msg = json.loads(resp) + except ValueError: + msg = resp + raise ClientException('error: code: {}, resp: {}'.format( + http_code, msg)) except ClientException as exc: - message="failed to export metric: metric {}\nerror:\n{}".format( + message="failed to export metric: metric {}\n{}".format( metric, exc.message) raise ClientException(message) diff --git a/osmclient/sol005/nsd.py b/osmclient/sol005/nsd.py index ef9e5ab..af1e2ef 100644 --- a/osmclient/sol005/nsd.py +++ b/osmclient/sol005/nsd.py @@ -75,11 +75,20 @@ class Nsd(object): headers = self._client._headers headers['Accept'] = 'application/binary' http_code, resp = self._http.get2_cmd('{}/{}/{}'.format(self._apiBase, nsd['_id'], thing)) - #print yaml.safe_dump(resp2) - if resp: - #store in a file - return resp - raise NotFound("nsd {} not found".format(name)) + #print 'HTTP CODE: {}'.format(http_code) + #print 'RESP: {}'.format(resp) + if http_code in (200, 201, 202, 204): + if resp: + #store in a file + return resp + else: + msg = "" + if resp: + try: + msg = json.loads(resp) + except ValueError: + msg = resp + raise ClientException("failed to get {} from {} - {}".format(thing, name, msg)) def get_descriptor(self, name, filename): self.get_thing(name, 'nsd', filename) @@ -97,15 +106,20 @@ class Nsd(object): querystring = '?FORCE=True' http_code, resp = self._http.delete_cmd('{}/{}{}'.format(self._apiBase, nsd['_id'], querystring)) - if resp: - resp = json.loads(resp) + #print 'HTTP CODE: {}'.format(http_code) #print 'RESP: {}'.format(resp) if http_code == 202: print 'Deletion in progress' elif http_code == 204: print 'Deleted' else: - raise ClientException("failed to delete nsd {}: {}".format(name, resp)) + msg = "" + if resp: + try: + resp = 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): mime_type = magic.from_file(filename, mime=True) @@ -142,13 +156,23 @@ class Nsd(object): self._apiVersion, self._apiResource) endpoint = '{}{}'.format(self._apiBase,ow_string) http_code, resp = self._http.post_cmd(endpoint=endpoint, filename=filename) - if resp: - resp = json.loads(resp) - #print resp - if not resp or 'id' not in resp: - raise ClientException("failed to upload package") - else: + #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 not resp or 'id' not in resp: + raise ClientException('unexpected response from server - {}'.format( + resp)) print resp['id'] + else: + msg = "" + if resp: + try: + msg = json.loads(resp) + except ValueError: + msg = resp + raise ClientException("failed to create/update nsd - {}".format(name, msg)) def update(self, name, filename): nsd = self.get(name) diff --git a/osmclient/sol005/package.py b/osmclient/sol005/package.py index b2fe035..172ee45 100644 --- a/osmclient/sol005/package.py +++ b/osmclient/sol005/package.py @@ -59,12 +59,21 @@ class Package(object): for (key,val) in headers.items()] self._http.set_http_header(http_header) http_code, resp = self._http.post_cmd(endpoint=endpoint, filename=filename) - if resp: - resp = json.loads(resp) - #print 'RESP: {}'.format(yaml.safe_dump(resp)) - if not resp or 'id' not in resp: - raise ClientException("failed to upload package") - else: + #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 not resp or 'id' not in resp: + raise ClientException('unexpected response from server - {}'.format( + resp)) print resp['id'] - + else: + msg = "" + if resp: + try: + msg = json.loads(resp) + except ValueError: + msg = resp + raise ClientException("failed to delete ns {} - {}".format(name, msg)) diff --git a/osmclient/sol005/sdncontroller.py b/osmclient/sol005/sdncontroller.py index 04b0775..39ad3be 100644 --- a/osmclient/sol005/sdncontroller.py +++ b/osmclient/sol005/sdncontroller.py @@ -37,27 +37,45 @@ class SdnController(object): def create(self, name, sdn_controller): http_code, resp = self._http.post_cmd(endpoint=self._apiBase, postfields_dict=sdn_controller) - if resp: - resp = json.loads(resp) + #print 'HTTP CODE: {}'.format(http_code) #print 'RESP: {}'.format(resp) - if not resp or 'id' not in resp: - raise ClientException('failed to create SDN controller: '.format( - resp)) - else: + if http_code in (200, 201, 202, 204): + if resp: + resp = json.loads(resp) + if not resp or 'id' not in resp: + raise ClientException('unexpected response from server - {}'.format( + resp)) print resp['id'] + else: + msg = "" + if resp: + try: + msg = json.loads(resp) + except ValueError: + msg = resp + raise ClientException("failed to create SDN controller {} - {}".format(name, msg)) def update(self, name, sdn_controller): sdnc = self.get(name) http_code, resp = self._http.put_cmd(endpoint='{}/{}'.format(self._apiBase,sdnc['_id']), postfields_dict=sdn_controller) - if resp: - resp = json.loads(resp) + #print 'HTTP CODE: {}'.format(http_code) #print 'RESP: {}'.format(resp) - if not resp or 'id' not in resp: - raise ClientException('failed to update SDN controller: '.format( - resp)) - else: + if http_code in (200, 201, 202, 204): + if resp: + resp = json.loads(resp) + if not resp or 'id' not in resp: + raise ClientException('unexpected response from server - {}'.format( + resp)) print resp['id'] + else: + msg = "" + if resp: + try: + msg = json.loads(resp) + except ValueError: + msg = resp + raise ClientException("failed to update SDN controller {} - {}".format(name, msg)) def delete(self, name, force=False): sdn_controller = self.get(name) @@ -66,17 +84,22 @@ class SdnController(object): querystring = '?FORCE=True' http_code, resp = self._http.delete_cmd('{}/{}{}'.format(self._apiBase, sdn_controller['_id'], querystring)) - if resp: - resp = json.loads(resp) + #print 'HTTP CODE: {}'.format(http_code) #print 'RESP: {}'.format(resp) if http_code == 202: print 'Deletion in progress' elif http_code == 204: print 'Deleted' - elif 'result' in resp: + elif resp and 'result' in resp: print 'Deleted' else: - raise ClientException("failed to delete vim {} - {}".format(name, resp)) + msg = "" + 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 diff --git a/osmclient/sol005/vim.py b/osmclient/sol005/vim.py index dc107ef..6e15e5b 100644 --- a/osmclient/sol005/vim.py +++ b/osmclient/sol005/vim.py @@ -43,7 +43,7 @@ class Vim(object): vim_account['name'] = name vim_account = self.update_vim_account_dict(vim_account, vim_access) - vim_config = {'hello': 'hello'} + vim_config = {} if 'config' in vim_access and vim_access['config'] is not None: vim_config = yaml.safe_load(vim_access['config']) if sdn_controller: @@ -60,13 +60,21 @@ class Vim(object): postfields_dict=vim_account) #print 'HTTP CODE: {}'.format(http_code) #print 'RESP: {}'.format(resp) - if resp: - resp = json.loads(resp) - if not resp or 'id' not in resp: - raise ClientException('failed to create vim {}: {}'.format( - name, resp)) - else: + if http_code in (200, 201, 202, 204): + if resp: + resp = json.loads(resp) + if not resp or 'id' not in resp: + raise ClientException('unexpected response from server - {}'.format( + resp)) print resp['id'] + else: + msg = "" + if resp: + try: + msg = json.loads(resp) + except ValueError: + msg = resp + raise ClientException("failed to create vim {} - {}".format(name, msg)) def update(self, vim_name, vim_account, sdn_controller, sdn_port_mapping): vim = self.get(vim_name) @@ -91,12 +99,21 @@ class Vim(object): postfields_dict=vim_account) #print 'HTTP CODE: {}'.format(http_code) #print 'RESP: {}'.format(resp) - if resp: - resp = json.loads(resp) - if not resp or 'id' not in resp: - raise ClientException('failed to update vim: '.format(resp)) - else: + if http_code in (200, 201, 202, 204): + if resp: + resp = json.loads(resp) + if not resp or 'id' not in resp: + raise ClientException('unexpected response from server - {}'.format( + resp)) print resp['id'] + else: + msg = "" + if resp: + try: + msg = json.loads(resp) + except ValueError: + msg = resp + raise ClientException("failed to update vim {} - {}".format(vim_name, msg)) def update_vim_account_dict(self, vim_account, vim_access): vim_account['vim_type'] = vim_access['vim-type'] @@ -124,15 +141,20 @@ class Vim(object): querystring = '?FORCE=True' http_code, resp = self._http.delete_cmd('{}/{}{}'.format(self._apiBase, vim_id, querystring)) - if resp: - resp = json.loads(resp) + #print 'HTTP CODE: {}'.format(http_code) #print 'RESP: {}'.format(resp) if http_code == 202: print 'Deletion in progress' elif http_code == 204: print 'Deleted' else: - raise ClientException("failed to delete vim {} - {}".format(vim_name, resp)) + msg = "" + 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 diff --git a/osmclient/sol005/vnfd.py b/osmclient/sol005/vnfd.py index c903f93..5afde93 100644 --- a/osmclient/sol005/vnfd.py +++ b/osmclient/sol005/vnfd.py @@ -74,11 +74,20 @@ class Vnfd(object): headers = self._client._headers headers['Accept'] = 'application/binary' http_code, resp = self._http.get2_cmd('{}/{}/{}'.format(self._apiBase, vnfd['_id'], thing)) - #print yaml.safe_dump(resp2) - if resp: - #store in a file - return resp - raise NotFound("vnfd {} not found".format(name)) + #print 'HTTP CODE: {}'.format(http_code) + #print 'RESP: {}'.format(resp) + if http_code in (200, 201, 202, 204): + if resp: + #store in a file + return resp + else: + msg = "" + if resp: + try: + msg = json.loads(resp) + except ValueError: + msg = resp + raise ClientException("failed to get {} from {} - {}".format(thing, name, msg)) def get_descriptor(self, name, filename): self.get_thing(name, 'vnfd', filename) @@ -96,15 +105,20 @@ class Vnfd(object): querystring = '?FORCE=True' http_code, resp = self._http.delete_cmd('{}/{}{}'.format(self._apiBase, vnfd['_id'], querystring)) - if resp: - resp = json.loads(resp) + #print 'HTTP CODE: {}'.format(http_code) #print 'RESP: {}'.format(resp) if http_code == 202: print 'Deletion in progress' elif http_code == 204: print 'Deleted' else: - raise ClientException("failed to delete vnfd {}: {}".format(name, resp)) + msg = "" + 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): mime_type = magic.from_file(filename, mime=True) @@ -141,13 +155,23 @@ class Vnfd(object): self._apiVersion, self._apiResource) endpoint = '{}{}'.format(self._apiBase,ow_string) http_code, resp = self._http.post_cmd(endpoint=endpoint, filename=filename) - if resp: - resp = json.loads(resp) - #print resp - if not resp or 'id' not in resp: - raise ClientException("failed to upload package") - else: + #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 not resp or 'id' not in resp: + raise ClientException('unexpected response from server: '.format( + resp)) print resp['id'] + else: + msg = "" + if resp: + try: + msg = json.loads(resp) + except ValueError: + msg = resp + raise ClientException("failed to create/update vnfd - {}".format(name, msg)) def update(self, name, filename): vnfd = self.get(name)