From 0a0f24f940cd13852f9fa8d64ce3dfe7a7d17f75 Mon Sep 17 00:00:00 2001 From: Memo Garcia Date: Fri, 25 Mar 2016 14:53:26 +0000 Subject: [PATCH] Filter jobs by client and/or custom filters Closes-bug: 1562015 Change-Id: Ib887226c53d747ae36bef0dea7ade6a7a5e37531 --- HACKING.rst | 2 +- freezerclient/shell.py | 3 ++- freezerclient/utils.py | 11 +++++++++++ freezerclient/v1/actions.py | 5 ++++- freezerclient/v1/backups.py | 27 ++++++++++++++++++--------- freezerclient/v1/clients.py | 5 ++++- freezerclient/v1/jobs.py | 37 ++++++++++++++++++++++++++++++------- 7 files changed, 70 insertions(+), 20 deletions(-) diff --git a/HACKING.rst b/HACKING.rst index b1c90df..65d66b8 100644 --- a/HACKING.rst +++ b/HACKING.rst @@ -1,4 +1,4 @@ python-freezerclient Style Commandments -=============================================== +======================================= Read the OpenStack Style Commandments http://docs.openstack.org/developer/hacking/ diff --git a/freezerclient/shell.py b/freezerclient/shell.py index 6ab5d39..d653d27 100644 --- a/freezerclient/shell.py +++ b/freezerclient/shell.py @@ -79,7 +79,8 @@ class FreezerShell(App): ) def build_option_parser(self, description, version): - parser = super(FreezerShell, self).build_option_parser(description, version) + parser = super(FreezerShell, self).build_option_parser(description, + version) parser.add_argument( '--os-auth-url', dest='os_auth_url', diff --git a/freezerclient/utils.py b/freezerclient/utils.py index 1fba574..778da9b 100644 --- a/freezerclient/utils.py +++ b/freezerclient/utils.py @@ -130,3 +130,14 @@ def create_headers_for_request(token): 'Content-Type': 'application/json', 'Accept': 'application/json' } + + +def prepare_search(search_term): + """Return a search term that the api can use to query the db + :param search_term: string + :return: search dict + """ + if search_term: + return {"match": [{"_all": search_term}, ], } + return {} + diff --git a/freezerclient/v1/actions.py b/freezerclient/v1/actions.py index 718c14c..60f30f1 100644 --- a/freezerclient/v1/actions.py +++ b/freezerclient/v1/actions.py @@ -20,6 +20,7 @@ from cliff.show import ShowOne from freezerclient import exceptions from freezerclient.utils import doc_from_json_file +from freezerclient.utils import prepare_search logging = logging.getLogger(__name__) @@ -90,10 +91,12 @@ class ActionList(Lister): return parser def take_action(self, parsed_args): + search = prepare_search(parsed_args.search) + actions = self.app.client.actions.list( limit=parsed_args.limit, offset=parsed_args.offset, - search=parsed_args.search + search=search ) return (('Action ID', 'Name', 'Action', diff --git a/freezerclient/v1/backups.py b/freezerclient/v1/backups.py index 40db38d..73b3623 100644 --- a/freezerclient/v1/backups.py +++ b/freezerclient/v1/backups.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import datetime import logging from pprint import pformat @@ -20,6 +21,7 @@ from cliff.lister import Lister from cliff.show import ShowOne from freezerclient import exceptions +from freezerclient.utils import prepare_search logging = logging.getLogger(__name__) @@ -35,10 +37,11 @@ class BackupShow(ShowOne): def take_action(self, parsed_args): # due to the fact that a backup_id is composed of several strings - # some of them may include a slash "/" so it will never find the correct - # backup, so the workaround for this version is to use the backup_uuid as - # a filter for the search. this won't work when the user wants to delete a - # backup, but that functionality is yet to be provided by the api. + # some of them may include a slash "/" so it will never find the + # correct backup, so the workaround for this version is to use the + # backup_uuid as a filter for the search. this won't work when the + # user wants to delete a backup, but that functionality is yet to be + # provided by the api. search = {"match": [{"backup_uuid": parsed_args.backup_uuid}, ], } backup = self.app.client.backups.list(search=search) @@ -88,11 +91,17 @@ class BackupList(Lister): return parser def take_action(self, parsed_args): + search = prepare_search(parsed_args.search) + backups = self.app.client.backups.list(limit=parsed_args.limit, offset=parsed_args.offset, - search=parsed_args.search) - return (('Backup ID', 'Backup UUID'), - ((backup.get('backup_id'), - backup.get('backup_uuid'), - ) for backup in backups)) + search=search) + return (('Backup UUID', 'Hostname', 'Path', 'Created at', 'Level'), + ((b.get('backup_uuid'), + b.get('backup_metadata', {}).get('hostname'), + b.get('backup_metadata', {}).get('path_to_backup'), + datetime.datetime.fromtimestamp( + int(b.get('backup_metadata', {}).get('time_stamp'))), + b.get('backup_metadata', {}).get('curr_backup_level') + ) for b in backups)) diff --git a/freezerclient/v1/clients.py b/freezerclient/v1/clients.py index 9a0e265..cc8f4a9 100644 --- a/freezerclient/v1/clients.py +++ b/freezerclient/v1/clients.py @@ -20,6 +20,7 @@ from cliff.show import ShowOne from freezerclient import exceptions from freezerclient.utils import doc_from_json_file +from freezerclient.utils import prepare_search logging = logging.getLogger(__name__) @@ -83,9 +84,11 @@ class ClientList(Lister): return parser def take_action(self, parsed_args): + search = prepare_search(parsed_args.search) + clients = self.app.client.clients.list(limit=parsed_args.limit, offset=parsed_args.offset, - search=parsed_args.search) + search=search) return (('Client ID', 'uuid', 'hostname', 'description'), ((client.get('client', {}).get('client_id'), diff --git a/freezerclient/v1/jobs.py b/freezerclient/v1/jobs.py index b16c63b..545f21c 100644 --- a/freezerclient/v1/jobs.py +++ b/freezerclient/v1/jobs.py @@ -23,6 +23,7 @@ from cliff.show import ShowOne from freezerclient import exceptions from freezerclient.utils import doc_from_json_file +from freezerclient.utils import prepare_search logging = logging.getLogger(__name__) @@ -89,19 +90,41 @@ class JobList(Lister): parser.add_argument( '--search', dest='search', - default='', + default={}, help='Define a filter for the query', ) + + parser.add_argument( + '--client', '-C', + dest='client_id', + default='', + help='Get jobs for a specific client', + ) return parser def take_action(self, parsed_args): - jobs = self.app.client.jobs.list_all( - limit=parsed_args.limit, - offset=parsed_args.offset, - search=parsed_args.search - ) - return (('Job ID', 'Description', '# Actions', 'Result', 'Event', 'Session ID'), + search = prepare_search(parsed_args.search) + + if parsed_args.client_id: + jobs = self.app.client.jobs.list( + limit=parsed_args.limit, + offset=parsed_args.offset, + search=search, + client_id=parsed_args.client_id + ) + else: + jobs = self.app.client.jobs.list_all( + limit=parsed_args.limit, + offset=parsed_args.offset, + search=search + ) + + if not jobs: + print('No jobs') + + return (('Job ID', 'Description', '# Actions', 'Result', 'Event', + 'Session ID'), ((job.get('job_id'), job.get('description'), len(job.get('job_actions', [])),