From 4d7d7e627eb4bb028251bd7993026aae45db7d8b Mon Sep 17 00:00:00 2001 From: Jan Hartkopf Date: Tue, 15 Mar 2022 15:49:12 +0100 Subject: [PATCH] network: Add tenant project filter for RBAC list Implements a new parser argument "--target-project" to list RBAC policies for a specific tenant project only. This uses the already existing server-side query parameter "target_tenant". Story: 2009937 Task: 44824 Depends-On: https://review.opendev.org/c/openstack/openstacksdk/+/834442 Change-Id: I83ff07041a022e8795e3c5550c6a7aabb0c0d8c8 Signed-off-by: Jan Hartkopf --- openstackclient/network/v2/network_rbac.py | 15 +++++++++++++++ .../unit/network/v2/test_network_rbac.py | 19 +++++++++++++++++++ ...enant-project-filter-1228f2287284e33c.yaml | 5 +++++ 3 files changed, 39 insertions(+) create mode 100644 releasenotes/notes/add-network-rbac-list-tenant-project-filter-1228f2287284e33c.yaml diff --git a/openstackclient/network/v2/network_rbac.py b/openstackclient/network/v2/network_rbac.py index 91cd9dc32d..fa4fca7c40 100644 --- a/openstackclient/network/v2/network_rbac.py +++ b/openstackclient/network/v2/network_rbac.py @@ -217,6 +217,11 @@ class ListNetworkRBAC(command.Lister): help=_('List network RBAC policies according to given ' 'action ("access_as_external" or "access_as_shared")') ) + parser.add_argument( + '--target-project', + metavar='', + help=_('List network RBAC policies for a specific target project') + ) parser.add_argument( '--long', action='store_true', @@ -247,6 +252,16 @@ class ListNetworkRBAC(command.Lister): query['object_type'] = parsed_args.type if parsed_args.action is not None: query['action'] = parsed_args.action + if parsed_args.target_project is not None: + project_id = "*" + + if parsed_args.target_project != "*": + identity_client = self.app.client_manager.identity + project_id = identity_common.find_project( + identity_client, + parsed_args.target_project, + ).id + query['target_project_id'] = project_id data = client.rbac_policies(**query) diff --git a/openstackclient/tests/unit/network/v2/test_network_rbac.py b/openstackclient/tests/unit/network/v2/test_network_rbac.py index b0bc7e86cb..7ce252054d 100644 --- a/openstackclient/tests/unit/network/v2/test_network_rbac.py +++ b/openstackclient/tests/unit/network/v2/test_network_rbac.py @@ -405,6 +405,9 @@ class TestListNetworkRABC(TestNetworkRBAC): self.network.rbac_policies = mock.Mock(return_value=self.rbac_policies) + self.project = identity_fakes_v3.FakeProject.create_one_project() + self.projects_mock.get.return_value = self.project + def test_network_rbac_list(self): arglist = [] verifylist = [] @@ -466,6 +469,22 @@ class TestListNetworkRABC(TestNetworkRBAC): self.assertEqual(self.columns_long, columns) self.assertEqual(self.data_long, list(data)) + def test_network_rbac_list_target_project_opt(self): + arglist = [ + '--target-project', self.rbac_policies[0].target_project_id, ] + verifylist = [ + ('target_project', self.rbac_policies[0].target_project_id)] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + # DisplayCommandBase.take_action() returns two tuples + columns, data = self.cmd.take_action(parsed_args) + + self.network.rbac_policies.assert_called_with(**{ + 'target_project_id': self.project.id + }) + self.assertEqual(self.columns, columns) + self.assertEqual(self.data, list(data)) + class TestSetNetworkRBAC(TestNetworkRBAC): diff --git a/releasenotes/notes/add-network-rbac-list-tenant-project-filter-1228f2287284e33c.yaml b/releasenotes/notes/add-network-rbac-list-tenant-project-filter-1228f2287284e33c.yaml new file mode 100644 index 0000000000..82fe6aaa76 --- /dev/null +++ b/releasenotes/notes/add-network-rbac-list-tenant-project-filter-1228f2287284e33c.yaml @@ -0,0 +1,5 @@ +--- +features: + - | + Add a new argument ``--target-project`` to the ``network rbac list`` + command to filter for a specific target project. \ No newline at end of file