From a04012c3d50c3623c699f57d0dd320783b92e1cb Mon Sep 17 00:00:00 2001 From: Tang Chen Date: Sat, 13 Feb 2016 09:49:46 +0800 Subject: [PATCH] Subnet Pool: Add "subnet pool list" command Change-Id: I7935be2488fb728ced9680d75880870e5d315655 Closes-Bug: 1544589 Implements: blueprint neutron-client --- doc/source/command-objects/subnet_pool.rst | 15 ++++ openstackclient/network/v2/subnet_pool.py | 51 ++++++++++++++ openstackclient/tests/network/v2/fakes.py | 6 +- .../tests/network/v2/test_subnet_pool.py | 69 +++++++++++++++++++ .../notes/bug-1544589-b9f669ef71aa5e57.yaml | 4 ++ setup.cfg | 1 + 6 files changed, 145 insertions(+), 1 deletion(-) create mode 100644 releasenotes/notes/bug-1544589-b9f669ef71aa5e57.yaml diff --git a/doc/source/command-objects/subnet_pool.rst b/doc/source/command-objects/subnet_pool.rst index 375fae81e1..4a2f9adc64 100644 --- a/doc/source/command-objects/subnet_pool.rst +++ b/doc/source/command-objects/subnet_pool.rst @@ -19,3 +19,18 @@ Delete subnet pool .. describe:: Subnet pool to delete (name or ID) + +subnet pool list +---------------- + +List subnet pools + +.. program:: subnet pool list +.. code:: bash + + os subnet pool list + [--long] + +.. option:: --long + + List additional fields in output diff --git a/openstackclient/network/v2/subnet_pool.py b/openstackclient/network/v2/subnet_pool.py index 133e3e7197..e82b2050f5 100644 --- a/openstackclient/network/v2/subnet_pool.py +++ b/openstackclient/network/v2/subnet_pool.py @@ -14,6 +14,7 @@ """Subnet pool action implementations""" from openstackclient.common import command +from openstackclient.common import utils class DeleteSubnetPool(command.Command): @@ -32,3 +33,53 @@ class DeleteSubnetPool(command.Command): client = self.app.client_manager.network obj = client.find_subnet_pool(parsed_args.subnet_pool) client.delete_subnet_pool(obj) + + +class ListSubnetPool(command.Lister): + """List subnet pools""" + + def get_parser(self, prog_name): + parser = super(ListSubnetPool, self).get_parser(prog_name) + parser.add_argument( + '--long', + action='store_true', + default=False, + help='List additional fields in output', + ) + return parser + + def take_action(self, parsed_args): + data = self.app.client_manager.network.subnet_pools() + + if parsed_args.long: + headers = ( + 'ID', + 'Name', + 'Prefixes', + 'Default Prefix Length', + 'Address Scope', + ) + columns = ( + 'id', + 'name', + 'prefixes', + 'default_prefixlen', + 'address_scope_id', + ) + else: + headers = ( + 'ID', + 'Name', + 'Prefixes', + ) + columns = ( + 'id', + 'name', + 'prefixes', + ) + + return (headers, + (utils.get_item_properties( + s, columns, + formatters={}, + ) for s in data)) diff --git a/openstackclient/tests/network/v2/fakes.py b/openstackclient/tests/network/v2/fakes.py index b48cde3ebb..dac3737b08 100644 --- a/openstackclient/tests/network/v2/fakes.py +++ b/openstackclient/tests/network/v2/fakes.py @@ -699,6 +699,9 @@ class FakeSubnetPool(object): subnet_pool_attrs = { 'id': 'subnet-pool-id-' + uuid.uuid4().hex, 'name': 'subnet-pool-name-' + uuid.uuid4().hex, + 'prefixes': ['10.0.0.0/24', '10.1.0.0/24'], + 'default_prefixlen': 8, + 'address_scope_id': 'address-scope-id-' + uuid.uuid4().hex, } # Overwrite default attributes. @@ -706,7 +709,8 @@ class FakeSubnetPool(object): # Set default methods. subnet_pool_methods = { - 'keys': ['id', 'name'] + 'keys': ['id', 'name', 'prefixes', 'default_prefixlen', + 'address_scope_id'] } # Overwrite default methods. diff --git a/openstackclient/tests/network/v2/test_subnet_pool.py b/openstackclient/tests/network/v2/test_subnet_pool.py index 0cbfa1eeb8..28be59378b 100644 --- a/openstackclient/tests/network/v2/test_subnet_pool.py +++ b/openstackclient/tests/network/v2/test_subnet_pool.py @@ -55,3 +55,72 @@ class TestDeleteSubnetPool(TestSubnetPool): self.network.delete_subnet_pool.assert_called_with(self._subnet_pool) self.assertIsNone(result) + + +class TestListSubnetPool(TestSubnetPool): + # The subnet pools going to be listed up. + _subnet_pools = network_fakes.FakeSubnetPool.create_subnet_pools(count=3) + + columns = ( + 'ID', + 'Name', + 'Prefixes', + ) + columns_long = columns + ( + 'Default Prefix Length', + 'Address Scope', + ) + + data = [] + for pool in _subnet_pools: + data.append(( + pool.id, + pool.name, + pool.prefixes, + )) + + data_long = [] + for pool in _subnet_pools: + data_long.append(( + pool.id, + pool.name, + pool.prefixes, + pool.default_prefixlen, + pool.address_scope_id, + )) + + def setUp(self): + super(TestListSubnetPool, self).setUp() + + # Get the command object to test + self.cmd = subnet_pool.ListSubnetPool(self.app, self.namespace) + + self.network.subnet_pools = mock.Mock(return_value=self._subnet_pools) + + def test_subnet_pool_list_no_option(self): + arglist = [] + verifylist = [ + ('long', False), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + self.network.subnet_pools.assert_called_with() + self.assertEqual(self.columns, columns) + self.assertEqual(self.data, list(data)) + + def test_subnet_pool_list_long(self): + arglist = [ + '--long', + ] + verifylist = [ + ('long', True), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + self.network.subnet_pools.assert_called_with() + self.assertEqual(self.columns_long, columns) + self.assertEqual(self.data_long, list(data)) diff --git a/releasenotes/notes/bug-1544589-b9f669ef71aa5e57.yaml b/releasenotes/notes/bug-1544589-b9f669ef71aa5e57.yaml new file mode 100644 index 0000000000..eafa8f5ad7 --- /dev/null +++ b/releasenotes/notes/bug-1544589-b9f669ef71aa5e57.yaml @@ -0,0 +1,4 @@ +--- +features: + - Add support for ``subnet pool list`` command. + [Bug `1544589 `_] diff --git a/setup.cfg b/setup.cfg index c289e94b9e..4cc449a022 100644 --- a/setup.cfg +++ b/setup.cfg @@ -342,6 +342,7 @@ openstack.network.v2 = security_group_rule_delete = openstackclient.network.v2.security_group_rule:DeleteSecurityGroupRule subnet_list = openstackclient.network.v2.subnet:ListSubnet subnet_pool_delete = openstackclient.network.v2.subnet_pool:DeleteSubnetPool + subnet_pool_list = openstackclient.network.v2.subnet_pool:ListSubnetPool openstack.object_store.v1 = object_store_account_set = openstackclient.object.v1.account:SetAccount