diff --git a/tuskarclient/common/formatting.py b/tuskarclient/common/formatting.py index 7ccf766..d067a0c 100644 --- a/tuskarclient/common/formatting.py +++ b/tuskarclient/common/formatting.py @@ -62,13 +62,14 @@ def print_list(objs, fields, formatters={}, custom_labels={}, sortby=0, def print_dict(d, formatters={}, custom_labels={}, outfile=sys.stdout): - '''Prints a dict. + '''Prints a dict to the provided file or file-like object. :param d: dict to print :param formatters: dict of functions that perform pre-print formatting of dict values (keys are keys from `d` parameter, values are functions that take one parameter - the dict value - to format) + to format). A wild card formatter can be provided as '*' which + will be applied to all fields without a dedicated formatter. :param custom_labels: dict of label overrides for keys (keys are keys from `d` parameter, values are custom labels) ''' @@ -76,10 +77,14 @@ def print_dict(d, formatters={}, custom_labels={}, outfile=sys.stdout): caching=False, print_empty=False) pt.align = 'l' + global_formatter = formatters.get('*') + for field in d.keys(): label = custom_labels.get(field, field) if field in formatters: pt.add_row([label, formatters[field](d[field])]) + elif global_formatter: + pt.add_row([label, global_formatter(d[field])]) else: pt.add_row([label, d[field]]) print(pt.get_string(sortby='Property'), file=outfile) @@ -112,8 +117,8 @@ def attr_proxy(attr, formatter=lambda a: a, allow_undefined=True): def attributes_formatter(attributes): """Given a simple dict format the keyvalue pairs with one on each line. """ - return u"\n".join(u"{0}={1}".format(k, v) for k, v in - sorted(attributes.items())) + return u"".join(u"{0}={1}\n".format(k, v) for k, v in + sorted(attributes.items())) def counts_formatter(counts): diff --git a/tuskarclient/tests/common/test_formatting.py b/tuskarclient/tests/common/test_formatting.py index 6d26626..d21d8cd 100644 --- a/tuskarclient/tests/common/test_formatting.py +++ b/tuskarclient/tests/common/test_formatting.py @@ -84,7 +84,7 @@ class FormattersTest(tutils.TestCase): 'a thing': 'a value' } self.assertEqual( - ("a thing=a value\nmysql_host=http://somewhere\npassword=pass"), + ("a thing=a value\nmysql_host=http://somewhere\npassword=pass\n"), fmt.attributes_formatter(attributes), ) diff --git a/tuskarclient/tests/integration/test_help_command.py b/tuskarclient/tests/integration/test_help_command.py index 4dbb641..cedb360 100644 --- a/tuskarclient/tests/integration/test_help_command.py +++ b/tuskarclient/tests/integration/test_help_command.py @@ -277,6 +277,29 @@ tests = [ ], 'err_string': '', 'return_code': 0, + }, + # Overcloud - show template parameters + { + 'commands': ['overcloud-show-template-parameters -h', + 'overcloud-show-template-parameters --help', + 'help overcloud-show-template-parameters'], + 'test_identifiers': [ + 'test_overcloud_show_template_parameters_dash_h', + 'test_overcloud_show_template_parameters_dashdash_help', + 'test_help_overcloud_show_template_parameters' + ], + 'out_includes': [ + 'Show the template parameters stored in the Tuskar API.' + ], + 'out_excludes': [ + '-n , --name ' + 'overcloud-list', + 'overcloud-role-template-parameters', + '--os-username OS_USERNAME', + 'Display help for ', + ], + 'err_string': '', + 'return_code': 0, } ] diff --git a/tuskarclient/v1/overclouds_shell.py b/tuskarclient/v1/overclouds_shell.py index 0d5ff7c..011e771 100644 --- a/tuskarclient/v1/overclouds_shell.py +++ b/tuskarclient/v1/overclouds_shell.py @@ -88,6 +88,16 @@ def do_overcloud_delete(tuskar, args, outfile=sys.stdout): print(u'Deleted Overcloud "%s".' % overcloud.name, file=outfile) +def do_overcloud_show_template_parameters(tuskar, args, outfile=sys.stdout): + """Show the template parameters stored in the Tuskar API.""" + template_parameters = tuskar.overclouds.template_parameters() + formatters = { + '*': fmt.attributes_formatter + } + template_parameters_dict = template_parameters.to_dict() + fmt.print_dict(template_parameters_dict, formatters, outfile=outfile) + + def create_overcloud_dict(args): """Marshal command line arguments to an API request dict.""" overcloud_dict = {}