From 6374ee04a378b8a970f9e429d66eb37c969bbf7b Mon Sep 17 00:00:00 2001 From: Sahid Orentino Ferdjaoui Date: Tue, 8 Oct 2013 15:29:41 +0000 Subject: [PATCH] Nova aggregate-details should be more human friendly 'nova aggregate-details' shows Hosts, Availability Zone and Metadata in the python unicode notation. Change-Id: If0028347146439994265c60c429af05dd67912f9 Closes-Bug: #1132961 --- novaclient/tests/test_utils.py | 20 ++++++++++++++++++++ novaclient/utils.py | 5 +++++ novaclient/v1_1/shell.py | 12 +++++++++++- novaclient/v3/shell.py | 11 ++++++++++- 4 files changed, 46 insertions(+), 2 deletions(-) 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 fbe70db7b..4c17f871f 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.')