diff --git a/doc/source/command-objects/subnet.rst b/doc/source/command-objects/subnet.rst index ba80037394..e8740e5348 100644 --- a/doc/source/command-objects/subnet.rst +++ b/doc/source/command-objects/subnet.rst @@ -152,6 +152,7 @@ List subnets os subnet list [--long] [--ip-version {4,6}] + [--dhcp | --no-dhcp] .. option:: --long @@ -162,6 +163,14 @@ List subnets List only subnets of given IP version in output. Allowed values for IP version are 4 and 6. +.. option:: --dhcp + + List subnets which have DHCP enabled + +.. option:: --no-dhcp + + List subnets which have DHCP disabled + subnet set ---------- diff --git a/openstackclient/network/v2/subnet.py b/openstackclient/network/v2/subnet.py index f26f680426..22452809cf 100644 --- a/openstackclient/network/v2/subnet.py +++ b/openstackclient/network/v2/subnet.py @@ -341,12 +341,27 @@ class ListSubnet(command.Lister): help=_("List only subnets of given IP version in output." "Allowed values for IP version are 4 and 6."), ) + dhcp_enable_group = parser.add_mutually_exclusive_group() + dhcp_enable_group.add_argument( + '--dhcp', + action='store_true', + help=_("List subnets which have DHCP enabled") + ) + dhcp_enable_group.add_argument( + '--no-dhcp', + action='store_true', + help=_("List subnets which have DHCP disabled") + ) return parser def take_action(self, parsed_args): filters = {} if parsed_args.ip_version: filters['ip_version'] = parsed_args.ip_version + if parsed_args.dhcp: + filters['enable_dhcp'] = True + elif parsed_args.no_dhcp: + filters['enable_dhcp'] = False data = self.app.client_manager.network.subnets(**filters) headers = ('ID', 'Name', 'Network', 'Subnet') diff --git a/openstackclient/tests/network/v2/test_subnet.py b/openstackclient/tests/network/v2/test_subnet.py index e7f3e7485a..ba757c9835 100644 --- a/openstackclient/tests/network/v2/test_subnet.py +++ b/openstackclient/tests/network/v2/test_subnet.py @@ -584,6 +584,38 @@ class TestListSubnet(TestSubnet): self.assertEqual(self.columns, columns) self.assertEqual(self.data, list(data)) + def test_subnet_list_dhcp(self): + arglist = [ + '--dhcp', + ] + verifylist = [ + ('dhcp', True), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + filters = {'enable_dhcp': True} + + self.network.subnets.assert_called_once_with(**filters) + self.assertEqual(self.columns, columns) + self.assertEqual(self.data, list(data)) + + def test_subnet_list_no_dhcp(self): + arglist = [ + '--no-dhcp', + ] + verifylist = [ + ('no_dhcp', True), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + filters = {'enable_dhcp': False} + + 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-1610883-e6345c32a35cc290.yaml b/releasenotes/notes/bug-1610883-e6345c32a35cc290.yaml new file mode 100644 index 0000000000..710ee39791 --- /dev/null +++ b/releasenotes/notes/bug-1610883-e6345c32a35cc290.yaml @@ -0,0 +1,7 @@ +--- +features: + - | + Make ``subnet list`` command supports listing up subnets with + dhcp enabled/disabled by adding ``--dhcp`` and ``--no-dhcp`` + options to the command. + [Bug `1610883 `_]