Add option to allow filtering by router on port list

Added support to allow filtering ports via --router
option to list ports that are applicable to specific router.

Partial-bug: #1519909
Partially-implements: blueprint neutron-client

Change-Id: I6dd958603909f641735c821a62fc0d45afd5c7ec
This commit is contained in:
Jas 2016-03-11 13:02:47 -06:00
parent f8ac17ac52
commit 62a02466c3
3 changed files with 46 additions and 2 deletions

View File

@ -108,6 +108,11 @@ List ports
.. code:: bash .. code:: bash
os port list os port list
[--router <router>]
.. option:: --router <router>
List only ports attached to this router (name or ID)
port set port set
-------- --------

View File

@ -243,6 +243,16 @@ class DeletePort(command.Command):
class ListPort(command.Lister): class ListPort(command.Lister):
"""List ports""" """List ports"""
def get_parser(self, prog_name):
parser = super(ListPort, self).get_parser(prog_name)
parser.add_argument(
'--router',
metavar='<router>',
dest='router',
help='List only ports attached to this router (name or ID)',
)
return parser
def take_action(self, parsed_args): def take_action(self, parsed_args):
client = self.app.client_manager.network client = self.app.client_manager.network
@ -259,7 +269,14 @@ class ListPort(command.Lister):
'Fixed IP Addresses', 'Fixed IP Addresses',
) )
data = client.ports() filters = {}
if parsed_args.router:
_router = client.find_router(parsed_args.router,
ignore_missing=False)
filters = {'device_id': _router.id}
data = client.ports(**filters)
return (column_headers, return (column_headers,
(utils.get_item_properties( (utils.get_item_properties(
s, columns, s, columns,

View File

@ -224,8 +224,11 @@ class TestListPort(TestPort):
# Get the command object to test # Get the command object to test
self.cmd = port.ListPort(self.app, self.namespace) self.cmd = port.ListPort(self.app, self.namespace)
self.network.ports = mock.Mock(return_value=self._ports) self.network.ports = mock.Mock(return_value=self._ports)
fake_router = network_fakes.FakeRouter.create_one_router({
'id': 'fake-router-id',
})
self.network.find_router = mock.Mock(return_value=fake_router)
def test_port_list_no_options(self): def test_port_list_no_options(self):
arglist = [] arglist = []
@ -239,6 +242,25 @@ class TestListPort(TestPort):
self.assertEqual(self.columns, columns) self.assertEqual(self.columns, columns)
self.assertEqual(self.data, list(data)) self.assertEqual(self.data, list(data))
def test_port_list_router_opt(self):
arglist = [
'--router', 'fake-router-name',
]
verifylist = [
('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_with(**{
'device_id': 'fake-router-id'
})
self.assertEqual(self.columns, columns)
self.assertEqual(self.data, list(data))
class TestSetPort(TestPort): class TestSetPort(TestPort):