Support listing specified server's ports

Add new option "--server" for "port list" command to
list all of the ports that are attached on the specified
server.

Change-Id: I8b5550ea5068405b163711303465b704b5207410
Closes-Bug: #1614385
This commit is contained in:
Rui Chen 2016-08-18 15:37:13 +08:00
parent 6f326acd26
commit 96a8ed435c
4 changed files with 48 additions and 6 deletions

View File

@ -115,7 +115,7 @@ List ports
os port list
[--device-owner <device-owner>]
[--router <router>]
[--router <router> | --server <server>]
.. option:: --device-owner <device-owner>
@ -126,6 +126,10 @@ List ports
List only ports attached to this router (name or ID)
.. option:: --server <server>
List only ports attached to this server (name or ID)
port set
--------

View File

@ -345,16 +345,23 @@ class ListPort(command.Lister):
"This is the entity that uses the port (for example, "
"network:dhcp).")
)
parser.add_argument(
device_group = parser.add_mutually_exclusive_group()
device_group.add_argument(
'--router',
metavar='<router>',
dest='router',
help=_("List only ports attached to this router (name or ID)")
)
device_group.add_argument(
'--server',
metavar='<server>',
help=_("List only ports attached to this server (name or ID)"),
)
return parser
def take_action(self, parsed_args):
client = self.app.client_manager.network
network_client = self.app.client_manager.network
compute_client = self.app.client_manager.compute
columns = (
'id',
@ -373,11 +380,15 @@ class ListPort(command.Lister):
if parsed_args.device_owner is not None:
filters['device_owner'] = parsed_args.device_owner
if parsed_args.router:
_router = client.find_router(parsed_args.router,
ignore_missing=False)
_router = network_client.find_router(parsed_args.router,
ignore_missing=False)
filters['device_id'] = _router.id
if parsed_args.server:
server = utils.find_resource(compute_client.servers,
parsed_args.server)
filters['device_id'] = server.id
data = client.ports(**filters)
data = network_client.ports(**filters)
return (column_headers,
(utils.get_item_properties(

View File

@ -19,6 +19,7 @@ from osc_lib import exceptions
from osc_lib import utils
from openstackclient.network.v2 import port
from openstackclient.tests.unit.compute.v2 import fakes as compute_fakes
from openstackclient.tests.unit.network.v2 import fakes as network_fakes
from openstackclient.tests.unit import utils as tests_utils
@ -337,6 +338,7 @@ class TestListPort(TestPort):
'id': 'fake-router-id',
})
self.network.find_router = mock.Mock(return_value=fake_router)
self.app.client_manager.compute = mock.Mock()
def test_port_list_no_options(self):
arglist = []
@ -369,6 +371,26 @@ class TestListPort(TestPort):
self.assertEqual(self.columns, columns)
self.assertEqual(self.data, list(data))
@mock.patch.object(utils, 'find_resource')
def test_port_list_with_server_option(self, mock_find):
fake_server = compute_fakes.FakeServer.create_one_server()
mock_find.return_value = fake_server
arglist = [
'--server', 'fake-server-name',
]
verifylist = [
('server', 'fake-server-name'),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
columns, data = self.cmd.take_action(parsed_args)
self.network.ports.assert_called_once_with(
device_id=fake_server.id)
mock_find.assert_called_once_with(mock.ANY, 'fake-server-name')
self.assertEqual(self.columns, columns)
self.assertEqual(self.data, list(data))
def test_port_list_device_owner_opt(self):
arglist = [
'--device-owner', self._ports[0].device_owner,

View File

@ -0,0 +1,5 @@
---
features:
- Support listing the specified server's ports by new option ``--server`` of
``port list`` command.
[Bug `1614385 <https://bugs.launchpad.net/python-openstackclient/+bug/1614385>`_]