Add a --number arg to cloudpulse result, default 25

Change-Id: I7aca4b7312ff55c9c7eaa3b6b05322e090883842
This commit is contained in:
Sawan Choudhary 2019-10-23 16:38:13 +05:30
parent 3ec6495652
commit b95ebe3faf
2 changed files with 34 additions and 3 deletions

View File

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

View File

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