diff --git a/doc/source/command-objects/port.rst b/doc/source/command-objects/port.rst index e4cf2cd212..0a74f88c7d 100644 --- a/doc/source/command-objects/port.rst +++ b/doc/source/command-objects/port.rst @@ -45,7 +45,8 @@ Create new port .. option:: --device-owner - Device owner of this port + Device owner of this port. This is the entity that uses + the port (for example, network:dhcp). .. option:: --vnic-type @@ -112,8 +113,14 @@ List ports .. code:: bash os port list + [--device-owner ] [--router ] +.. option:: --device-owner + + List only ports with the specified device owner. This is + the entity that uses the port (for example, network:dhcp). + .. option:: --router List only ports attached to this router (name or ID) @@ -153,7 +160,8 @@ Set port properties .. option:: --device-owner - Device owner of this port + Device owner of this port. This is the entity that uses + the port (for example, network:dhcp). .. option:: --vnic-type diff --git a/openstackclient/network/v2/port.py b/openstackclient/network/v2/port.py index 1c5db706e3..71dc108340 100644 --- a/openstackclient/network/v2/port.py +++ b/openstackclient/network/v2/port.py @@ -169,7 +169,8 @@ def _add_updatable_args(parser): parser.add_argument( '--device-owner', metavar='', - help=_("Device owner of this port") + help=_("Device owner of this port. This is the entity that uses " + "the port (for example, network:dhcp).") ) parser.add_argument( '--vnic-type', @@ -309,6 +310,13 @@ class ListPort(command.Lister): def get_parser(self, prog_name): parser = super(ListPort, self).get_parser(prog_name) + parser.add_argument( + '--device-owner', + metavar='', + help=_("List only ports with the specified device owner. " + "This is the entity that uses the port (for example, " + "network:dhcp).") + ) parser.add_argument( '--router', metavar='', @@ -334,10 +342,12 @@ class ListPort(command.Lister): ) filters = {} + if parsed_args.device_owner is not None: + filters['device_owner'] = parsed_args.device_owner if parsed_args.router: _router = client.find_router(parsed_args.router, ignore_missing=False) - filters = {'device_id': _router.id} + filters['device_id'] = _router.id data = client.ports(**filters) diff --git a/openstackclient/tests/network/v2/test_port.py b/openstackclient/tests/network/v2/test_port.py index 779dca0519..8da6ec108b 100644 --- a/openstackclient/tests/network/v2/test_port.py +++ b/openstackclient/tests/network/v2/test_port.py @@ -316,6 +316,47 @@ class TestListPort(TestPort): self.assertEqual(self.columns, columns) self.assertEqual(self.data, list(data)) + def test_port_list_device_owner_opt(self): + arglist = [ + '--device-owner', self._ports[0].device_owner, + ] + + verifylist = [ + ('device_owner', self._ports[0].device_owner) + ] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + self.network.ports.assert_called_once_with(**{ + 'device_owner': self._ports[0].device_owner + }) + self.assertEqual(self.columns, columns) + self.assertEqual(self.data, list(data)) + + def test_port_list_all_opt(self): + arglist = [ + '--device-owner', self._ports[0].device_owner, + '--router', 'fake-router-name', + ] + + verifylist = [ + ('device_owner', self._ports[0].device_owner), + ('router', 'fake-router-name') + ] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + self.network.ports.assert_called_once_with(**{ + 'device_owner': self._ports[0].device_owner, + 'device_id': 'fake-router-id' + }) + self.assertEqual(self.columns, columns) + self.assertEqual(self.data, list(data)) + class TestSetPort(TestPort): diff --git a/releasenotes/notes/bp-neutron-client-a0552f8ca909b665.yaml b/releasenotes/notes/bp-neutron-client-a0552f8ca909b665.yaml index f8de4ee6ae..3665dd8122 100644 --- a/releasenotes/notes/bp-neutron-client-a0552f8ca909b665.yaml +++ b/releasenotes/notes/bp-neutron-client-a0552f8ca909b665.yaml @@ -3,3 +3,6 @@ features: - Add ``geneve`` choice to the ``network create`` command ``--provider-network-type`` option. [Blueprint :oscbp:`neutron-client`] + - Add ``--device-owner`` option to the ``port list`` command + to enable listing ports based on device owner. + [Blueprint :oscbp:`neutron-client`]