summaryrefslogtreecommitdiffstats
path: root/testapi/testapi-client/testapiclient
diff options
context:
space:
mode:
authorSerenaFeng <feng.xiaowei@zte.com.cn>2018-03-12 21:05:51 +0800
committerSerenaFeng <feng.xiaowei@zte.com.cn>2018-03-13 10:08:32 +0800
commit12b7a2b54f4e3c36d49dec2b2620826aa7029a3e (patch)
tree000db4ce860a939214c218f9961fceb2380905c7 /testapi/testapi-client/testapiclient
parentc22ede1f489a6b8df123c657a6a0001103eb7ba4 (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')
-rw-r--r--testapi/testapi-client/testapiclient/cli/pods.py24
-rw-r--r--testapi/testapi-client/testapiclient/cli/projects.py28
-rw-r--r--testapi/testapi-client/testapiclient/utils/__init__.py8
-rw-r--r--testapi/testapi-client/testapiclient/utils/command.py44
-rw-r--r--testapi/testapi-client/testapiclient/utils/http_client.py36
-rw-r--r--testapi/testapi-client/testapiclient/utils/identity.py2
6 files changed, 98 insertions, 44 deletions
diff --git a/testapi/testapi-client/testapiclient/cli/pods.py b/testapi/testapi-client/testapiclient/cli/pods.py
index cdedc3e..8b2f3af 100644
--- a/testapi/testapi-client/testapiclient/cli/pods.py
+++ b/testapi/testapi-client/testapiclient/cli/pods.py
@@ -25,7 +25,19 @@ 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(
+ self.filter_by_name(pods_url(), parsed_args)).get('pods', [])
+
+ return self.format_output(columns, data)
class PodGetOne(command.ShowOne):
@@ -39,10 +51,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 +70,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 +85,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..94a8fac 100644
--- a/testapi/testapi-client/testapiclient/cli/projects.py
+++ b/testapi/testapi-client/testapiclient/cli/projects.py
@@ -24,7 +24,16 @@ 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(
+ self.filter_by_name(projects_url(),
+ parsed_args)).get('projects', [])
+ return self.format_output(columns, data)
class ProjectGetOne(command.ShowOne):
@@ -38,10 +47,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 +63,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 +79,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 +99,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..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