diff options
author | Serena Feng <feng.xiaowei@zte.com.cn> | 2018-03-13 07:36:27 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@opnfv.org> | 2018-03-13 07:36:27 +0000 |
commit | 526d3008a1b1ddf0c50cb1b59fcd1c2d2357d6c9 (patch) | |
tree | 2a26fa7981c38002a76495e3025e4bcdb81eb206 | |
parent | 424c55b290faf19983e823f0a36b8a93290bbe7b (diff) | |
parent | 010060d932e236c7eb6802d696c68be8ac0d91ec (diff) |
Merge changes from topics 'format_urls', 'format_output'
* changes:
format URLs
format output
7 files changed, 117 insertions, 57 deletions
diff --git a/testapi/testapi-client/testapiclient/cli/pods.py b/testapi/testapi-client/testapiclient/cli/pods.py index cdedc3e..9cadee7 100644 --- a/testapi/testapi-client/testapiclient/cli/pods.py +++ b/testapi/testapi-client/testapiclient/cli/pods.py @@ -3,15 +3,15 @@ import json from testapiclient.utils import command from testapiclient.utils import http_client as client from testapiclient.utils import identity -from testapiclient.utils import url_parse +from testapiclient.utils import url_parse as up def pods_url(): - return url_parse.resource_join('pods') + return up.resource_join('pods') def pod_url(parsed_args): - return url_parse.path_join(pods_url(), parsed_args.name) + return up.path_join(pods_url(), parsed_args.name) class PodGet(command.Lister): @@ -25,7 +25,17 @@ class PodGet(command.Lister): return parser def take_action(self, parsed_args): - self.show(client.get(self.filter_by_name(pods_url(), parsed_args))) + columns = ( + "name", + "_id", + "creator", + "role", + "mode", + "creation_date", + ) + + data = client.get(up.query_by(pods_url(), 'name', parsed_args)) + return self.format_output(columns, data.get('pods', [])) class PodGetOne(command.ShowOne): @@ -39,10 +49,10 @@ class PodGetOne(command.ShowOne): return parser def take_action(self, parsed_args): - self.show(client.get(pod_url(parsed_args))) + return self.format_output(client.get(pod_url(parsed_args))) -class PodCreate(command.Command): +class PodCreate(command.ShowOne): "Handle post request for pods" def get_parser(self, prog_name): @@ -58,8 +68,7 @@ class PodCreate(command.Command): @identity.authenticate def take_action(self, parsed_args): - self.show('Create', - client.post(pods_url(), parsed_args.pod)) + return self.format_output(client.post(pods_url(), parsed_args.pod)) class PodDelete(command.Command): @@ -74,5 +83,4 @@ class PodDelete(command.Command): @identity.authenticate def take_action(self, parsed_args): - self.show('Delete', - client.delete(pod_url(parsed_args))) + return client.delete(pod_url(parsed_args)) diff --git a/testapi/testapi-client/testapiclient/cli/projects.py b/testapi/testapi-client/testapiclient/cli/projects.py index 113b030..5fa6894 100644 --- a/testapi/testapi-client/testapiclient/cli/projects.py +++ b/testapi/testapi-client/testapiclient/cli/projects.py @@ -3,15 +3,15 @@ import json from testapiclient.utils import command from testapiclient.utils import http_client as client from testapiclient.utils import identity -from testapiclient.utils import url_parse +from testapiclient.utils import url_parse as up def projects_url(): - return url_parse.resource_join('projects') + return up.resource_join('projects') def project_url(parsed_args): - return url_parse.path_join(projects_url(), parsed_args.name) + return up.path_join(projects_url(), parsed_args.name) class ProjectGet(command.Lister): @@ -24,7 +24,14 @@ class ProjectGet(command.Lister): return parser def take_action(self, parsed_args): - self.show(client.get(self.filter_name(projects_url(), parsed_args))) + columns = ( + 'name', + '_id', + 'creator', + 'creation_date' + ) + data = client.get(up.query_by(projects_url(), 'name', parsed_args)) + return self.format_output(columns, data.get('project', [])) class ProjectGetOne(command.ShowOne): @@ -38,10 +45,10 @@ class ProjectGetOne(command.ShowOne): return parser def take_action(self, parsed_args): - self.show(client.get(project_url(parsed_args))) + return self.format_output(client.get(project_url(parsed_args))) -class ProjectCreate(command.Command): +class ProjectCreate(command.ShowOne): def get_parser(self, prog_name): parser = super(ProjectCreate, self).get_parser(prog_name) @@ -54,8 +61,8 @@ class ProjectCreate(command.Command): @identity.authenticate def take_action(self, parsed_args): - self.show('Create', - client.post(projects_url(), parsed_args.project)) + return self.format_output( + client.post(projects_url(), parsed_args.project)) class ProjectDelete(command.Command): @@ -70,11 +77,10 @@ class ProjectDelete(command.Command): @identity.authenticate def take_action(self, parsed_args): - self.show('Delete', - client.delete(project_url(parsed_args))) + return client.delete(project_url(parsed_args)) -class ProjectPut(command.Command): +class ProjectPut(command.ShowOne): def get_parser(self, prog_name): parser = super(ProjectPut, self).get_parser(prog_name) @@ -91,5 +97,5 @@ class ProjectPut(command.Command): @identity.authenticate def take_action(self, parsed_args): - self.show('Update', - client.put(project_url(parsed_args), parsed_args.project)) + return self.format_output( + client.put(project_url(parsed_args), parsed_args.project)) 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..9614acf 100644 --- a/testapi/testapi-client/testapiclient/utils/command.py +++ b/testapi/testapi-client/testapiclient/utils/command.py @@ -1,9 +1,26 @@ +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,30 +29,21 @@ 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(): - return url_parse.query_join(url, name=parsed_args.name) - - return query_url() if parsed_args.name else url + def format_output(columns, data): + return (columns, + (utils.get_item_properties(s, columns) for s in data)) - def show(self, response): - print response.json() if response.status_code < 300 \ - else 'Get failed: {}'.format(response.reason) - -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 diff --git a/testapi/testapi-client/testapiclient/utils/url_parse.py b/testapi/testapi-client/testapiclient/utils/url_parse.py index 08f7a63..fbc503a 100644 --- a/testapi/testapi-client/testapiclient/utils/url_parse.py +++ b/testapi/testapi-client/testapiclient/utils/url_parse.py @@ -20,3 +20,21 @@ def query_join(base, **queries): def resource_join(url): testapi_url = os.environ.get('testapi_url') return path_join(testapi_url, url) + + +def get_queries(queries, parsed_args): + if not isinstance(queries, list): + queries = [queries] + + return {query: getattr(parsed_args, query) + for query in queries + if hasattr(parsed_args, query)} + + +def query_by(base, queries, parsed_args): + return query_join(base, + **get_queries(queries, parsed_args)) + + +def url_format(base, parsed_args): + return base.format(**(parsed_args.__dict__)) |