From 307d23bb58f0f44c9b9facdeaaa10227748ef50d Mon Sep 17 00:00:00 2001 From: Mohammed Naser Date: Tue, 16 Jun 2020 15:31:26 -0400 Subject: [PATCH] port: add --host to list command This adds an option to allow filtering ports bound to a specific host when listing them. Change-Id: I747ed0f8b070074c51789576a158345f670fe733 --- openstackclient/network/v2/port.py | 6 ++++++ .../tests/unit/network/v2/test_port.py | 16 ++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/openstackclient/network/v2/port.py b/openstackclient/network/v2/port.py index a22bcafb56..4011f5f0a2 100644 --- a/openstackclient/network/v2/port.py +++ b/openstackclient/network/v2/port.py @@ -515,6 +515,10 @@ class ListPort(command.Lister): "This is the entity that uses the port (for example, " "network:dhcp).") ) + parser.add_argument( + '--host', + metavar='', + help=_("List only ports bound to this host ID")) parser.add_argument( '--network', metavar='', @@ -603,6 +607,8 @@ class ListPort(command.Lister): server = utils.find_resource(compute_client.servers, parsed_args.server) filters['device_id'] = server.id + if parsed_args.host: + filters['binding:host_id'] = parsed_args.host if parsed_args.network: network = network_client.find_network(parsed_args.network, ignore_missing=False) diff --git a/openstackclient/tests/unit/network/v2/test_port.py b/openstackclient/tests/unit/network/v2/test_port.py index b1a18da6c1..87aea61fac 100644 --- a/openstackclient/tests/unit/network/v2/test_port.py +++ b/openstackclient/tests/unit/network/v2/test_port.py @@ -1054,6 +1054,22 @@ class TestListPort(TestPort): self.assertEqual(self.columns_long, columns) self.assertListItemEqual(self.data_long, list(data)) + def test_port_list_host(self): + arglist = [ + '--host', 'foobar', + ] + verifylist = [ + ('host', 'foobar'), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + filters = {'binding:host_id': 'foobar'} + + self.network.ports.assert_called_once_with(**filters) + self.assertEqual(self.columns, columns) + self.assertListItemEqual(self.data, list(data)) + def test_port_list_project(self): project = identity_fakes.FakeProject.create_one_project() self.projects_mock.get.return_value = project