diff --git a/doc/source/command-objects/subnet.rst b/doc/source/command-objects/subnet.rst index 8daa251fdc..ff6354e658 100644 --- a/doc/source/command-objects/subnet.rst +++ b/doc/source/command-objects/subnet.rst @@ -147,6 +147,10 @@ List subnets List additional fields in output +.. option:: --ip-version {4, 6} + + List only subnets of given IP version in output + subnet set ---------- diff --git a/openstackclient/network/v2/subnet.py b/openstackclient/network/v2/subnet.py index fb441cbf46..5fc01b642e 100644 --- a/openstackclient/network/v2/subnet.py +++ b/openstackclient/network/v2/subnet.py @@ -296,10 +296,22 @@ class ListSubnet(command.Lister): default=False, help=_("List additional fields in output") ) + parser.add_argument( + '--ip-version', + type=int, + choices=[4, 6], + metavar='', + dest='ip_version', + help=_("List only subnets of given IP version in output" + "Allowed values for IP version are 4 and 6."), + ) return parser def take_action(self, parsed_args): - data = self.app.client_manager.network.subnets() + filters = {} + if parsed_args.ip_version: + filters['ip_version'] = parsed_args.ip_version + data = self.app.client_manager.network.subnets(**filters) headers = ('ID', 'Name', 'Network', 'Subnet') columns = ('id', 'name', 'network_id', 'cidr') diff --git a/openstackclient/tests/network/v2/test_subnet.py b/openstackclient/tests/network/v2/test_subnet.py index 1923286dc2..22c288f9f1 100644 --- a/openstackclient/tests/network/v2/test_subnet.py +++ b/openstackclient/tests/network/v2/test_subnet.py @@ -469,6 +469,22 @@ class TestListSubnet(TestSubnet): self.assertEqual(self.columns_long, columns) self.assertEqual(self.data_long, list(data)) + def test_subnet_list_ip_version(self): + arglist = [ + '--ip-version', str(4), + ] + verifylist = [ + ('ip_version', 4), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + filters = {'ip_version': 4} + + self.network.subnets.assert_called_once_with(**filters) + self.assertEqual(self.columns, columns) + self.assertEqual(self.data, list(data)) + class TestSetSubnet(TestSubnet): diff --git a/releasenotes/notes/bug-1581179-4d15dc504777f9e7.yaml b/releasenotes/notes/bug-1581179-4d15dc504777f9e7.yaml new file mode 100644 index 0000000000..53a52849a3 --- /dev/null +++ b/releasenotes/notes/bug-1581179-4d15dc504777f9e7.yaml @@ -0,0 +1,6 @@ +--- +features: + - + Add the ``--ip-version`` option to the ``subnet list`` command. This + will output subnets based on IP version filter. + [`Bug 1581179 `_]