From cc4f3dfc9c7d290a4c7140a925a793df8a3e09f6 Mon Sep 17 00:00:00 2001 From: Rafi Khardalian Date: Tue, 19 Aug 2014 15:33:24 -0700 Subject: [PATCH] Enhance network-list to allow --fields Add the ability to specify --fields, using any valid vields, when doing a network-list. This code was shamelessly borrowed from do_list(). This will allow crafting network-list queries which mirror the functionality of 'nova-manage network list', which this query was intended to replace. Change-Id: I7ec7066350cdb979a64d20d14362b12e3ec76496 --- novaclient/tests/v1_1/fakes.py | 2 +- novaclient/tests/v1_1/test_shell.py | 7 +++++++ novaclient/v1_1/shell.py | 17 ++++++++++++++++- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/novaclient/tests/v1_1/fakes.py b/novaclient/tests/v1_1/fakes.py index c3fade4a8..dcf2231bc 100644 --- a/novaclient/tests/v1_1/fakes.py +++ b/novaclient/tests/v1_1/fakes.py @@ -1708,7 +1708,7 @@ class FakeHTTPClient(base_client.HTTPClient): def get_os_networks(self, **kw): return (200, {}, {'networks': [{"label": "1", "cidr": "10.0.0.0/24", 'project_id': '4ffc664c198e435e9853f2538fbcd7a7', - 'id': '1'}]}) + 'id': '1', 'vlan': '1234'}]}) def delete_os_networks_1(self, **kw): return (202, {}, None) diff --git a/novaclient/tests/v1_1/test_shell.py b/novaclient/tests/v1_1/test_shell.py index c167498b6..54821262d 100644 --- a/novaclient/tests/v1_1/test_shell.py +++ b/novaclient/tests/v1_1/test_shell.py @@ -1690,6 +1690,13 @@ class ShellTest(utils.TestCase): self.run_command('network-list') self.assert_called('GET', '/os-networks') + def test_network_list_fields(self): + output = self.run_command('network-list --fields ' + 'vlan,project_id') + self.assert_called('GET', '/os-networks') + self.assertIn('1234', output) + self.assertIn('4ffc664c198e435e9853f2538fbcd7a7', output) + def test_network_show(self): self.run_command('network-show 1') self.assert_called('GET', '/os-networks/1') diff --git a/novaclient/v1_1/shell.py b/novaclient/v1_1/shell.py index 0c4b6d265..61c540dd9 100644 --- a/novaclient/v1_1/shell.py +++ b/novaclient/v1_1/shell.py @@ -779,10 +779,25 @@ def do_scrub(cs, args): cs.security_groups.delete(group) -def do_network_list(cs, _args): +@utils.arg('--fields', + default=None, + metavar='', + help='Comma-separated list of fields to display. ' + 'Use the show command to see which fields are available.') +def do_network_list(cs, args): """Print a list of available networks.""" network_list = cs.networks.list() columns = ['ID', 'Label', 'Cidr'] + + formatters = {} + field_titles = [] + if args.fields: + for field in args.fields.split(','): + field_title, formatter = utils._make_field_formatter(field, {}) + field_titles.append(field_title) + formatters[field_title] = formatter + + columns = columns + field_titles utils.print_list(network_list, columns)