From 96a8ed435c7e55633c00dbb1283477ff11cf35f9 Mon Sep 17 00:00:00 2001 From: Rui Chen Date: Thu, 18 Aug 2016 15:37:13 +0800 Subject: [PATCH] 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 --- doc/source/command-objects/port.rst | 6 ++++- openstackclient/network/v2/port.py | 21 +++++++++++++----- .../tests/unit/network/v2/test_port.py | 22 +++++++++++++++++++ .../notes/bug-1614385-460b5034ba372463.yaml | 5 +++++ 4 files changed, 48 insertions(+), 6 deletions(-) create mode 100644 releasenotes/notes/bug-1614385-460b5034ba372463.yaml diff --git a/doc/source/command-objects/port.rst b/doc/source/command-objects/port.rst index 4d7b95b4e3..771f2f8d48 100644 --- a/doc/source/command-objects/port.rst +++ b/doc/source/command-objects/port.rst @@ -115,7 +115,7 @@ List ports os port list [--device-owner ] - [--router ] + [--router | --server ] .. option:: --device-owner @@ -126,6 +126,10 @@ List ports List only ports attached to this router (name or ID) +.. option:: --server + + List only ports attached to this server (name or ID) + port set -------- diff --git a/openstackclient/network/v2/port.py b/openstackclient/network/v2/port.py index 05c5a012f3..d7268573dc 100644 --- a/openstackclient/network/v2/port.py +++ b/openstackclient/network/v2/port.py @@ -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='', dest='router', help=_("List only ports attached to this router (name or ID)") ) + device_group.add_argument( + '--server', + metavar='', + 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( diff --git a/openstackclient/tests/unit/network/v2/test_port.py b/openstackclient/tests/unit/network/v2/test_port.py index d5d7f3309b..43a78f8c88 100644 --- a/openstackclient/tests/unit/network/v2/test_port.py +++ b/openstackclient/tests/unit/network/v2/test_port.py @@ -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, diff --git a/releasenotes/notes/bug-1614385-460b5034ba372463.yaml b/releasenotes/notes/bug-1614385-460b5034ba372463.yaml new file mode 100644 index 0000000000..ef416a3fb3 --- /dev/null +++ b/releasenotes/notes/bug-1614385-460b5034ba372463.yaml @@ -0,0 +1,5 @@ +--- +features: + - Support listing the specified server's ports by new option ``--server`` of + ``port list`` command. + [Bug `1614385 `_]