diff --git a/doc/source/command-objects/port.rst b/doc/source/command-objects/port.rst index 771f2f8d48..04cf59a2c0 100644 --- a/doc/source/command-objects/port.rst +++ b/doc/source/command-objects/port.rst @@ -116,6 +116,7 @@ List ports os port list [--device-owner ] [--router | --server ] + [--network ] .. option:: --device-owner @@ -130,6 +131,10 @@ List ports List only ports attached to this server (name or ID) +.. option:: --network + + List only ports attached to this network (name or ID) + port set -------- diff --git a/openstackclient/network/v2/port.py b/openstackclient/network/v2/port.py index d7268573dc..0df78e43b3 100644 --- a/openstackclient/network/v2/port.py +++ b/openstackclient/network/v2/port.py @@ -345,6 +345,10 @@ class ListPort(command.Lister): "This is the entity that uses the port (for example, " "network:dhcp).") ) + parser.add_argument( + '--network', + metavar='', + help=_("List only ports connected to this network (name or ID)")) device_group = parser.add_mutually_exclusive_group() device_group.add_argument( '--router', @@ -387,6 +391,10 @@ class ListPort(command.Lister): server = utils.find_resource(compute_client.servers, parsed_args.server) filters['device_id'] = server.id + if parsed_args.network: + network = network_client.find_network(parsed_args.network, + ignore_missing=False) + filters['network_id'] = network.id data = network_client.ports(**filters) diff --git a/openstackclient/tests/unit/network/v2/test_port.py b/openstackclient/tests/unit/network/v2/test_port.py index afa67bbadb..271e816078 100644 --- a/openstackclient/tests/unit/network/v2/test_port.py +++ b/openstackclient/tests/unit/network/v2/test_port.py @@ -337,7 +337,11 @@ class TestListPort(TestPort): fake_router = network_fakes.FakeRouter.create_one_router({ 'id': 'fake-router-id', }) + fake_network = network_fakes.FakeNetwork.create_one_network({ + 'id': 'fake-network-id', + }) self.network.find_router = mock.Mock(return_value=fake_router) + self.network.find_network = mock.Mock(return_value=fake_network) self.app.client_manager.compute = mock.Mock() def test_port_list_no_options(self): @@ -414,11 +418,13 @@ class TestListPort(TestPort): arglist = [ '--device-owner', self._ports[0].device_owner, '--router', 'fake-router-name', + '--network', 'fake-network-name', ] verifylist = [ ('device_owner', self._ports[0].device_owner), - ('router', 'fake-router-name') + ('router', 'fake-router-name'), + ('network', 'fake-network-name') ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -427,7 +433,8 @@ class TestListPort(TestPort): self.network.ports.assert_called_once_with(**{ 'device_owner': self._ports[0].device_owner, - 'device_id': 'fake-router-id' + 'device_id': 'fake-router-id', + 'network_id': 'fake-network-id' }) self.assertEqual(self.columns, columns) self.assertEqual(self.data, list(data)) diff --git a/releasenotes/notes/add-network-list-option-to-ports-9d101344ddeb3e64.yaml b/releasenotes/notes/add-network-list-option-to-ports-9d101344ddeb3e64.yaml new file mode 100644 index 0000000000..478b29a359 --- /dev/null +++ b/releasenotes/notes/add-network-list-option-to-ports-9d101344ddeb3e64.yaml @@ -0,0 +1,7 @@ +--- +features: + - | + Ports can now be listed as per the networks they are + connected to by using the ``--network`` option with + the ``port list`` CLI. + [ Blueprint `network-commands-options `_]