Skip to content

Commit 6c7a30a

Browse files
NazcaLinesHuanxuan Ao
andcommitted
Implement rbac list and show command
1. implement "openstack network rbac list" 2. implement "openstack network rbac show" 3. also add FakeRBACPolicy to test "network rbac xxx" command The unit test class similar to FakeRouter, which is able to fake one or more rbac policies. It will be used by the rbac CRUD patches. Change-Id: I6c97bc8819698546895fd530464a2cbb347bf77d Co-Authored-By: Huanxuan Ao <huanxuan.ao@easystack.cn> Partially-Implements: blueprint neutron-client-rbac Depends-On: I88f409a24947b67146c0f93ec8480834cef56d2f
1 parent 7cda2b2 commit 6c7a30a

7 files changed

Lines changed: 291 additions & 0 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
============
2+
network rbac
3+
============
4+
5+
A **network rbac** is a Role-Based Access Control (RBAC) policy for
6+
network resources. It enables both operators and users to grant access
7+
to network resources for specific projects.
8+
9+
Network v2
10+
11+
network rbac list
12+
-----------------
13+
14+
List network RBAC policies
15+
16+
.. program:: network rbac list
17+
.. code:: bash
18+
19+
os network rbac list
20+
21+
network rbac show
22+
-----------------
23+
24+
Display network RBAC policy details
25+
26+
.. program:: network rbac show
27+
.. code:: bash
28+
29+
os network rbac show
30+
<rbac-policy>
31+
32+
.. _network_rbac_show-rbac-policy:
33+
.. describe:: <rbac-policy>
34+
35+
RBAC policy (ID only)

doc/source/commands.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ referring to both Compute and Volume quotas.
105105
* ``mapping``: (**Identity**) a definition to translate identity provider attributes to Identity concepts
106106
* ``module``: (**Internal**) - installed Python modules in the OSC process
107107
* ``network``: (**Compute**, **Network**) - a virtual network for connecting servers and other resources
108+
* ``network rbac``: (**Network**) - an RBAC policy for network resources
108109
* ``network segment``: (**Network**) - a segment of a virtual network
109110
* ``object``: (**Object Storage**) a single file in the Object Storage
110111
* ``object store account``: (**Object Storage**) owns a group of Object Storage resources
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
2+
# not use this file except in compliance with the License. You may obtain
3+
# a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10+
# License for the specific language governing permissions and limitations
11+
# under the License.
12+
#
13+
14+
"""RBAC action implementations"""
15+
16+
from osc_lib.command import command
17+
from osc_lib import utils
18+
19+
from openstackclient.i18n import _
20+
21+
22+
def _get_columns(item):
23+
columns = list(item.keys())
24+
if 'tenant_id' in columns:
25+
columns.remove('tenant_id')
26+
columns.append('project_id')
27+
if 'target_tenant' in columns:
28+
columns.remove('target_tenant')
29+
columns.append('target_project')
30+
return tuple(sorted(columns))
31+
32+
33+
class ListNetworkRBAC(command.Lister):
34+
"""List network RBAC policies"""
35+
36+
def take_action(self, parsed_args):
37+
client = self.app.client_manager.network
38+
39+
columns = (
40+
'id',
41+
'object_type',
42+
'object_id',
43+
)
44+
column_headers = (
45+
'ID',
46+
'Object Type',
47+
'Object ID',
48+
)
49+
50+
data = client.rbac_policies()
51+
return (column_headers,
52+
(utils.get_item_properties(
53+
s, columns,
54+
) for s in data))
55+
56+
57+
class ShowNetworkRBAC(command.ShowOne):
58+
"""Display network RBAC policy details"""
59+
60+
def get_parser(self, prog_name):
61+
parser = super(ShowNetworkRBAC, self).get_parser(prog_name)
62+
parser.add_argument(
63+
'rbac_policy',
64+
metavar="<rbac-policy>",
65+
help=_("RBAC policy (ID only)")
66+
)
67+
return parser
68+
69+
def take_action(self, parsed_args):
70+
client = self.app.client_manager.network
71+
obj = client.find_rbac_policy(parsed_args.rbac_policy,
72+
ignore_missing=False)
73+
columns = _get_columns(obj)
74+
data = utils.get_item_properties(obj, columns)
75+
return columns, data

openstackclient/tests/network/v2/fakes.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,57 @@ def get_ports(ports=None, count=2):
480480
return mock.MagicMock(side_effect=ports)
481481

482482

