diff --git a/novaclient/tests/test_utils.py b/novaclient/tests/test_utils.py index 8809133ac..0fff7ea55 100644 --- a/novaclient/tests/test_utils.py +++ b/novaclient/tests/test_utils.py @@ -163,3 +163,23 @@ class PrintResultTestCase(test_utils.TestCase): '| k3 | 3 |\n' '| k2 | 2 |\n' '+------+-------+\n') + + def test_pretty_choice_list(self): + l = [] + r = utils.pretty_choice_list(l) + self.assertEqual(r, "") + + l = ["v1", "v2", "v3"] + r = utils.pretty_choice_list(l) + self.assertEqual(r, "'v1', 'v2', 'v3'") + + def test_pretty_choice_dict(self): + d = {} + r = utils.pretty_choice_dict(d) + self.assertEqual(r, "") + + d = {"k1": "v1", + "k2": "v2", + "k3": "v3"} + r = utils.pretty_choice_dict(d) + self.assertEqual(r, "'k1=v1', 'k2=v2', 'k3=v3'") diff --git a/novaclient/utils.py b/novaclient/utils.py index 2020a56f3..cb96992f7 100644 --- a/novaclient/utils.py +++ b/novaclient/utils.py @@ -143,6 +143,11 @@ def pretty_choice_list(l): return ', '.join("'%s'" % i for i in l) +def pretty_choice_dict(d): + """Returns a formatted dict as 'key=value'.""" + return pretty_choice_list(['%s=%s' % (k, d[k]) for k in sorted(d.keys())]) + + def print_list(objs, fields, formatters={}, sortby_index=None): if sortby_index is None: sortby = None diff --git a/novaclient/v1_1/shell.py b/novaclient/v1_1/shell.py index 998d681b5..a50c045c2 100644 --- a/novaclient/v1_1/shell.py +++ b/novaclient/v1_1/shell.py @@ -2687,7 +2687,17 @@ def do_aggregate_details(cs, args): def _print_aggregate_details(aggregate): columns = ['Id', 'Name', 'Availability Zone', 'Hosts', 'Metadata'] - utils.print_list([aggregate], columns) + + parser_metadata = lambda o: utils.pretty_choice_dict( + getattr(o, 'metadata', {}) or {}) + parser_hosts = lambda o: utils.pretty_choice_list( + getattr(o, 'hosts', [])) + + formatters = { + 'Metadata': parser_metadata, + 'Hosts': parser_hosts, + } + utils.print_list([aggregate], columns, formatters=formatters) @utils.arg('server', metavar='', help='Name or ID of server.') diff --git a/novaclient/v3/shell.py b/novaclient/v3/shell.py index 3044e6d9f..55724c2bb 100644 --- a/novaclient/v3/shell.py +++ b/novaclient/v3/shell.py @@ -2517,7 +2517,16 @@ def do_aggregate_details(cs, args): def _print_aggregate_details(aggregate): columns = ['Id', 'Name', 'Availability Zone', 'Hosts', 'Metadata'] - utils.print_list([aggregate], columns) + parser_metadata = lambda o: utils.pretty_choice_dict( + getattr(o, 'metadata', {}) or {}) + parser_hosts = lambda o: utils.pretty_choice_list( + getattr(o, 'hosts', [])) + + formatters = { + 'Metadata': parser_metadata, + 'Hosts': parser_hosts, + } + utils.print_list([aggregate], columns, formatters=formatters) @utils.arg('server', metavar='', help='Name or ID of server.')