From: pinoa Date: Thu, 12 Dec 2019 11:10:27 +0000 (+0100) Subject: Fix Bug 934. OSM CLI crashes for listing operations with insufficient permissions X-Git-Tag: v7.0.1rc2^2~2 X-Git-Url: https://osm.etsi.org/gitweb/?a=commitdiff_plain;h=refs%2Fchanges%2F85%2F8385%2F14;p=osm%2Fosmclient.git Fix Bug 934. OSM CLI crashes for listing operations with insufficient permissions Change-Id: I77c802a6ad1d77979b01377b0d1bebfff936e306 Signed-off-by: pinoa --- diff --git a/osmclient/common/exceptions.py b/osmclient/common/exceptions.py index 0be2778..92ed0f6 100644 --- a/osmclient/common/exceptions.py +++ b/osmclient/common/exceptions.py @@ -21,3 +21,6 @@ class ClientException(Exception): class NotFound(ClientException): pass + +class OsmHttpException(ClientException): + pass diff --git a/osmclient/sol005/client.py b/osmclient/sol005/client.py index bf6f845..3f397f7 100644 --- a/osmclient/sol005/client.py +++ b/osmclient/sol005/client.py @@ -36,7 +36,6 @@ from osmclient.sol005 import role from osmclient.sol005 import pdud from osmclient.sol005 import k8scluster from osmclient.sol005 import repo -from osmclient.common.exceptions import ClientException from osmclient.common import package_tool import json import logging @@ -107,9 +106,9 @@ class Client(object): 'project_id': self._project} http_code, resp = self._http_client.post_cmd(endpoint=self._auth_endpoint, postfields_dict=postfields_dict) - if http_code not in (200, 201, 202, 204): - message ='Authentication error: not possible to get auth token\nresp:\n{}'.format(resp) - raise ClientException(message) +# if http_code not in (200, 201, 202, 204): +# message ='Authentication error: not possible to get auth token\nresp:\n{}'.format(resp) +# raise ClientException(message) token = json.loads(resp) if resp else None self._token = token['id'] @@ -121,6 +120,7 @@ class Client(object): self._http_client.set_http_header(http_header) def get_version(self): - resp = self._http_client.get_cmd(endpoint="/version") + _, resp = self._http_client.get2_cmd(endpoint="/version") + resp = json.loads(resp) return "{} {}".format(resp.get("version"), resp.get("date")) diff --git a/osmclient/sol005/http.py b/osmclient/sol005/http.py index aca8a4b..9bdf94e 100644 --- a/osmclient/sol005/http.py +++ b/osmclient/sol005/http.py @@ -20,6 +20,7 @@ import json import logging import copy from osmclient.common import http +from osmclient.common.exceptions import OsmHttpException class Http(http.Http): @@ -54,6 +55,7 @@ class Http(http.Http): http_code = curl_cmd.getinfo(pycurl.HTTP_CODE) self._logger.info("Response HTTPCODE: {}".format(http_code)) curl_cmd.close() + 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()))) @@ -106,6 +108,7 @@ class Http(http.Http): http_code = curl_cmd.getinfo(pycurl.HTTP_CODE) self._logger.info("Response HTTPCODE: {}".format(http_code)) 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() @@ -150,8 +153,15 @@ class Http(http.Http): http_code = curl_cmd.getinfo(pycurl.HTTP_CODE) self._logger.info("Response HTTPCODE: {}".format(http_code)) 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() return http_code, None + def check_http_response(self, http_code, data): + if http_code >= 300: + resp = "" + if data.getvalue(): + resp=": " + data.getvalue().decode() + raise OsmHttpException("Error {}{}".format(http_code, resp)) diff --git a/osmclient/sol005/k8scluster.py b/osmclient/sol005/k8scluster.py index 5520787..f001dee 100644 --- a/osmclient/sol005/k8scluster.py +++ b/osmclient/sol005/k8scluster.py @@ -17,8 +17,8 @@ OSM K8s cluster API handling """ 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 class K8scluster(object): @@ -45,21 +45,22 @@ class K8scluster(object): postfields_dict=k8s_cluster) #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)) + #if http_code in (200, 201, 202, 204): + if resp: + resp = json.loads(resp) print(resp['id']) - else: - msg = "" - if resp: - try: - msg = json.loads(resp) - except ValueError: - msg = resp - raise ClientException("failed to add K8s cluster {} - {}".format(name, msg)) + if not resp or 'id' not in resp: + raise OsmHttpException('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 add K8s cluster {} - {}".format(name, msg)) def update(self, name, k8s_cluster): self._client.get_token() @@ -68,16 +69,16 @@ class K8scluster(object): postfields_dict=k8s_cluster) # print 'HTTP CODE: {}'.format(http_code) # print 'RESP: {}'.format(resp) - if http_code in (200, 201, 202, 204): - pass - else: - msg = "" - if resp: - try: - msg = json.loads(resp) - except ValueError: - msg = resp - raise ClientException("failed to update K8s cluster {} - {}".format(name, msg)) + #if http_code in (200, 201, 202, 204): + #pass + #else: + # msg = "" + # if resp: + # try: + # msg = json.loads(resp) + # except ValueError: + # msg = resp + # raise ClientException("failed to update K8s cluster {} - {}".format(name, msg)) def get_id(self, name): """Returns a K8s cluster id from a K8s cluster name @@ -110,7 +111,7 @@ class K8scluster(object): msg = json.loads(resp) except ValueError: msg = resp - raise ClientException("failed to delete K8s cluster {} - {}".format(name, msg)) + raise OsmHttpException("failed to delete K8s cluster {} - {}".format(name, msg)) def list(self, filter=None): """Returns a list of K8s clusters @@ -119,9 +120,9 @@ class K8scluster(object): filter_string = '' if filter: filter_string = '?{}'.format(filter) - resp = self._http.get_cmd('{}{}'.format(self._apiBase,filter_string)) + _, resp = self._http.get2_cmd('{}{}'.format(self._apiBase,filter_string)) if resp: - return resp + return json.loads(resp) return list() def get(self, name): @@ -131,10 +132,11 @@ class K8scluster(object): cluster_id = name if not utils.validate_uuid4(name): cluster_id = self.get_id(name) - resp = self._http.get_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: - return resp + _, 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)) diff --git a/osmclient/sol005/ns.py b/osmclient/sol005/ns.py index 12c5416..c8e2a77 100644 --- a/osmclient/sol005/ns.py +++ b/osmclient/sol005/ns.py @@ -22,6 +22,7 @@ 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 @@ -61,9 +62,9 @@ class Ns(object): filter_string = '' if filter: filter_string = '?{}'.format(filter) - resp = self._http.get_cmd('{}{}'.format(self._apiBase,filter_string)) + _, resp = self._http.get2_cmd('{}{}'.format(self._apiBase,filter_string)) if resp: - return resp + return json.loads(resp) return list() def get(self, name): @@ -90,11 +91,11 @@ class Ns(object): if name == ns['name']: ns_id = ns['_id'] break - resp = self._http.get_cmd('{}/{}'.format(self._apiBase, ns_id)) + _, 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 resp + return json.loads(resp) raise NotFound("ns {} not found".format(name)) def delete(self, name, force=False, wait=False): @@ -123,7 +124,7 @@ class Ns(object): msg = json.loads(resp) except ValueError: msg = resp - raise ClientException("failed to delete ns {} - {}".format(name, msg)) + raise OsmHttpException("failed to delete ns {} - {}".format(name, msg)) def create(self, nsd_name, nsr_name, account, config=None, ssh_keys=None, description='default description', @@ -226,33 +227,33 @@ class Ns(object): for (key,val) in list(headers.items())] self._http.set_http_header(http_header) http_code, resp = self._http.post_cmd(endpoint=self._apiBase, - postfields_dict=ns) + postfields_dict=ns) # 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( + #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)) - if wait: - # Wait for status for NS instance creation - self._wait(resp.get('nslcmop_id')) - return resp['id'] - else: - msg = "" - if resp: - try: - msg = json.loads(resp) - except ValueError: - msg = resp - raise ClientException(msg) + if wait: + # Wait for status for NS instance creation + self._wait(resp.get('nslcmop_id')) + return 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, nsd_name, str(exc)) - raise ClientException(message) + raise OsmHttpException(message) def list_op(self, name, filter=None): """Returns the list of operations of a NS @@ -265,32 +266,32 @@ class Ns(object): self._apiVersion, self._apiResource) filter_string = '' if filter: - filter_string = '&{}'.format(filter) + filter_string = '&{}'.format(filter) 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: - if resp: - resp = json.loads(resp) - return resp - else: - raise ClientException('unexpected response from server') + #if http_code == 200: + if resp: + resp = json.loads(resp) + return resp else: - msg = "" - if resp: - try: - resp = json.loads(resp) - msg = resp['detail'] - except ValueError: - msg = resp - raise ClientException(msg) + raise ClientException('unexpected response from server') + #else: + # 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, str(exc)) - raise ClientException(message) + raise OsmHttpException(message) def get_op(self, operationId): """Returns the status of an operation @@ -304,26 +305,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 - else: - raise ClientException('unexpected response from server') + #if http_code == 200: + if resp: + resp = json.loads(resp) + return resp else: - msg = "" - if resp: - try: - resp = json.loads(resp) - msg = resp['detail'] - except ValueError: - msg = resp - raise ClientException(msg) + raise ClientException('unexpected response from server') + #else: + # 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, str(exc)) - raise ClientException(message) + raise OsmHttpException(message) def exec_op(self, name, op_name, op_data=None, wait=False, ): """Executes an operation on a NS @@ -341,30 +342,30 @@ class Ns(object): http_code, resp = self._http.post_cmd(endpoint=endpoint, postfields_dict=op_data) #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)) - if wait: - # Wait for status for NS instance action - # For the 'action' operation, 'id' is used - self._wait(resp.get('id')) - return resp['id'] - else: - msg = "" - if resp: - try: - msg = json.loads(resp) - except ValueError: - msg = resp - raise ClientException(msg) + #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)) + if wait: + # Wait for status for NS instance action + # For the 'action' operation, 'id' is used + self._wait(resp.get('id')) + return 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, str(exc)) - raise ClientException(message) + raise OsmHttpException(message) def scale_vnf(self, ns_name, vnf_name, scaling_group, scale_in, scale_out, wait=False): """Scales a VNF by adding/removing VDUs @@ -396,28 +397,28 @@ 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') - else: - 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 {}\n{}".format( - alarm, - str(exc)) - raise ClientException(message) + #if http_code in (200, 201, 202, 204): + #resp = json.loads(resp) + print('Alarm created') + #else: + # 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 {}\n{}".format( + # alarm, + # str(exc)) + # raise ClientException(message) def delete_alarm(self, name): self._logger.debug("") @@ -426,56 +427,56 @@ 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') - else: - 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 {}\n{}".format( - name, - str(exc)) - raise ClientException(message) + #if http_code in (200, 201, 202, 204): + #resp = json.loads(resp) + print('Alarm deleted') + #else: + # 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 {}\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' - else: - 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 {}\n{}".format( - metric, - str(exc)) - raise ClientException(message) + #if http_code in (200, 201, 202, 204): + #resp = json.loads(resp) + return 'Metric exported' + #else: + # 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 {}\n{}".format( + # metric, + # str(exc)) + # raise ClientException(message) def get_field(self, ns_name, field): self._logger.debug("") diff --git a/osmclient/sol005/nsd.py b/osmclient/sol005/nsd.py index bf91ca6..a2a1902 100644 --- a/osmclient/sol005/nsd.py +++ b/osmclient/sol005/nsd.py @@ -20,6 +20,7 @@ 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 @@ -47,10 +48,10 @@ class Nsd(object): filter_string = '' if filter: filter_string = '?{}'.format(filter) - resp = self._http.get_cmd('{}{}'.format(self._apiBase, filter_string)) + _, resp = self._http.get2_cmd('{}{}'.format(self._apiBase, filter_string)) #print(yaml.safe_dump(resp)) if resp: - return resp + return json.loads(resp) return list() def get(self, name): @@ -72,10 +73,10 @@ class Nsd(object): 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.get_cmd('{}/{}'.format(self._apiBase, nsd['_id'])) + _, resp = self._http.get2_cmd('{}/{}'.format(self._apiBase, nsd['_id'])) #print(yaml.safe_dump(resp)) if resp: - return resp + return json.loads(resp) raise NotFound("nsd {} not found".format(name)) def get_thing(self, name, thing, filename): @@ -87,18 +88,18 @@ class Nsd(object): http_code, resp = self._http.get2_cmd('{}/{}/{}'.format(self._apiBase, nsd['_id'], thing)) #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)) + #if http_code in (200, 201, 202, 204): + if resp: + #store in a file + return json.loads(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._logger.debug("") @@ -133,7 +134,7 @@ class Nsd(object): msg = json.loads(resp) except ValueError: msg = resp - raise ClientException("failed to delete nsd {} - {}".format(name, msg)) + raise OsmHttpException("failed to delete nsd {} - {}".format(name, msg)) def create(self, filename, overwrite=None, update_endpoint=None): self._logger.debug("") @@ -175,23 +176,22 @@ class Nsd(object): http_code, resp = self._http.post_cmd(endpoint=endpoint, filename=filename) #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): if resp: resp = json.loads(resp) if not resp or 'id' not in resp: - raise ClientException('unexpected response from server - {}'.format( - resp)) + raise OsmHttpException('unexpected response from server - {}'.format(resp)) print(resp['id']) elif http_code == 204: print('Updated') - else: - msg = "Error {}".format(http_code) - if resp: - try: - msg = "{} - {}".format(msg, json.loads(resp)) - except ValueError: - msg = "{} - {}".format(msg, resp) - raise ClientException("failed to create/update nsd - {}".format(msg)) + # else: + # msg = "Error {}".format(http_code) + # if resp: + # try: + # msg = "{} - {}".format(msg, json.loads(resp)) + # except ValueError: + # msg = "{} - {}".format(msg, resp) + # raise ClientException("failed to create/update nsd - {}".format(msg)) def update(self, name, filename): self._logger.debug("") diff --git a/osmclient/sol005/nsi.py b/osmclient/sol005/nsi.py index c01b4e9..b582ae1 100644 --- a/osmclient/sol005/nsi.py +++ b/osmclient/sol005/nsi.py @@ -22,6 +22,7 @@ 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 @@ -62,9 +63,9 @@ class Nsi(object): filter_string = '' if filter: filter_string = '?{}'.format(filter) - resp = self._http.get_cmd('{}{}'.format(self._apiBase,filter_string)) + _, resp = self._http.get2_cmd('{}{}'.format(self._apiBase,filter_string)) if resp: - return resp + return json.loads(resp) return list() def get(self, name): @@ -91,11 +92,11 @@ class Nsi(object): if name == nsi['name']: nsi_id = nsi['_id'] break - resp = self._http.get_cmd('{}/{}'.format(self._apiBase, nsi_id)) + _, 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 resp + return json.loads(resp) raise NotFound("nsi {} not found".format(name)) def delete(self, name, force=False, wait=False): @@ -125,7 +126,7 @@ class Nsi(object): msg = json.loads(resp) except ValueError: msg = resp - raise ClientException("failed to delete nsi {} - {}".format(name, msg)) + raise OsmHttpException("failed to delete nsi {} - {}".format(name, msg)) def create(self, nst_name, nsi_name, account, config=None, ssh_keys=None, description='default description', @@ -226,33 +227,33 @@ class Nsi(object): for (key,val) in list(headers.items())] self._http.set_http_header(http_header) http_code, resp = self._http.post_cmd(endpoint=self._apiBase, - postfields_dict=nsi) + postfields_dict=nsi) #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)) - if wait: - # Wait for status for NSI instance creation - self._wait(resp.get('nsilcmop_id')) - print(resp['id']) - else: - msg = "" - if resp: - try: - msg = json.loads(resp) - except ValueError: - msg = resp - raise ClientException(msg) + #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)) + if wait: + # Wait for status for NSI instance creation + self._wait(resp.get('nsilcmop_id')) + 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 nsi: {} nst: {}\nerror:\n{}".format( nsi_name, nst_name, str(exc)) - raise ClientException(message) + raise OsmHttpException(message) def list_op(self, name, filter=None): """Returns the list of operations of a NSI @@ -271,26 +272,26 @@ class Nsi(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 - else: - raise ClientException('unexpected response from server') + #if http_code == 200: + if resp: + resp = json.loads(resp) + return resp else: - msg = "" - if resp: - try: - resp = json.loads(resp) - msg = resp['detail'] - except ValueError: - msg = resp - raise ClientException(msg) + raise ClientException('unexpected response from server') + #else: + # 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 NSI {}:\nerror:\n{}".format( name, str(exc)) - raise ClientException(message) + raise OsmHttpException(message) def get_op(self, operationId): """Returns the status of an operation @@ -304,26 +305,26 @@ class Nsi(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 - else: - raise ClientException('unexpected response from server') + #if http_code == 200: + if resp: + resp = json.loads(resp) + return resp else: - msg = "" - if resp: - try: - resp = json.loads(resp) - msg = resp['detail'] - except ValueError: - msg = resp - raise ClientException(msg) + raise ClientException('unexpected response from server') + #else: + # 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, str(exc)) - raise ClientException(message) + raise OsmHttpException(message) def exec_op(self, name, op_name, op_data=None): """Executes an operation on a NSI @@ -340,24 +341,24 @@ class Nsi(object): http_code, resp = self._http.post_cmd(endpoint=endpoint, postfields_dict=op_data) #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(msg) + #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(msg) except ClientException as exc: message="failed to exec operation {}:\nerror:\n{}".format( name, str(exc)) - raise ClientException(message) + raise OsmHttpException(message) diff --git a/osmclient/sol005/nst.py b/osmclient/sol005/nst.py index e75c8f5..f6a03f1 100644 --- a/osmclient/sol005/nst.py +++ b/osmclient/sol005/nst.py @@ -20,6 +20,7 @@ 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 @@ -45,10 +46,10 @@ class Nst(object): filter_string = '' if filter: filter_string = '?{}'.format(filter) - resp = self._http.get_cmd('{}{}'.format(self._apiBase, filter_string)) + _, resp = self._http.get2_cmd('{}{}'.format(self._apiBase, filter_string)) #print(yaml.safe_dump(resp)) if resp: - return resp + return json.loads(resp) return list() def get(self, name): @@ -69,10 +70,10 @@ 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.get_cmd('{}/{}'.format(self._apiBase, nst['_id'])) + _, resp = self._http.get2_cmd('{}/{}'.format(self._apiBase, nst['_id'])) #print(yaml.safe_dump(resp)) if resp: - return resp + return json.loads(resp) raise NotFound("nst {} not found".format(name)) def get_thing(self, name, thing, filename): @@ -83,18 +84,18 @@ class Nst(object): http_code, resp = self._http.get2_cmd('{}/{}/{}'.format(self._apiBase, nst['_id'], thing)) #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)) + #if http_code in (200, 201, 202, 204): + if resp: + #store in a file + return json.loads(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._logger.debug("") @@ -129,7 +130,7 @@ class Nst(object): resp = json.loads(resp) except ValueError: msg = resp - raise ClientException("failed to delete nst {} - {}".format(name, msg)) + raise OsmHttpException("failed to delete nst {} - {}".format(name, msg)) def create(self, filename, overwrite=None, update_endpoint=None): self._logger.debug("") @@ -170,21 +171,20 @@ class Nst(object): http_code, resp = self._http.post_cmd(endpoint=endpoint, filename=filename) #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 = "Error {}".format(http_code) - if resp: - try: - msg = "{} - {}".format(msg, json.loads(resp)) - except ValueError: - msg = "{} - {}".format(msg, resp) - raise ClientException("failed to create/update nst - {}".format(msg)) + # if http_code in (200, 201, 202, 204): + if resp: + resp = json.loads(resp) + if not resp or 'id' not in resp: + raise OsmHttpException('unexpected response from server - {}'.format(resp)) + print(resp['id']) + # else: + # msg = "Error {}".format(http_code) + # if resp: + # try: + # msg = "{} - {}".format(msg, json.loads(resp)) + # except ValueError: + # msg = "{} - {}".format(msg, resp) + # raise ClientException("failed to create/update nst - {}".format(msg)) def update(self, name, filename): self._logger.debug("") diff --git a/osmclient/sol005/package.py b/osmclient/sol005/package.py index a52ba15..27b3ad4 100644 --- a/osmclient/sol005/package.py +++ b/osmclient/sol005/package.py @@ -20,6 +20,7 @@ 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 @@ -100,19 +101,18 @@ class Package(object): http_code, resp = self._http.post_cmd(endpoint=endpoint, filename=filename) #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 upload package - {}".format(msg)) - + #if http_code in (200, 201, 202, 204): + if resp: + resp = json.loads(resp) + if not resp or 'id' not in resp: + raise OsmHttpException('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 upload package - {}".format(msg)) diff --git a/osmclient/sol005/pdud.py b/osmclient/sol005/pdud.py index 42fe40d..9b15ca7 100644 --- a/osmclient/sol005/pdud.py +++ b/osmclient/sol005/pdud.py @@ -19,7 +19,7 @@ OSM pdud 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 logging @@ -43,9 +43,9 @@ class Pdu(object): filter_string = '' if filter: filter_string = '?{}'.format(filter) - resp = self._http.get_cmd('{}{}'.format(self._apiBase,filter_string)) + _, resp = self._http.get2_cmd('{}{}'.format(self._apiBase,filter_string)) if resp: - return resp + return json.loads(resp) return list() def get(self, name): @@ -66,10 +66,10 @@ 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.get_cmd('{}/{}'.format(self._apiBase, pdud['_id'])) + _, resp = self._http.get2_cmd('{}/{}'.format(self._apiBase, pdud['_id'])) #print(yaml.safe_dump(resp)) if resp: - return resp + return json.loads(resp) raise NotFound("pdu {} not found".format(name)) def delete(self, name, force=False): @@ -93,7 +93,7 @@ class Pdu(object): msg = json.loads(resp) except ValueError: msg = resp - raise ClientException("failed to delete pdu {} - {}".format(name, msg)) + raise OsmHttpException("failed to delete pdu {} - {}".format(name, msg)) def create(self, pdu, update_endpoint=None): self._logger.debug("") @@ -111,21 +111,21 @@ class Pdu(object): http_code, resp = self._http.post_cmd(endpoint=endpoint, postfields_dict=pdu) #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 = "Error {}".format(http_code) - if resp: - try: - msg = "{} - {}".format(msg, json.loads(resp)) - except ValueError: - msg = "{} - {}".format(msg, resp) - raise ClientException("failed to create/update pdu - {}".format(msg)) + #if http_code in (200, 201, 202, 204): + if resp: + resp = json.loads(resp) + if not resp or 'id' not in resp: + raise OsmHttpException('unexpected response from server: '.format( + resp)) + print(resp['id']) + #else: + # msg = "Error {}".format(http_code) + # if resp: + # try: + # msg = "{} - {}".format(msg, json.loads(resp)) + # except ValueError: + # msg = "{} - {}".format(msg, resp) + # raise ClientException("failed to create/update pdu - {}".format(msg)) def update(self, name, filename): self._logger.debug("") diff --git a/osmclient/sol005/project.py b/osmclient/sol005/project.py index 8f119e8..39d2453 100644 --- a/osmclient/sol005/project.py +++ b/osmclient/sol005/project.py @@ -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 @@ -46,21 +46,21 @@ class Project(object): postfields_dict=project) #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 project {} - {}".format(name, msg)) + #if http_code in (200, 201, 202, 204): + if resp: + resp = json.loads(resp) + if not resp or 'id' not in resp: + raise OsmHttpException('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 project {} - {}".format(name, msg)) def update(self, project, project_changes): """Updates an OSM project identified by name @@ -76,19 +76,19 @@ class Project(object): if resp: resp = json.loads(resp) if not resp or 'id' not in resp: - raise ClientException('unexpected response from server - {}'.format( + raise OsmHttpException('unexpected response from server - {}'.format( resp)) print(resp['id']) elif http_code == 204: print("Updated") - else: - msg = "" - if resp: - try: - msg = json.loads(resp) - except ValueError: - msg = resp - raise ClientException("failed to update project {} - {}".format(project, msg)) + #else: + # msg = "" + # if resp: + # try: + # msg = json.loads(resp) + # except ValueError: + # msg = resp + # raise ClientException("failed to update project {} - {}".format(project, msg)) def delete(self, name, force=False): """Deletes an OSM project identified by name @@ -116,7 +116,7 @@ class Project(object): msg = json.loads(resp) except ValueError: msg = resp - raise ClientException("failed to delete project {} - {}".format(name, msg)) + raise OsmHttpException("failed to delete project {} - {}".format(name, msg)) def list(self, filter=None): """Returns the list of OSM projects @@ -126,10 +126,10 @@ class Project(object): filter_string = '' if filter: filter_string = '?{}'.format(filter) - resp = self._http.get_cmd('{}{}'.format(self._apiBase,filter_string)) + _, resp = self._http.get2_cmd('{}{}'.format(self._apiBase,filter_string)) #print('RESP: {}'.format(resp)) if resp: - return resp + return json.loads(resp) return list() def get(self, name): diff --git a/osmclient/sol005/repo.py b/osmclient/sol005/repo.py index a8d8f17..1f487de 100644 --- a/osmclient/sol005/repo.py +++ b/osmclient/sol005/repo.py @@ -17,6 +17,7 @@ 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 @@ -37,21 +38,21 @@ class Repo(object): postfields_dict=repo) #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 add repo {} - {}".format(name, msg)) + #if http_code in (200, 201, 202, 204): + if resp: + resp = json.loads(resp) + if not resp or 'id' not in resp: + raise OsmHttpException('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 add repo {} - {}".format(name, msg)) def update(self, name, repo): self._client.get_token() @@ -60,16 +61,16 @@ class Repo(object): postfields_dict=repo) # print 'HTTP CODE: {}'.format(http_code) # print 'RESP: {}'.format(resp) - if http_code in (200, 201, 202, 204): - pass - else: - msg = "" - if resp: - try: - msg = json.loads(resp) - except ValueError: - msg = resp - raise ClientException("failed to update repo {} - {}".format(name, msg)) + #if http_code in (200, 201, 202, 204): + # pass + #else: + # msg = "" + # if resp: + # try: + # msg = json.loads(resp) + # except ValueError: + # msg = resp + # raise ClientException("failed to update repo {} - {}".format(name, msg)) def get_id(self, name): """Returns a repo id from a repo name @@ -112,9 +113,9 @@ class Repo(object): filter_string = '' if filter: filter_string = '?{}'.format(filter) - resp = self._http.get_cmd('{}{}'.format(self._apiBase,filter_string)) + _, resp = self._http.get2_cmd('{}{}'.format(self._apiBase,filter_string)) if resp: - return resp + return json.loads(resp) return list() def get(self, name): @@ -124,10 +125,11 @@ class Repo(object): repo_id = name if not utils.validate_uuid4(name): repo_id = self.get_id(name) - resp = self._http.get_cmd('{}/{}'.format(self._apiBase,repo_id)) - if not resp or '_id' not in resp: - raise ClientException('failed to get repo info: '.format(resp)) - else: - return resp + _, 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)) diff --git a/osmclient/sol005/role.py b/osmclient/sol005/role.py index 404784a..733e00f 100644 --- a/osmclient/sol005/role.py +++ b/osmclient/sol005/role.py @@ -23,6 +23,7 @@ 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 @@ -68,21 +69,21 @@ class Role(object): postfields_dict=role) # 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 role {} - {}".format(name, msg)) + #if http_code in (200, 201, 202, 204): + if resp: + resp = json.loads(resp) + if not resp or 'id' not in resp: + raise OsmHttpException('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 role {} - {}".format(name, msg)) def update(self, name, new_name, permissions, add=None, remove=None): """ @@ -155,19 +156,19 @@ class Role(object): if resp: resp = json.loads(resp) if not resp or 'id' not in resp: - raise ClientException('Unexpected response from server - {}'.format( + raise OsmHttpException('Unexpected response from server - {}'.format( resp)) print(resp['id']) elif http_code == 204: print("Updated") - else: - msg = "" - if resp: - try: - msg = json.loads(resp) - except ValueError: - msg = resp - raise ClientException("Failed to update role {} - {}".format(name, msg)) + #else: + # msg = "" + # if resp: + # try: + # msg = json.loads(resp) + # except ValueError: + # msg = resp + # raise ClientException("Failed to update role {} - {}".format(name, msg)) def delete(self, name, force=False): """ @@ -200,7 +201,7 @@ class Role(object): msg = json.loads(resp) except ValueError: msg = resp - raise ClientException("Failed to delete role {} - {}".format(name, msg)) + raise OsmHttpException("Failed to delete role {} - {}".format(name, msg)) def list(self, filter=None): """ @@ -214,10 +215,10 @@ class Role(object): filter_string = '' if filter: filter_string = '?{}'.format(filter) - resp = self._http.get_cmd('{}{}'.format(self._apiBase, filter_string)) + _, resp = self._http.get2_cmd('{}{}'.format(self._apiBase, filter_string)) # print('RESP: {}'.format(resp)) if resp: - return resp + return json.loads(resp) return list() def get(self, name): diff --git a/osmclient/sol005/sdncontroller.py b/osmclient/sol005/sdncontroller.py index b2bbc37..2a4d409 100644 --- a/osmclient/sol005/sdncontroller.py +++ b/osmclient/sol005/sdncontroller.py @@ -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 @@ -67,26 +67,28 @@ class SdnController(object): def create(self, name, sdn_controller, wait=False): self._logger.debug("") self._client.get_token() - http_code, resp = self._http.post_cmd(endpoint=self._apiBase, postfields_dict=sdn_controller) - # 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)) - if wait: - # Wait for status for SDNC instance creation - self._wait(resp.get('id')) - 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)) + http_code, resp = self._http.post_cmd(endpoint=self._apiBase, + postfields_dict=sdn_controller) + #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 OsmHttpException('unexpected response from server - {}'.format( + resp)) + if wait: + # Wait for status for SDNC instance creation + self._wait(resp.get('id')) + 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, wait=False): self._logger.debug("") @@ -97,23 +99,23 @@ class SdnController(object): postfields_dict=sdn_controller) # print('HTTP CODE: {}'.format(http_code)) # print('RESP: {}'.format(resp)) - 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 = sdnc_id_for_wait - # Wait for status for VI instance update - self._wait(wait_id) - else: - pass + #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 = sdnc_id_for_wait + # Wait for status for VI instance update + self._wait(wait_id) else: - msg = "" - if resp: - try: - msg = json.loads(resp) - except ValueError: - msg = resp - raise ClientException("failed to update SDN controller {} - {}".format(name, msg)) + pass + #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, wait=False): self._logger.debug("") @@ -144,7 +146,7 @@ class SdnController(object): msg = json.loads(resp) except ValueError: msg = resp - raise ClientException("failed to delete SDN controller {} - {}".format(name, msg)) + raise OsmHttpException("failed to delete SDN controller {} - {}".format(name, msg)) def list(self, filter=None): """Returns a list of SDN controllers @@ -154,10 +156,10 @@ class SdnController(object): filter_string = '' if filter: filter_string = '?{}'.format(filter) - resp = self._http.get_cmd('{}{}'.format(self._apiBase, filter_string)) - # print('RESP: {}'.format(resp)) + _, resp = self._http.get2_cmd('{}{}'.format(self._apiBase,filter_string)) + #print('RESP: {}'.format(resp)) if resp: - return resp + return json.loads(resp) return list() def get(self, name): diff --git a/osmclient/sol005/user.py b/osmclient/sol005/user.py index 6c10325..276c450 100644 --- a/osmclient/sol005/user.py +++ b/osmclient/sol005/user.py @@ -22,6 +22,7 @@ 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 @@ -68,21 +69,21 @@ class User(object): postfields_dict=user) #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 user {} - {}".format(name, msg)) + #if http_code in (200, 201, 202, 204): + if resp: + resp = json.loads(resp) + if not resp or 'id' not in resp: + raise OsmHttpException('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 user {} - {}".format(name, msg)) def update(self, name, user): """Updates an existing OSM user identified by name @@ -152,19 +153,19 @@ class User(object): if resp: resp = json.loads(resp) if not resp or 'id' not in resp: - raise ClientException('unexpected response from server - {}'.format( + raise OsmHttpException('unexpected response from server - {}'.format( resp)) print(resp['id']) elif http_code == 204: print('Updated') - else: - msg = "" - if resp: - try: - msg = json.loads(resp) - except ValueError: - msg = resp - raise ClientException("failed to update user {} - {}".format(name, msg)) + #else: + # msg = "" + # if resp: + # try: + # msg = json.loads(resp) + # except ValueError: + # msg = resp + # raise ClientException("failed to update user {} - {}".format(name, msg)) def delete(self, name, force=False): """Deletes an existing OSM user identified by name @@ -192,7 +193,7 @@ class User(object): msg = json.loads(resp) except ValueError: msg = resp - raise ClientException("failed to delete user {} - {}".format(name, msg)) + raise OsmHttpException("failed to delete user {} - {}".format(name, msg)) def list(self, filter=None): """Returns the list of OSM users @@ -202,10 +203,10 @@ class User(object): filter_string = '' if filter: filter_string = '?{}'.format(filter) - resp = self._http.get_cmd('{}{}'.format(self._apiBase,filter_string)) + _, resp = self._http.get2_cmd('{}{}'.format(self._apiBase,filter_string)) #print('RESP: {}'.format(resp)) if resp: - return resp + return json.loads(resp) return list() def get(self, name): diff --git a/osmclient/sol005/vim.py b/osmclient/sol005/vim.py index 43911a6..b6f1614 100644 --- a/osmclient/sol005/vim.py +++ b/osmclient/sol005/vim.py @@ -22,6 +22,7 @@ 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 @@ -94,24 +95,24 @@ class Vim(object): 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 not resp or 'id' not in resp: - raise ClientException('unexpected response from server - {}'.format( - resp)) - if wait: - # Wait for status for VIM instance creation - self._wait(resp.get('id')) - 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)) + #if http_code in (200, 201, 202, 204): + if resp: + resp = json.loads(resp) + if not resp or 'id' not in resp: + raise OsmHttpException('unexpected response from server - {}'.format( + resp)) + if wait: + # Wait for status for VIM instance creation + self._wait(resp.get('id')) + 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, wait=False): self._logger.debug("") @@ -138,23 +139,23 @@ class Vim(object): postfields_dict=vim_account) # print('HTTP CODE: {}'.format(http_code)) # print('RESP: {}'.format(resp)) - 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 - self._wait(wait_id) - else: - pass + #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 + self._wait(wait_id) else: - msg = "" - if resp: - try: - msg = json.loads(resp) - except ValueError: - msg = resp - raise ClientException("failed to update vim {} - {}".format(vim_name, msg)) + pass + #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): self._logger.debug("") @@ -209,7 +210,7 @@ class Vim(object): msg = json.loads(resp) except ValueError: msg = resp - raise ClientException("failed to delete vim {} - {}".format(vim_name, msg)) + raise OsmHttpException("failed to delete vim {} - {}".format(vim_name, msg)) def list(self, filter=None): """Returns a list of VIM accounts @@ -219,11 +220,11 @@ class Vim(object): filter_string = '' if filter: filter_string = '?{}'.format(filter) - resp = self._http.get_cmd('{}{}'.format(self._apiBase,filter_string)) + _, resp = self._http.get2_cmd('{}{}'.format(self._apiBase,filter_string)) if not resp: return list() vim_accounts = [] - for datacenter in resp: + for datacenter in json.loads(resp): vim_accounts.append({"name": datacenter['name'], "uuid": datacenter['_id'] if '_id' in datacenter else None}) return vim_accounts @@ -236,11 +237,12 @@ class Vim(object): vim_id = name if not utils.validate_uuid4(name): vim_id = self.get_id(name) - resp = self._http.get_cmd('{}/{}'.format(self._apiBase,vim_id)) - if not resp or '_id' not in resp: - raise ClientException('failed to get vim info: '.format( - resp)) - else: - return resp + _, 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)) diff --git a/osmclient/sol005/vnf.py b/osmclient/sol005/vnf.py index 6aa44d8..d0c87b8 100644 --- a/osmclient/sol005/vnf.py +++ b/osmclient/sol005/vnf.py @@ -21,7 +21,7 @@ OSM vnf API handling from osmclient.common import utils from osmclient.common.exceptions import NotFound import logging - +import json class Vnf(object): @@ -49,10 +49,10 @@ class Vnf(object): filter_string += ',nsr-id-ref={}'.format(ns_instance['_id']) else: filter_string = '?nsr-id-ref={}'.format(ns_instance['_id']) - resp = self._http.get_cmd('{}{}'.format(self._apiBase,filter_string)) + _, resp = self._http.get2_cmd('{}{}'.format(self._apiBase,filter_string)) #print('RESP: {}'.format(resp)) if resp: - return resp + return json.loads(resp) return list() def get(self, name): @@ -79,9 +79,9 @@ class Vnf(object): if name == vnf['name']: vnf_id = vnf['_id'] break - resp = self._http.get_cmd('{}/{}'.format(self._apiBase, vnf_id)) + _, resp = self._http.get2_cmd('{}/{}'.format(self._apiBase, vnf_id)) #print('RESP: {}'.format(resp)) if resp: - return resp + return json.loads(resp) raise NotFound("vnf {} not found".format(name)) diff --git a/osmclient/sol005/vnfd.py b/osmclient/sol005/vnfd.py index ec54c95..8f0f3c6 100644 --- a/osmclient/sol005/vnfd.py +++ b/osmclient/sol005/vnfd.py @@ -20,6 +20,7 @@ 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 @@ -47,9 +48,9 @@ class Vnfd(object): filter_string = '' if filter: filter_string = '?{}'.format(filter) - resp = self._http.get_cmd('{}{}'.format(self._apiBase,filter_string)) + _, resp = self._http.get2_cmd('{}{}'.format(self._apiBase,filter_string)) if resp: - return resp + return json.loads(resp) return list() def get(self, name): @@ -70,10 +71,10 @@ 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.get_cmd('{}/{}'.format(self._apiBase, vnfd['_id'])) + _, resp = self._http.get2_cmd('{}/{}'.format(self._apiBase, vnfd['_id'])) #print(yaml.safe_dump(resp)) if resp: - return resp + return json.loads(resp) raise NotFound("vnfd {} not found".format(name)) def get_thing(self, name, thing, filename): @@ -84,18 +85,18 @@ class Vnfd(object): http_code, resp = self._http.get2_cmd('{}/{}/{}'.format(self._apiBase, vnfd['_id'], thing)) #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)) + #if http_code in (200, 201, 202, 204): + if resp: + #store in a file + return json.loads(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._logger.debug("") @@ -131,7 +132,7 @@ class Vnfd(object): msg = json.loads(resp) except ValueError: msg = resp - raise ClientException("failed to delete vnfd {} - {}".format(name, msg)) + raise OsmHttpException("failed to delete vnfd {} - {}".format(name, msg)) def create(self, filename, overwrite=None, update_endpoint=None): self._logger.debug("") @@ -177,19 +178,18 @@ class Vnfd(object): if resp: resp = json.loads(resp) if not resp or 'id' not in resp: - raise ClientException('unexpected response from server: '.format( - resp)) + raise OsmHttpException('unexpected response from server: '.format(resp)) print(resp['id']) elif http_code == 204: print('Updated') - else: - msg = "Error {}".format(http_code) - if resp: - try: - msg = "{} - {}".format(msg, json.loads(resp)) - except ValueError: - msg = "{} - {}".format(msg, resp) - raise ClientException("failed to create/update vnfd - {}".format(msg)) + # else: + # msg = "Error {}".format(http_code) + # if resp: + # try: + # msg = "{} - {}".format(msg, json.loads(resp)) + # except ValueError: + # msg = "{} - {}".format(msg, resp) + # raise ClientException("failed to create/update vnfd - {}".format(msg)) def update(self, name, filename): self._logger.debug("") diff --git a/osmclient/sol005/wim.py b/osmclient/sol005/wim.py index 61c4dda..b5a03b8 100644 --- a/osmclient/sol005/wim.py +++ b/osmclient/sol005/wim.py @@ -21,6 +21,7 @@ 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 @@ -88,24 +89,24 @@ class Wim(object): postfields_dict=wim_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 not resp or 'id' not in resp: - raise ClientException('unexpected response from server - {}'.format( - resp)) - if wait: - # Wait for status for WIM instance creation - self._wait(resp.get('id')) - print(resp['id']) - else: - msg = "" - if resp: - try: - msg = json.loads(resp) - except ValueError: - msg = resp - raise ClientException("failed to create wim {} - {}".format(name, msg)) + #if http_code in (200, 201, 202, 204): + if resp: + resp = json.loads(resp) + if not resp or 'id' not in resp: + raise OsmHttpException('unexpected response from server - {}'.format( + resp)) + if wait: + # Wait for status for WIM instance creation + self._wait(resp.get('id')) + print(resp['id']) + #else: + # msg = "" + # if resp: + # try: + # msg = json.loads(resp) + # except ValueError: + # msg = resp + # raise ClientException("failed to create wim {} - {}".format(name, msg)) def update(self, wim_name, wim_account, wim_port_mapping=None, wait=False): self._logger.debug("") @@ -129,23 +130,23 @@ class Wim(object): postfields_dict=wim_account) #print('HTTP CODE: {}'.format(http_code)) #print('RESP: {}'.format(resp)) - 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 = wim_id_for_wait - # Wait for status for WIM instance update - self._wait(wait_id) - else: - pass + #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 = wim_id_for_wait + # Wait for status for WIM instance update + self._wait(wait_id) else: - msg = "" - if resp: - try: - msg = json.loads(resp) - except ValueError: - msg = resp - raise ClientException("failed to update wim {} - {}".format(wim_name, msg)) + pass + #else: + # msg = "" + # if resp: + # try: + # msg = json.loads(resp) + # except ValueError: + # msg = resp + # raise ClientException("failed to update wim {} - {}".format(wim_name, msg)) def update_wim_account_dict(self, wim_account, wim_input): self._logger.debug("") @@ -202,7 +203,7 @@ class Wim(object): msg = json.loads(resp) except ValueError: msg = resp - raise ClientException("failed to delete wim {} - {}".format(wim_name, msg)) + raise OsmHttpException("failed to delete wim {} - {}".format(wim_name, msg)) def list(self, filter=None): """Returns a list of VIM accounts @@ -212,11 +213,11 @@ class Wim(object): filter_string = '' if filter: filter_string = '?{}'.format(filter) - resp = self._http.get_cmd('{}{}'.format(self._apiBase,filter_string)) + _, resp = self._http.get2_cmd('{}{}'.format(self._apiBase,filter_string)) if not resp: return list() wim_accounts = [] - for datacenter in resp: + for datacenter in json.loads(resp): wim_accounts.append({"name": datacenter['name'], "uuid": datacenter['_id'] if '_id' in datacenter else None}) return wim_accounts @@ -229,11 +230,12 @@ class Wim(object): wim_id = name if not utils.validate_uuid4(name): wim_id = self.get_id(name) - resp = self._http.get_cmd('{}/{}'.format(self._apiBase,wim_id)) - if not resp or '_id' not in resp: - raise ClientException('failed to get wim info: '.format( - resp)) - else: - return resp + _, 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: + return json.loads(resp) raise NotFound("wim {} not found".format(name))