diff --git a/openstackclient/network/v2/port.py b/openstackclient/network/v2/port.py index 68154c3d77..a21324ae27 100644 --- a/openstackclient/network/v2/port.py +++ b/openstackclient/network/v2/port.py @@ -514,6 +514,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='', @@ -602,6 +606,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