From f157128052e59df5c37489926f7d5755714c3cbf Mon Sep 17 00:00:00 2001 From: garciadeblas Date: Mon, 14 May 2018 16:06:22 +0200 Subject: [PATCH] proper handling of http_codes in http.post_cmd Change-Id: I7526522f11f2c7c991c61e0a8bbe99393bddbc85 Signed-off-by: garciadeblas --- osmclient/sol005/client.py | 6 +++++- osmclient/sol005/http.py | 21 +++++---------------- osmclient/sol005/ns.py | 24 +++++++++++++++++------- osmclient/sol005/nsd.py | 13 ++++++++----- osmclient/sol005/package.py | 5 ++++- osmclient/sol005/sdncontroller.py | 11 ++++++++--- osmclient/sol005/vim.py | 11 ++++++++--- osmclient/sol005/vnfd.py | 13 ++++++++----- 8 files changed, 63 insertions(+), 41 deletions(-) diff --git a/osmclient/sol005/client.py b/osmclient/sol005/client.py index d1c90aa..b8627b9 100644 --- a/osmclient/sol005/client.py +++ b/osmclient/sol005/client.py @@ -28,6 +28,7 @@ from osmclient.sol005 import package from osmclient.sol005 import http from osmclient.sol005 import sdncontroller from osmclient.common.exceptions import ClientException +import json class Client(object): @@ -94,8 +95,11 @@ class Client(object): postfields_dict = {'username': self._user, 'password': self._password, 'project-id': self._project} - token = self._http_client.post_cmd(endpoint=self._auth_endpoint, + 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): + raise ClientException(resp) + token = json.loads(resp) if resp else None if token is not None: return token['_id'] return None diff --git a/osmclient/sol005/http.py b/osmclient/sol005/http.py index 60f0a49..8e80ba9 100644 --- a/osmclient/sol005/http.py +++ b/osmclient/sol005/http.py @@ -84,21 +84,10 @@ class Http(http.Http): curl_cmd.perform() http_code = curl_cmd.getinfo(pycurl.HTTP_CODE) curl_cmd.close() - if http_code not in (200, 201, 202, 204): - raise ClientException(data.getvalue().decode()) - if postfields_dict is not None: - if data.getvalue(): - return json.loads(data.getvalue().decode()) - return None - elif formfile is not None: - if data.getvalue(): - return yaml.safe_load(data.getvalue().decode()) - return None - elif filename is not None: - if data.getvalue(): - return yaml.safe_load(data.getvalue().decode()) - return None - return None + if data.getvalue(): + return http_code, data.getvalue().decode() + else: + return http_code, None def post_cmd(self, endpoint='', postfields_dict=None, formfile=None, filename=None): @@ -133,6 +122,6 @@ class Http(http.Http): http_code = curl_cmd.getinfo(pycurl.HTTP_CODE) curl_cmd.close() if data.getvalue(): - return http_code, data.getvalue() + return http_code, data.getvalue().decode() return http_code, None diff --git a/osmclient/sol005/ns.py b/osmclient/sol005/ns.py index 73c5973..945e622 100644 --- a/osmclient/sol005/ns.py +++ b/osmclient/sol005/ns.py @@ -148,8 +148,10 @@ class Ns(object): self._apiResource = '/ns_instances_content' self._apiBase = '{}{}{}'.format(self._apiName, self._apiVersion, self._apiResource) - resp = self._http.post_cmd(endpoint=self._apiBase, + http_code, resp = self._http.post_cmd(endpoint=self._apiBase, postfields_dict=ns) + if resp: + resp = json.loads(resp) #print 'RESP: {}'.format(resp) if not resp or 'id' not in resp: raise ClientException('unexpected response from server: '.format( @@ -176,7 +178,7 @@ class Ns(object): filter_string = '&{}'.format(filter) http_code, resp = self._http.get2_cmd('{}?nsInstanceId={}'.format(self._apiBase, ns['_id'], filter_string) ) - resp = json.loads(resp.decode()) + resp = json.loads(resp) #print 'RESP: {}'.format(resp) if http_code == 200: return resp @@ -197,7 +199,7 @@ 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.decode()) + resp = json.loads(resp) #print 'RESP: {}'.format(resp) if http_code == 200: return resp @@ -220,7 +222,9 @@ class Ns(object): endpoint = '{}/{}/{}'.format(self._apiBase, ns['_id'], op_name) #print 'OP_NAME: {}'.format(op_name) #print 'OP_DATA: {}'.format(json.dumps(op_data)) - resp = self._http.post_cmd(endpoint=endpoint, postfields_dict=op_data) + http_code, resp = self._http.post_cmd(endpoint=endpoint, postfields_dict=op_data) + if resp: + resp = json.loads(resp) #print 'RESP: {}'.format(resp) if not resp or 'id' not in resp: raise ClientException('unexpected response from server: '.format( @@ -241,8 +245,10 @@ class Ns(object): data["create_alarm_request"] = {} data["create_alarm_request"]["alarm_create_request"] = alarm try: - resp = self._http.post_cmd(endpoint='/test/message/alarm_request', + http_code, resp = self._http.post_cmd(endpoint='/test/message/alarm_request', postfields_dict=data) + if resp: + resp = json.loads(resp) #print 'RESP: {}'.format(resp) if not resp: raise ClientException('unexpected response from server: '.format( @@ -260,8 +266,10 @@ class Ns(object): data["delete_alarm_request"]["alarm_delete_request"] = {} data["delete_alarm_request"]["alarm_delete_request"]["alarm_uuid"] = name try: - resp = self._http.post_cmd(endpoint='/test/message/alarm_request', + http_code, resp = self._http.post_cmd(endpoint='/test/message/alarm_request', postfields_dict=data) + if resp: + resp = json.loads(resp) #print 'RESP: {}'.format(resp) if not resp: raise ClientException('unexpected response from server: '.format( @@ -280,8 +288,10 @@ class Ns(object): data = {} data["read_metric_data_request"] = metric try: - resp = self._http.post_cmd(endpoint='/test/message/metric_request', + http_code, resp = self._http.post_cmd(endpoint='/test/message/metric_request', postfields_dict=data) + if resp: + resp = json.loads(resp) #print 'RESP: {}'.format(resp) if not resp: raise ClientException('unexpected response from server: '.format( diff --git a/osmclient/sol005/nsd.py b/osmclient/sol005/nsd.py index 95fc92a..4ae755b 100644 --- a/osmclient/sol005/nsd.py +++ b/osmclient/sol005/nsd.py @@ -22,6 +22,7 @@ from osmclient.common.exceptions import NotFound from osmclient.common.exceptions import ClientException from osmclient.common import utils import yaml +import json import magic #from os import stat #from os.path import basename @@ -73,11 +74,11 @@ class Nsd(object): nsd = self.get(name) headers = self._client._headers headers['Accept'] = 'application/binary' - http_code, resp2 = self._http.get2_cmd('{}/{}/{}'.format(self._apiBase, nsd['_id'], thing)) + http_code, resp = self._http.get2_cmd('{}/{}/{}'.format(self._apiBase, nsd['_id'], thing)) #print yaml.safe_dump(resp2) - if resp2: + if resp: #store in a file - return resp2 + return resp raise NotFound("nsd {} not found".format(name)) def get_descriptor(self, name, filename): @@ -125,7 +126,7 @@ class Nsd(object): for (key,val) in headers.items()] self._http.set_http_header(http_header) if update_endpoint: - resp = self._http.put_cmd(endpoint=update_endpoint, filename=filename) + http_code, resp = self._http.put_cmd(endpoint=update_endpoint, filename=filename) else: ow_string = '' if overwrite: @@ -134,7 +135,9 @@ class Nsd(object): self._apiBase = '{}{}{}'.format(self._apiName, self._apiVersion, self._apiResource) endpoint = '{}{}'.format(self._apiBase,ow_string) - resp = self._http.post_cmd(endpoint=endpoint, filename=filename) + 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") diff --git a/osmclient/sol005/package.py b/osmclient/sol005/package.py index 9fd734a..b2fe035 100644 --- a/osmclient/sol005/package.py +++ b/osmclient/sol005/package.py @@ -26,6 +26,7 @@ import yaml from osmclient.common.exceptions import ClientException from osmclient.common.exceptions import NotFound from osmclient.common import utils +import json class Package(object): @@ -57,7 +58,9 @@ class Package(object): http_header = ['{}: {}'.format(key,val) for (key,val) in headers.items()] self._http.set_http_header(http_header) - resp = self._http.post_cmd(endpoint=endpoint, filename=filename) + 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") diff --git a/osmclient/sol005/sdncontroller.py b/osmclient/sol005/sdncontroller.py index 36605eb..0639559 100644 --- a/osmclient/sol005/sdncontroller.py +++ b/osmclient/sol005/sdncontroller.py @@ -21,7 +21,8 @@ OSM SDN controller API handling from osmclient.common import utils from osmclient.common.exceptions import ClientException from osmclient.common.exceptions import NotFound -import yaml +import yaml +import json class SdnController(object): @@ -34,8 +35,10 @@ class SdnController(object): self._apiBase = '{}{}{}'.format(self._apiName, self._apiVersion, self._apiResource) def create(self, name, sdn_controller): - resp = self._http.post_cmd(endpoint=self._apiBase, + http_code, resp = self._http.post_cmd(endpoint=self._apiBase, postfields_dict=sdn_controller) + if resp: + resp = json.loads(resp) #print 'RESP: {}'.format(resp) if not resp or 'id' not in resp: raise ClientException('failed to create SDN controller: '.format( @@ -45,8 +48,10 @@ class SdnController(object): def update(self, name, sdn_controller): sdnc = self.get(name) - resp = self._http.patch_cmd(endpoint='{}/{}'.format(self._apiBase,sdnc['_id']), + http_code, resp = self._http.patch_cmd(endpoint='{}/{}'.format(self._apiBase,sdnc['_id']), postfields_dict=sdn_controller) + if resp: + resp = json.loads(resp) print 'RESP: {}'.format(resp) if not resp or 'id' not in resp: raise ClientException('failed to update SDN controller: '.format( diff --git a/osmclient/sol005/vim.py b/osmclient/sol005/vim.py index d540c63..8aee453 100644 --- a/osmclient/sol005/vim.py +++ b/osmclient/sol005/vim.py @@ -22,6 +22,7 @@ from osmclient.common import utils from osmclient.common.exceptions import ClientException from osmclient.common.exceptions import NotFound import yaml +import json class Vim(object): @@ -49,8 +50,10 @@ class Vim(object): vim_account['config'] = vim_config - resp = self._http.post_cmd(endpoint=self._apiBase, + http_code, resp = self._http.post_cmd(endpoint=self._apiBase, postfields_dict=vim_account) + if resp: + resp = json.loads(resp) if not resp or 'id' not in resp: raise ClientException('failed to create vim {}: {}'.format( name, resp)) @@ -59,9 +62,11 @@ class Vim(object): def update(self, vim_name, vim_account): vim = self.get(vim_name) - #resp = self._http.put_cmd(endpoint='{}/{}'.format(self._apiBase,vim['_id']), - resp = self._http.patch_cmd(endpoint='{}/{}'.format(self._apiBase,vim['_id']), + #http_code, resp = self._http.put_cmd(endpoint='{}/{}'.format(self._apiBase,vim['_id']), + http_code, resp = self._http.patch_cmd(endpoint='{}/{}'.format(self._apiBase,vim['_id']), postfields_dict=vim_account) + if resp: + resp = json.loads(resp) #print 'RESP: {}'.format(resp) if not resp or 'id' not in resp: raise ClientException('failed to update vim: '.format(resp)) diff --git a/osmclient/sol005/vnfd.py b/osmclient/sol005/vnfd.py index 6b09a1f..cc5f04a 100644 --- a/osmclient/sol005/vnfd.py +++ b/osmclient/sol005/vnfd.py @@ -22,6 +22,7 @@ from osmclient.common.exceptions import NotFound from osmclient.common.exceptions import ClientException from osmclient.common import utils import yaml +import json import magic #from os import stat #from os.path import basename @@ -72,11 +73,11 @@ class Vnfd(object): vnfd = self.get(name) headers = self._client._headers headers['Accept'] = 'application/binary' - http_code, resp2 = self._http.get2_cmd('{}/{}/{}'.format(self._apiBase, vnfd['_id'], thing)) + http_code, resp = self._http.get2_cmd('{}/{}/{}'.format(self._apiBase, vnfd['_id'], thing)) #print yaml.safe_dump(resp2) - if resp2: + if resp: #store in a file - return resp2 + return resp raise NotFound("vnfd {} not found".format(name)) def get_descriptor(self, name, filename): @@ -124,7 +125,7 @@ class Vnfd(object): for (key,val) in headers.items()] self._http.set_http_header(http_header) if update_endpoint: - resp = self._http.put_cmd(endpoint=update_endpoint, filename=filename) + http_code, resp = self._http.put_cmd(endpoint=update_endpoint, filename=filename) else: ow_string = '' if overwrite: @@ -133,7 +134,9 @@ class Vnfd(object): self._apiBase = '{}{}{}'.format(self._apiName, self._apiVersion, self._apiResource) endpoint = '{}{}'.format(self._apiBase,ow_string) - resp = self._http.post_cmd(endpoint=endpoint, filename=filename) + 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") -- 2.25.1