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
This commit is contained in:
Mohammed Naser 2020-06-16 15:31:26 -04:00
parent 9754a67d5d
commit 307d23bb58
2 changed files with 22 additions and 0 deletions

View File

@ -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='<host-id>',
help=_("List only ports bound to this host ID"))
parser.add_argument(
'--network',
metavar='<network>',
@ -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)

View File

@ -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