483+
class FakeNetworkRBAC(object):
484+
"""Fake one or more network rbac policies."""
485+
486+
@staticmethod
487+
def create_one_network_rbac(attrs=None):
488+
"""Create a fake network rbac
489+
490+
:param Dictionary attrs:
491+
A dictionary with all attributes
492+
:return:
493+
A FakeResource object, with id, action, target_tenant,
494+
tenant_id, type
495+
"""
496+
attrs = attrs or {}
497+
498+
# Set default attributes
499+
rbac_attrs = {
500+
'id': 'rbac-id-' + uuid.uuid4().hex,
501+
'object_type': 'network',
502+
'object_id': 'object-id-' + uuid.uuid4().hex,
503+
'action': 'access_as_shared',
504+
'target_tenant': 'target-tenant-' + uuid.uuid4().hex,
505+
'tenant_id': 'tenant-id-' + uuid.uuid4().hex,
506+
}
507+
rbac_attrs.update(attrs)
508+
rbac = fakes.FakeResource(info=copy.deepcopy(rbac_attrs),
509+
loaded=True)
510+
# Set attributes with special mapping in OpenStack SDK.
511+
rbac.project_id = rbac_attrs['tenant_id']
512+
rbac.target_project = rbac_attrs['target_tenant']
513+
return rbac
514+
515+
@staticmethod
516+
def create_network_rbacs(attrs=None, count=2):
517+
"""Create multiple fake network rbac policies.
518+
519+
:param Dictionary attrs:
520+
A dictionary with all attributes
521+
:param int count:
522+
The number of rbac policies to fake
523+
:return:
524+
A list of FakeResource objects faking the rbac policies
525+
"""
526+
rbac_policies = []
527+
for i in range(0, count):
528+
rbac_policies.append(FakeNetworkRBAC.
529+
create_one_network_rbac(attrs))
530+
531+
return rbac_policies
532+
533+
483534
class FakeRouter(object):
484535
"""Fake one or more routers."""
485536

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
2+
# not use this file except in compliance with the License. You may obtain
3+
# a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10+
# License for the specific language governing permissions and limitations
11+
# under the License.
12+
#
13+
14+
import mock
15+
16+
from openstackclient.network.v2 import network_rbac
17+
from openstackclient.tests.network.v2 import fakes as network_fakes
18+
from openstackclient.tests import utils as tests_utils
19+
20+
21+
class TestNetworkRBAC(network_fakes.TestNetworkV2):
22+
23+
def setUp(self):
24+
super(TestNetworkRBAC, self).setUp()
25+
26+
# Get a shortcut to the network client
27+
self.network = self.app.client_manager.network
28+
29+
30+
class TestListNetworkRABC(TestNetworkRBAC):
31+
32+
# The network rbac policies going to be listed up.
33+
rbac_policies = network_fakes.FakeNetworkRBAC.create_network_rbacs(count=3)
34+
35+
columns = (
36+
'ID',
37+
'Object Type',
38+
'Object ID',
39+
)
40+
41+
data = []
42+
for r in rbac_policies:
43+
data.append((
44+
r.id,
45+
r.object_type,
46+
r.object_id,
47+
))
48+
49+
def setUp(self):
50+
super(TestListNetworkRABC, self).setUp()
51+
52+
# Get the command object to test
53+
self.cmd = network_rbac.ListNetworkRBAC(self.app, self.namespace)
54+
55+
self.network.rbac_policies = mock.Mock(return_value=self.rbac_policies)
56+
57+
def test_network_rbac_list(self):
58+
arglist = []
59+
verifylist = []
60+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
61+
62+
# DisplayCommandBase.take_action() returns two tuples
63+
columns, data = self.cmd.take_action(parsed_args)
64+
65+
self.network.rbac_policies.assert_called_with()
66+
self.assertEqual(self.columns, columns)
67+
self.assertEqual(self.data, list(data))
68+
69+
70+
class TestShowNetworkRBAC(TestNetworkRBAC):
71+
72+
rbac_policy = network_fakes.FakeNetworkRBAC.create_one_network_rbac()
73+
74+
columns = (
75+
'action',
76+
'id',
77+
'object_id',
78+
'object_type',
79+
'project_id',
80+
'target_project',
81+
)
82+
83+
data = [
84+
rbac_policy.action,
85+
rbac_policy.id,
86+
rbac_policy.object_id,
87+
rbac_policy.object_type,
88+
rbac_policy.tenant_id,
89+
rbac_policy.target_tenant,
90+
]
91+
92+
def setUp(self):
93+
super(TestShowNetworkRBAC, self).setUp()
94+
95+
# Get the command object to test
96+
self.cmd = network_rbac.ShowNetworkRBAC(self.app, self.namespace)
97+
98+
self.network.find_rbac_policy = mock.Mock(
99+
return_value=self.rbac_policy)
100+
101+
def test_show_no_options(self):
102+
arglist = []
103+
verifylist = []
104+
105+
self.assertRaises(tests_utils.ParserException, self.check_parser,
106+
self.cmd, arglist, verifylist)
107+
108+
def test_network_rbac_show_all_options(self):
109+
arglist = [
110+
self.rbac_policy.object_id,
111+
]
112+
verifylist = []
113+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
114+
115+
# DisplayCommandBase.take_action() returns two tuples
116+
columns, data = self.cmd.take_action(parsed_args)
117+
118+
self.network.find_rbac_policy.assert_called_with(
119+
self.rbac_policy.object_id, ignore_missing=False
120+
)
121+
self.assertEqual(self.columns, columns)
122+
self.assertEqual(self.data, list(data))
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
features:
3+
- Add ``network rbac list`` and ``network rbac show`` commands.
4+
[Blueprint `neutron-client-rbac <https://blueprints.launchpad.net/python-openstackclient/+spec/neutron-client-rbac>`_]

setup.cfg

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,9 @@ openstack.network.v2 =
348348
network_set = openstackclient.network.v2.network:SetNetwork
349349
network_show = openstackclient.network.v2.network:ShowNetwork
350350

351+
network_rbac_list = openstackclient.network.v2.network_rbac:ListNetworkRBAC
352+
network_rbac_show = openstackclient.network.v2.network_rbac:ShowNetworkRBAC
353+
351354
network_segment_list = openstackclient.network.v2.network_segment:ListNetworkSegment
352355
network_segment_show = openstackclient.network.v2.network_segment:ShowNetworkSegment
353356

0 commit comments

Comments
 (0)