Add CLI support for the template_parameters action

Change-Id: I26eb71a4707e3a34e644557389016ea0c35019ca
This commit is contained in:
Dougal Matthews 2014-03-03 17:02:43 +00:00
parent 8e0764c37c
commit 2881226f33
4 changed files with 43 additions and 5 deletions

View File

@ -62,13 +62,14 @@ def print_list(objs, fields, formatters={}, custom_labels={}, sortby=0,
def print_dict(d, formatters={}, custom_labels={}, outfile=sys.stdout): 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 d: dict to print
:param formatters: dict of functions that perform pre-print :param formatters: dict of functions that perform pre-print
formatting of dict values (keys are keys from `d` parameter, formatting of dict values (keys are keys from `d` parameter,
values are functions that take one parameter - the dict value 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 :param custom_labels: dict of label overrides for keys (keys are
keys from `d` parameter, values are custom labels) 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) caching=False, print_empty=False)
pt.align = 'l' pt.align = 'l'
global_formatter = formatters.get('*')
for field in d.keys(): for field in d.keys():
label = custom_labels.get(field, field) label = custom_labels.get(field, field)
if field in formatters: if field in formatters:
pt.add_row([label, formatters[field](d[field])]) pt.add_row([label, formatters[field](d[field])])
elif global_formatter:
pt.add_row([label, global_formatter(d[field])])
else: else:
pt.add_row([label, d[field]]) pt.add_row([label, d[field]])
print(pt.get_string(sortby='Property'), file=outfile) 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): def attributes_formatter(attributes):
"""Given a simple dict format the keyvalue pairs with one on each line. """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 return u"".join(u"{0}={1}\n".format(k, v) for k, v in
sorted(attributes.items())) sorted(attributes.items()))
def counts_formatter(counts): def counts_formatter(counts):

View File

@ -84,7 +84,7 @@ class FormattersTest(tutils.TestCase):
'a thing': 'a value' 'a thing': 'a value'
} }
self.assertEqual( 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), fmt.attributes_formatter(attributes),
) )

View File

@ -277,6 +277,29 @@ tests = [
], ],
'err_string': '', 'err_string': '',
'return_code': 0, '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>, --name <NAME>'
'overcloud-list',
'overcloud-role-template-parameters',
'--os-username OS_USERNAME',
'Display help for <subcommand>',
],
'err_string': '',
'return_code': 0,
} }
] ]

View File

@ -88,6 +88,16 @@ def do_overcloud_delete(tuskar, args, outfile=sys.stdout):
print(u'Deleted Overcloud "%s".' % overcloud.name, file=outfile) 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): def create_overcloud_dict(args):
"""Marshal command line arguments to an API request dict.""" """Marshal command line arguments to an API request dict."""
overcloud_dict = {} overcloud_dict = {}