From b95ebe3faf9796a8127881d4aeb767e8fd8a9c12 Mon Sep 17 00:00:00 2001 From: Sawan Choudhary <sawchoud@cisco.com> Date: Wed, 23 Oct 2019 16:38:13 +0530 Subject: [PATCH] Add a --number arg to cloudpulse result, default 25 Change-Id: I7aca4b7312ff55c9c7eaa3b6b05322e090883842 --- cloudpulseclient/openstack/common/cliutils.py | 28 ++++++++++++++++++- cloudpulseclient/v1/shell.py | 9 ++++-- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/cloudpulseclient/openstack/common/cliutils.py b/cloudpulseclient/openstack/common/cliutils.py index be9176f..74802a6 100644 --- a/cloudpulseclient/openstack/common/cliutils.py +++ b/cloudpulseclient/openstack/common/cliutils.py @@ -41,6 +41,27 @@ class MissingArgs(Exception): super(MissingArgs, self).__init__(msg) +class InvalidNumber(Exception): + """Supplied argument for --number is invalid""" + def __init__(self): + msg = _("Invalid input, expected a number in range 1<=number<=240") + super(InvalidNumber, self).__init__(msg) + + +def check_int_limit(value): + """Check that supplied arg is of integer type and in range 1<=value<=240""" + try: + int_value = int(value) + except (ValueError, TypeError): + raise InvalidNumber() + + # max_db_entries in cloudpulse is 240, hence this limit + if not 1 <= int_value <= 240: + raise InvalidNumber() + + return int_value + + def validate_args(fn, *args, **kwargs): """Check that the supplied args are sufficient for calling a function. @@ -139,7 +160,7 @@ def isunauthenticated(func): def print_list(objs, fields, formatters=None, sortby_index=0, - mixed_case_fields=None, field_labels=None): + mixed_case_fields=None, field_labels=None, limit_number=25): """Print a list or objects as a table, one row per object. :param objs: iterable of :class:`Resource` @@ -163,6 +184,11 @@ def print_list(objs, fields, formatters=None, sortby_index=0, kwargs = {} else: kwargs = {'sortby': field_labels[sortby_index]} + + # Limit the number to 25 (default) or provided number from user + kwargs['start'] = 0 + kwargs['end'] = limit_number + pt = prettytable.PrettyTable(field_labels) pt.align = 'l' diff --git a/cloudpulseclient/v1/shell.py b/cloudpulseclient/v1/shell.py index 12c8048..f6d52da 100644 --- a/cloudpulseclient/v1/shell.py +++ b/cloudpulseclient/v1/shell.py @@ -29,17 +29,22 @@ def _print_list_field(field): @utils.arg('--period', metavar='<period>', help='List tests results that have been run in the last x minutes.') +@utils.arg('--number', + metavar='<number>', + default=25, + type=utils.check_int_limit, + help='List x number of tests (Max 240).') def do_result(cs, args): """List all the test results""" search_opts = { 'failed': args.failed, - 'period': args.period, + 'period': args.period } healtchecks = cs.healthcheck.list(search_opts=search_opts) columns = ('uuid', 'id', 'name', 'testtype', 'state') utils.print_list(healtchecks, columns, {'versions': _print_list_field('versions')}, - sortby_index=1) + sortby_index=1, limit_number=args.number) @utils.arg('--name',