Merge "Add option to allow filtering by router on port list"
This commit is contained in:
commit
a60e31ad4b
@ -108,6 +108,11 @@ List ports
|
||||
.. code:: bash
|
||||
|
||||
os port list
|
||||
[--router <router>]
|
||||
|
||||
.. option:: --router <router>
|
||||
|
||||
List only ports attached to this router (name or ID)
|
||||
|
||||
port set
|
||||
--------
|
||||
|
@ -243,6 +243,16 @@ class DeletePort(command.Command):
|
||||
class ListPort(command.Lister):
|
||||
"""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):
|
||||
client = self.app.client_manager.network
|
||||
|
||||
@ -259,7 +269,14 @@ class ListPort(command.Lister):
|
||||
'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,
|
||||
(utils.get_item_properties(
|
||||
s, columns,
|
||||
|
@ -224,8 +224,11 @@ class TestListPort(TestPort):
|
||||
|
||||
# Get the command object to test
|
||||
self.cmd = port.ListPort(self.app, self.namespace)
|
||||
|
||||
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):
|
||||
arglist = []
|
||||
@ -239,6 +242,25 @@ class TestListPort(TestPort):
|
||||
self.assertEqual(self.columns, columns)
|
||||
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):
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user