Filter jobs by client and/or custom filters

Closes-bug: 1562015
Change-Id: Ib887226c53d747ae36bef0dea7ade6a7a5e37531
This commit is contained in:
Memo Garcia 2016-03-25 14:53:26 +00:00 committed by Saad Zaher
parent 5270aa2c66
commit 0a0f24f940
7 changed files with 70 additions and 20 deletions

View File

@ -1,4 +1,4 @@
python-freezerclient Style Commandments
===============================================
=======================================
Read the OpenStack Style Commandments http://docs.openstack.org/developer/hacking/

View File

@ -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',

View File

@ -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 {}

View File

@ -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',

View File

@ -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))

View File

@ -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'),

View File

@ -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', [])),