diff options
author | SerenaFeng <feng.xiaowei@zte.com.cn> | 2018-03-12 21:05:51 +0800 |
---|---|---|
committer | SerenaFeng <feng.xiaowei@zte.com.cn> | 2018-03-13 10:08:32 +0800 |
commit | 12b7a2b54f4e3c36d49dec2b2620826aa7029a3e (patch) | |
tree | 000db4ce860a939214c218f9961fceb2380905c7 /testapi/testapi-client/testapiclient/utils | |
parent | c22ede1f489a6b8df123c657a6a0001103eb7ba4 (diff) |
format output
JIRA: RELENG-348
Change-Id: I8f6edae6ed70542f5dde45d81601c35d32af96d9
Signed-off-by: SerenaFeng <feng.xiaowei@zte.com.cn>
Diffstat (limited to 'testapi/testapi-client/testapiclient/utils')
4 files changed, 63 insertions, 27 deletions
diff --git a/testapi/testapi-client/testapiclient/utils/__init__.py b/testapi/testapi-client/testapiclient/utils/__init__.py index e69de29..ebb891f 100644 --- a/testapi/testapi-client/testapiclient/utils/__init__.py +++ b/testapi/testapi-client/testapiclient/utils/__init__.py @@ -0,0 +1,8 @@ +def get_item_properties(item, fields): + """Return a tuple containing the item properties. + + :param item: a single item resource (e.g. Server, Project, etc) + :param fields: tuple of strings with the desired field names + """ + + return tuple([item.get(field, '') for field in fields]) diff --git a/testapi/testapi-client/testapiclient/utils/command.py b/testapi/testapi-client/testapiclient/utils/command.py index f9c75a7..c99a3f3 100644 --- a/testapi/testapi-client/testapiclient/utils/command.py +++ b/testapi/testapi-client/testapiclient/utils/command.py @@ -1,9 +1,27 @@ +import abc +import logging + from cliff import command +from cliff import lister +from cliff import show +import six +from testapiclient import utils from testapiclient.utils import url_parse +class CommandMeta(abc.ABCMeta): + + def __new__(mcs, name, bases, cls_dict): + if 'log' not in cls_dict: + cls_dict['log'] = logging.getLogger( + cls_dict['__module__'] + '.' + name) + return super(CommandMeta, mcs).__new__(mcs, name, bases, cls_dict) + + +@six.add_metaclass(CommandMeta) class Command(command.Command): + def get_parser(self, prog_name): parser = super(Command, self).get_parser(prog_name) parser.add_argument('-u', @@ -12,17 +30,14 @@ class Command(command.Command): parser.add_argument('-p', type=str, help='Password for authentication') - return parser - def show(self, request, response): - print ' '.join([request, - 'success' if response.status_code < 300 - else 'failed: {}'.format(response.reason)]) - + def run(self, parsed_args): + self.log.debug('run(%s)', parsed_args) + return super(Command, self).run(parsed_args) -class Lister(command.Command): +class Lister(Command, lister.Lister): @staticmethod def filter_by_name(url, parsed_args): def query_url(): @@ -30,12 +45,13 @@ class Lister(command.Command): return query_url() if parsed_args.name else url - def show(self, response): - print response.json() if response.status_code < 300 \ - else 'Get failed: {}'.format(response.reason) + @staticmethod + def format_output(columns, data): + return (columns, + (utils.get_item_properties(s, columns) for s in data)) -class ShowOne(command.Command): - def show(self, response): - print response.json() if response.status_code < 300 \ - else 'Get failed: {}'.format(response.reason) +class ShowOne(Command, show.ShowOne): + @staticmethod + def format_output(body): + return zip(*sorted(six.iteritems(body))) diff --git a/testapi/testapi-client/testapiclient/utils/http_client.py b/testapi/testapi-client/testapiclient/utils/http_client.py index 6be33ee..359dd14 100644 --- a/testapi/testapi-client/testapiclient/utils/http_client.py +++ b/testapi/testapi-client/testapiclient/utils/http_client.py @@ -1,3 +1,4 @@ +import httplib import json import requests @@ -25,31 +26,42 @@ class HTTPClient(object): HTTPClient.__instance = self def get(self, url): - return requests.get(url) + return self._parse_response('Get', requests.get(url)) def post(self, url, data): - return self._request('post', url, - data=json.dumps(data), - headers=self.headers) + return self._parse_response('Create', + self._request('post', url, + data=json.dumps(data), + headers=self.headers)) def put(self, url, data): - return self._request('put', url, - data=json.dumps(data), - headers=self.headers) + return self._parse_response('Update', + self._request('put', url, + data=json.dumps(data), + headers=self.headers)) def delete(self, url, *args): data = json.dumps(args[0]) if len(args) > 0 else None - return self._request('delete', url, - data=data, - headers=self.headers) + return self._parse_response('Delete', + self._request('delete', url, + data=data, + headers=self.headers)) def _request(self, method, *args, **kwargs): return getattr(user.User.session, method)(*args, **kwargs) + def _raise_failure(self, op, response): + raise Exception('{} failed: {}'.format(op, response.reason)) + + def _parse_response(self, op, response): + if response.status_code == httplib.OK: + return response.json() if op != 'Delete' else None + else: + self._raise_failure(op, response) + def _request(method, *args, **kwargs): - client = HTTPClient.get_Instance() - return getattr(client, method)(*args, **kwargs) + return getattr(HTTPClient.get_Instance(), method)(*args, **kwargs) def get(url): diff --git a/testapi/testapi-client/testapiclient/utils/identity.py b/testapi/testapi-client/testapiclient/utils/identity.py index 2aeb87a..a00dd87 100644 --- a/testapi/testapi-client/testapiclient/utils/identity.py +++ b/testapi/testapi-client/testapiclient/utils/identity.py @@ -34,5 +34,5 @@ def authenticate(xstep): if "login" in response.text: print "Authentication has failed." return - xstep(self, parsed_args) + return xstep(self, parsed_args) return wrapper |