Skip to content

Commit ab18045

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "Implement rbac list and show command"
2 parents d0daf90 + 6c7a30a commit ab18045

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
@@ -109,6 +109,7 @@ referring to both Compute and Volume quotas.
109109
* ``mapping``: (**Identity**) a definition to translate identity provider attributes to Identity concepts
110110
* ``module``: (**Internal**) - installed Python modules in the OSC process
111111
* ``network``: (**Compute**, **Network**) - a virtual network for connecting servers and other resources
112+
* ``network rbac``: (**Network**) - an RBAC policy for network resources
112113
* ``network segment``: (**Network**) - a segment of a virtual network
113114
* ``object``: (**Object Storage**) a single file in the Object Storage
114115
* ``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
@@ -483,6 +483,57 @@ def get_ports(ports=None, count=2):
483483
return mock.MagicMock(side_effect=ports)
484484

485485

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

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
@@ -360,6 +360,9 @@ openstack.network.v2 =
360360
network_set = openstackclient.network.v2.network:SetNetwork
361361
network_show = openstackclient.network.v2.network:ShowNetwork
362362

363+
network_rbac_list = openstackclient.network.v2.network_rbac:ListNetworkRBAC
364+
network_rbac_show = openstackclient.network.v2.network_rbac:ShowNetworkRBAC
365+
363366
network_segment_list = openstackclient.network.v2.network_segment:ListNetworkSegment
364367
network_segment_show = openstackclient.network.v2.network_segment:ShowNetworkSegment
365368

0 commit comments

Comments
 (0)