Skip to content

Commit e4e9fb5

Browse files
author
Brian Haley
committed
Add --subnet-pool to subnet list
The neutron API supports filtering subnets by subnet pool id, but the CLI was missing support for it. Change-Id: Ic230c2c5cda8255d8f2c422880aeac81670b2df3
1 parent 1c84b44 commit e4e9fb5

3 files changed

Lines changed: 57 additions & 0 deletions

File tree

openstackclient/network/v2/subnet.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,12 @@ def get_parser(self, prog_name):
488488
"(in CIDR notation) in output "
489489
"e.g.: --subnet-range 10.10.0.0/16")
490490
)
491+
parser.add_argument(
492+
'--subnet-pool',
493+
metavar='<subnet-pool>',
494+
help=_("List only subnets which belong to a given subnet pool "
495+
"in output (Name or ID)")
496+
)
491497
_tag.add_tag_filtering_option_to_parser(parser, _('subnets'))
492498
return parser
493499

@@ -523,6 +529,10 @@ def take_action(self, parsed_args):
523529
filters['name'] = parsed_args.name
524530
if parsed_args.subnet_range:
525531
filters['cidr'] = parsed_args.subnet_range
532+
if parsed_args.subnet_pool:
533+
subnetpool_id = network_client.find_subnet_pool(
534+
parsed_args.subnet_pool, ignore_missing=False).id
535+
filters['subnetpool_id'] = subnetpool_id
526536
_tag.get_tag_filtering_args(parsed_args, filters)
527537
data = network_client.subnets(**filters)
528538

openstackclient/tests/unit/network/v2/test_subnet.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,48 @@ def test_subnet_list_subnet_range(self):
899899
self.assertEqual(self.columns, columns)
900900
self.assertItemsEqual(self.data, list(data))
901901

902+
def test_subnet_list_subnetpool_by_name(self):
903+
subnet_pool = network_fakes.FakeSubnetPool.create_one_subnet_pool()
904+
subnet = network_fakes.FakeSubnet.create_one_subnet(
905+
{'subnetpool_id': subnet_pool.id})
906+
self.network.find_network = mock.Mock(return_value=subnet)
907+
self.network.find_subnet_pool = mock.Mock(return_value=subnet_pool)
908+
arglist = [
909+
'--subnet-pool', subnet_pool.name,
910+
]
911+
verifylist = [
912+
('subnet_pool', subnet_pool.name),
913+
]
914+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
915+
916+
columns, data = self.cmd.take_action(parsed_args)
917+
filters = {'subnetpool_id': subnet_pool.id}
918+
919+
self.network.subnets.assert_called_once_with(**filters)
920+
self.assertEqual(self.columns, columns)
921+
self.assertItemsEqual(self.data, list(data))
922+
923+
def test_subnet_list_subnetpool_by_id(self):
924+
subnet_pool = network_fakes.FakeSubnetPool.create_one_subnet_pool()
925+
subnet = network_fakes.FakeSubnet.create_one_subnet(
926+
{'subnetpool_id': subnet_pool.id})
927+
self.network.find_network = mock.Mock(return_value=subnet)
928+
self.network.find_subnet_pool = mock.Mock(return_value=subnet_pool)
929+
arglist = [
930+
'--subnet-pool', subnet_pool.id,
931+
]
932+
verifylist = [
933+
('subnet_pool', subnet_pool.id),
934+
]
935+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
936+
937+
columns, data = self.cmd.take_action(parsed_args)
938+
filters = {'subnetpool_id': subnet_pool.id}
939+
940+
self.network.subnets.assert_called_once_with(**filters)
941+
self.assertEqual(self.columns, columns)
942+
self.assertItemsEqual(self.data, list(data))
943+
902944
def test_list_with_tag_options(self):
903945
arglist = [
904946
'--tags', 'red,blue',
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
features:
3+
- |
4+
Add ``--subnet-pool`` option to ``subnet list`` to filter
5+
by subnets by subnet pool.

0 commit comments

Comments
 (0)