From 780ce07459f8c196dbf289909c390ff88a380e3f Mon Sep 17 00:00:00 2001 From: Badhmapriya Boopalan Date: Thu, 8 Dec 2016 14:48:25 +0000 Subject: [PATCH] To support '--project' and '--project-domain' options Include '--project' and '--project-domain' filtering options to 'port list' command. Change-Id: Ic7deae348f737c754b2f3e2113bd76d71a442400 Closes-Bug: #1648087 --- doc/source/command-objects/port.rst | 10 +++++ openstackclient/network/v2/port.py | 15 +++++++ .../tests/unit/network/v2/test_port.py | 41 +++++++++++++++++++ .../notes/bug-1648087-21dfb7250abfdbe9.yaml | 6 +++ 4 files changed, 72 insertions(+) create mode 100644 releasenotes/notes/bug-1648087-21dfb7250abfdbe9.yaml diff --git a/doc/source/command-objects/port.rst b/doc/source/command-objects/port.rst index 3f6e3fc02c..522cb3ac5e 100644 --- a/doc/source/command-objects/port.rst +++ b/doc/source/command-objects/port.rst @@ -149,6 +149,7 @@ List ports [--network ] [--mac-address ] [--long] + [--project [--project-domain ]] .. option:: --device-owner @@ -175,6 +176,15 @@ List ports List additional fields in output +.. option:: --project + + List ports according to their project (name or ID) + +.. option:: --project-domain + + Domain the project belongs to (name or ID). + This can be used in case collisions between project names exist. + port set -------- diff --git a/openstackclient/network/v2/port.py b/openstackclient/network/v2/port.py index 4525da1800..b1d456410b 100644 --- a/openstackclient/network/v2/port.py +++ b/openstackclient/network/v2/port.py @@ -432,11 +432,18 @@ class ListPort(command.Lister): default=False, help=_("List additional fields in output") ) + parser.add_argument( + '--project', + metavar='', + help=_("List ports according to their project (name or ID)") + ) + identity_common.add_project_domain_option_to_parser(parser) return parser def take_action(self, parsed_args): network_client = self.app.client_manager.network compute_client = self.app.client_manager.compute + identity_client = self.app.client_manager.identity columns = ( 'id', @@ -473,6 +480,14 @@ class ListPort(command.Lister): filters['network_id'] = network.id if parsed_args.mac_address: filters['mac_address'] = parsed_args.mac_address + if parsed_args.project: + project_id = identity_common.find_project( + identity_client, + parsed_args.project, + parsed_args.project_domain, + ).id + filters['tenant_id'] = project_id + filters['project_id'] = project_id data = network_client.ports(**filters) diff --git a/openstackclient/tests/unit/network/v2/test_port.py b/openstackclient/tests/unit/network/v2/test_port.py index 255e81166e..6f61eabc34 100644 --- a/openstackclient/tests/unit/network/v2/test_port.py +++ b/openstackclient/tests/unit/network/v2/test_port.py @@ -20,6 +20,7 @@ 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.identity.v3 import fakes as identity_fakes from openstackclient.tests.unit.network.v2 import fakes as network_fakes from openstackclient.tests.unit import utils as tests_utils @@ -31,6 +32,8 @@ class TestPort(network_fakes.TestNetworkV2): # Get a shortcut to the network client self.network = self.app.client_manager.network + # Get a shortcut to the ProjectManager Mock + self.projects_mock = self.app.client_manager.identity.projects def _get_common_cols_data(self, fake_port): columns = ( @@ -673,6 +676,44 @@ class TestListPort(TestPort): self.assertEqual(self.columns_long, columns) self.assertEqual(self.data_long, list(data)) + def test_port_list_project(self): + project = identity_fakes.FakeProject.create_one_project() + self.projects_mock.get.return_value = project + arglist = [ + '--project', project.id, + ] + verifylist = [ + ('project', project.id), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + filters = {'tenant_id': project.id, 'project_id': project.id} + + self.network.ports.assert_called_once_with(**filters) + self.assertEqual(self.columns, columns) + self.assertEqual(self.data, list(data)) + + def test_port_list_project_domain(self): + project = identity_fakes.FakeProject.create_one_project() + self.projects_mock.get.return_value = project + arglist = [ + '--project', project.id, + '--project-domain', project.domain_id, + ] + verifylist = [ + ('project', project.id), + ('project_domain', project.domain_id), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + filters = {'tenant_id': project.id, 'project_id': project.id} + + self.network.ports.assert_called_once_with(**filters) + self.assertEqual(self.columns, columns) + self.assertEqual(self.data, list(data)) + class TestSetPort(TestPort): diff --git a/releasenotes/notes/bug-1648087-21dfb7250abfdbe9.yaml b/releasenotes/notes/bug-1648087-21dfb7250abfdbe9.yaml new file mode 100644 index 0000000000..fad99aee9c --- /dev/null +++ b/releasenotes/notes/bug-1648087-21dfb7250abfdbe9.yaml @@ -0,0 +1,6 @@ +--- +features: + - | + Add ``--project`` and ``--project-domain`` filtering options + to ``port list`` command. + [Bug `1648087 `_]