diff --git a/doc/source/command-objects/network-rbac.rst b/doc/source/command-objects/network-rbac.rst index 33fa6af40e..c49f29bb37 100644 --- a/doc/source/command-objects/network-rbac.rst +++ b/doc/source/command-objects/network-rbac.rst @@ -79,6 +79,21 @@ List network RBAC policies .. code:: bash openstack network rbac list + [--type ] + [--action ] + [--long] + +.. option:: --type + + List network RBAC policies according to given object type ("qos_policy" or "network") + +.. option:: --action + + List network RBAC policies according to given action ("access_as_external" or "access_as_shared") + +.. option:: --long + + List additional fields in output network rbac set ---------------- diff --git a/openstackclient/network/v2/network_rbac.py b/openstackclient/network/v2/network_rbac.py index e837af3a62..9075473726 100644 --- a/openstackclient/network/v2/network_rbac.py +++ b/openstackclient/network/v2/network_rbac.py @@ -164,6 +164,30 @@ class DeleteNetworkRBAC(command.Command): class ListNetworkRBAC(command.Lister): _description = _("List network RBAC policies") + def get_parser(self, prog_name): + parser = super(ListNetworkRBAC, self).get_parser(prog_name) + parser.add_argument( + '--type', + metavar='', + choices=['qos_policy', 'network'], + help=_('List network RBAC policies according to ' + 'given object type ("qos_policy" or "network")') + ) + parser.add_argument( + '--action', + metavar='', + choices=['access_as_external', 'access_as_shared'], + help=_('List network RBAC policies according to given ' + 'action ("access_as_external" or "access_as_shared")') + ) + parser.add_argument( + '--long', + action='store_true', + default=False, + help=_("List additional fields in output") + ) + return parser + def take_action(self, parsed_args): client = self.app.client_manager.network @@ -178,7 +202,17 @@ class ListNetworkRBAC(command.Lister): 'Object ID', ) - data = client.rbac_policies() + query = {} + if parsed_args.long: + columns += ('action',) + column_headers += ('Action',) + if parsed_args.type is not None: + query['object_type'] = parsed_args.type + if parsed_args.action is not None: + query['action'] = parsed_args.action + + data = client.rbac_policies(**query) + return (column_headers, (utils.get_item_properties( s, columns, diff --git a/openstackclient/tests/unit/network/v2/test_network_rbac.py b/openstackclient/tests/unit/network/v2/test_network_rbac.py index b884dbc02d..935ce0758c 100644 --- a/openstackclient/tests/unit/network/v2/test_network_rbac.py +++ b/openstackclient/tests/unit/network/v2/test_network_rbac.py @@ -327,7 +327,12 @@ class TestListNetworkRABC(TestNetworkRBAC): 'Object Type', 'Object ID', ) - + columns_long = ( + 'ID', + 'Object Type', + 'Object ID', + 'Action', + ) data = [] for r in rbac_policies: data.append(( @@ -335,6 +340,14 @@ class TestListNetworkRABC(TestNetworkRBAC): r.object_type, r.object_id, )) + data_long = [] + for r in rbac_policies: + data_long.append(( + r.id, + r.object_type, + r.object_id, + r.action, + )) def setUp(self): super(TestListNetworkRABC, self).setUp() @@ -356,6 +369,55 @@ class TestListNetworkRABC(TestNetworkRBAC): self.assertEqual(self.columns, columns) self.assertEqual(self.data, list(data)) + def test_network_rbac_list_type_opt(self): + arglist = [ + '--type', self.rbac_policies[0].object_type, ] + verifylist = [ + ('type', self.rbac_policies[0].object_type)] + 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(**{ + 'object_type': self.rbac_policies[0].object_type + }) + self.assertEqual(self.columns, columns) + self.assertEqual(self.data, list(data)) + + def test_network_rbac_list_action_opt(self): + arglist = [ + '--action', self.rbac_policies[0].action, ] + verifylist = [ + ('action', self.rbac_policies[0].action)] + 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(**{ + 'action': self.rbac_policies[0].action + }) + self.assertEqual(self.columns, columns) + self.assertEqual(self.data, list(data)) + + def test_network_rbac_list_with_long(self): + arglist = [ + '--long', + ] + + verifylist = [ + ('long', True), + ] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + self.network.rbac_policies.assert_called_with() + self.assertEqual(self.columns_long, columns) + self.assertEqual(self.data_long, list(data)) + class TestSetNetworkRBAC(TestNetworkRBAC): diff --git a/releasenotes/notes/bug-1648307-a2c6d7698e927449.yaml b/releasenotes/notes/bug-1648307-a2c6d7698e927449.yaml new file mode 100644 index 0000000000..2e426948a3 --- /dev/null +++ b/releasenotes/notes/bug-1648307-a2c6d7698e927449.yaml @@ -0,0 +1,6 @@ +--- +features: + - | + Add ``--type``, ``--action``, ``--long`` options + to ``network rbac list`` command + [Bug `1648307 `_]