Skip to content

Commit 95fa02b

Browse files
NazcaLinesHuanxuan Ao
authored andcommitted
Add neutron rbac support
do following things: 1. add documentations 2. add interfaces in _proxy.py 3. add rbac 4. unit tests & functional tests Partially-Implements: blueprint neutron-client-rbac Change-Id: I88f409a24947b67146c0f93ec8480834cef56d2f
1 parent 39fd3ae commit 95fa02b

File tree

7 files changed

+278
-0
lines changed

7 files changed

+278
-0
lines changed

doc/source/users/resources/network/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Network Resources
2020
v2/pool_member
2121
v2/port
2222
v2/quota
23+
v2/rbac_policy
2324
v2/router
2425
v2/security_group
2526
v2/security_group_rule
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
openstack.network.v2.rbac_policy
2+
================================
3+
4+
.. automodule:: openstack.network.v2.rbac_policy
5+
6+
The RBACPolicy Class
7+
--------------------
8+
9+
The ``RBACPolicy`` class inherits from :class:`~openstack.resource.Resource`.
10+
11+
.. autoclass:: openstack.network.v2.rbac_policy.RBACPolicy
12+
:members:

openstack/network/v2/_proxy.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from openstack.network.v2 import pool_member as _pool_member
2727
from openstack.network.v2 import port as _port
2828
from openstack.network.v2 import quota as _quota
29+
from openstack.network.v2 import rbac_policy as _rbac_policy
2930
from openstack.network.v2 import router as _router
3031
from openstack.network.v2 import security_group as _security_group
3132
from openstack.network.v2 import security_group_rule as _security_group_rule
@@ -1199,6 +1200,91 @@ def update_quota(self, quota, **attrs):
11991200
"""
12001201
return self._update(_quota.Quota, quota, **attrs)
12011202

1203+
def create_rbac_policy(self, **attrs):
1204+
"""Create a new RBAC policy from attributes
1205+
1206+
:param dict attrs: Keyword arguments which will be used to create a
1207+
:class:`~openstack.network.v2.rbac_policy.RBACPolicy`,
1208+
comprised of the properties on the RBACPolicy class.
1209+
1210+
:return: The results of RBAC policy creation
1211+
:rtype: :class:`~openstack.network.v2.rbac_policy.RBACPolicy`
1212+
"""
1213+
return self._create(_rbac_policy.RBACPolicy, **attrs)
1214+
1215+
def delete_rbac_policy(self, rbac_policy, ignore_missing=True):
1216+
"""Delete a RBAC policy
1217+
1218+
:param rbac_policy: The value can be either the ID of a RBAC policy or
1219+
a :class:`~openstack.network.v2.rbac_policy.RBACPolicy` instance.
1220+
:param bool ignore_missing: When set to ``False``
1221+
:class:`~openstack.exceptions.ResourceNotFound` will be
1222+
raised when the RBAC policy does not exist.
1223+
When set to ``True``, no exception will be set when
1224+
attempting to delete a nonexistent RBAC policy.
1225+
1226+
:returns: ``None``
1227+
"""
1228+
self._delete(_rbac_policy.RBACPolicy, rbac_policy,
1229+
ignore_missing=ignore_missing)
1230+
1231+
def find_rbac_policy(self, id, ignore_missing=True):
1232+
"""Find a single RBAC policy
1233+
1234+
:param id: The ID of a RBAC policy.
1235+
:param bool ignore_missing: When set to ``False``
1236+
:class:`~openstack.exceptions.ResourceNotFound` will be
1237+
raised when the resource does not exist.
1238+
When set to ``True``, None will be returned when
1239+
attempting to find a nonexistent resource.
1240+
:returns: One
1241+
:class:`~openstack.network.v2.rbac_policy.RBACPolicy` or None
1242+
"""
1243+
return self._find(_rbac_policy.RBACPolicy, id,
1244+
ignore_missing=ignore_missing)
1245+
1246+
def get_rbac_policy(self, rbac_policy):
1247+
"""Get a single RBAC policy
1248+
1249+
:param rbac_policy: The value can be the ID of a RBAC policy or a
1250+
:class:`~openstack.network.v2.rbac_policy.RBACPolicy` instance.
1251+
1252+
:returns: One :class:`~openstack.network.v2.rbac_policy.RBACPolicy`
1253+
:raises: :class:`~openstack.exceptions.ResourceNotFound`
1254+
when no resource can be found.
1255+
"""
1256+
return self._get(_rbac_policy.RBACPolicy, rbac_policy)
1257+
1258+
def rbac_policies(self, **query):
1259+
"""Return a generator of RBAC policies
1260+
1261+
:param kwargs \*\*query: Optional query parameters to be sent to limit
1262+
the resources being returned. Available parameters include:
1263+
1264+
* tenant_id: The owner tenant ID.
1265+
* target_tenant: ID of the tenant to which the RBAC policy
1266+
will be enforced.
1267+
* object_type: Type of the object that RBAC policy affects.
1268+
* action: Action for the RBAC policy.
1269+
1270+
:returns: A generator of rbac objects
1271+
:rtype: :class:`~openstack.network.v2.rbac_policy.RBACPolicy`
1272+
"""
1273+
return self._list(_rbac_policy.RBACPolicy, paginated=False, **query)
1274+
1275+
def update_rbac_policy(self, rbac_policy, **attrs):
1276+
"""Update a RBAC policy
1277+
1278+
:param rbac_policy: Either the id of a RBAC policy or a
1279+
:class:`~openstack.network.v2.rbac_policy.RBACPolicy` instance.
1280+
:attrs kwargs: The attributes to update on the RBAC policy represented
1281+
by ``value``.
1282+
1283+
:returns: The updated RBAC policy
1284+
:rtype: :class:`~openstack.network.v2.rbac_policy.RBACPolicy`
1285+
"""
1286+
return self._update(_rbac_policy.RBACPolicy, rbac_policy, **attrs)
1287+
12021288
def create_router(self, **attrs):
12031289
"""Create a new router from attributes
12041290
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
from openstack.network import network_service
14+
from openstack import resource
15+
16+
17+
class RBACPolicy(resource.Resource):
18+
resource_key = 'rbac_policy'
19+
resources_key = 'rbac_policies'
20+
base_path = '/rbac-policies'
21+
service = network_service.NetworkService()
22+
23+
# capabilities
24+
allow_create = True
25+
allow_retrieve = True
26+
allow_update = True
27+
allow_delete = True
28+
allow_list = True
29+
30+
# Properties
31+
#: The id of RBAC object.
32+
object_id = resource.prop('object_id')
33+
#: The ID of the project this RBAC will be enforced.
34+
target_project_id = resource.prop('target_tenant')
35+
#: The owner project ID.
36+
project_id = resource.prop('tenant_id')
37+
#: Type of the object that RBAC policy affects.
38+
object_type = resource.prop('object_type')
39+
#: Action for the RBAC policy.
40+
action = resource.prop('action')
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
import uuid
14+
15+
from openstack.network.v2 import network
16+
from openstack.network.v2 import rbac_policy
17+
from openstack.tests.functional import base
18+
19+
20+
class TestRBACPolicy(base.BaseFunctionalTest):
21+
22+
NET_NAME = 'net-' + uuid.uuid4().hex
23+
UPDATE_NAME = uuid.uuid4().hex
24+
ACTION = 'access_as_shared'
25+
OBJ_TYPE = 'network'
26+
TARGET_TENANT_ID = '*'
27+
NET_ID = None
28+
ID = None
29+
30+
@classmethod
31+
def setUpClass(cls):
32+
super(TestRBACPolicy, cls).setUpClass()
33+
net = cls.conn.network.create_network(name=cls.NET_NAME)
34+
assert isinstance(net, network.Network)
35+
cls.NET_ID = net.id
36+
37+
sot = cls.conn.network.\
38+
create_rbac_policy(action=cls.ACTION,
39+
object_type=cls.OBJ_TYPE,
40+
target_tenant=cls.TARGET_TENANT_ID,
41+
object_id=cls.NET_ID)
42+
assert isinstance(sot, rbac_policy.RBACPolicy)
43+
cls.ID = sot.id
44+
45+
@classmethod
46+
def tearDownClass(cls):
47+
sot = cls.conn.network.delete_rbac_policy(cls.ID,
48+
ignore_missing=False)
49+
cls.assertIs(None, sot)
50+
sot = cls.conn.network.delete_network(cls.NET_ID,
51+
ignore_missing=False)
52+
cls.assertIs(None, sot)
53+
54+
def test_find(self):
55+
sot = self.conn.network.find_rbac_policy(self.ID)
56+
self.assertEqual(self.ID, sot.id)
57+
58+
def test_get(self):
59+
sot = self.conn.network.get_rbac_policy(self.ID)
60+
self.assertEqual(self.ID, sot.id)
61+
62+
def test_list(self):
63+
ids = [o.id for o in self.conn.network.rbac_policies()]
64+
self.assertIn(self.ID, ids)

openstack/tests/unit/network/v2/test_proxy.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from openstack.network.v2 import pool_member
3030
from openstack.network.v2 import port
3131
from openstack.network.v2 import quota
32+
from openstack.network.v2 import rbac_policy
3233
from openstack.network.v2 import router
3334
from openstack.network.v2 import security_group
3435
from openstack.network.v2 import security_group_rule
@@ -396,6 +397,32 @@ def test_quotas(self):
396397
def test_quota_update(self):
397398
self.verify_update(self.proxy.update_quota, quota.Quota)
398399

400+
def test_rbac_policy_create_attrs(self):
401+
self.verify_create(self.proxy.create_rbac_policy,
402+
rbac_policy.RBACPolicy)
403+
404+
def test_rbac_policy_delete(self):
405+
self.verify_delete(self.proxy.delete_rbac_policy,
406+
rbac_policy.RBACPolicy, False)
407+
408+
def test_rbac_policy_delete_ignore(self):
409+
self.verify_delete(self.proxy.delete_rbac_policy,
410+
rbac_policy.RBACPolicy, True)
411+
412+
def test_rbac_policy_find(self):
413+
self.verify_find(self.proxy.find_rbac_policy, rbac_policy.RBACPolicy)
414+
415+
def test_rbac_policy_get(self):
416+
self.verify_get(self.proxy.get_rbac_policy, rbac_policy.RBACPolicy)
417+
418+
def test_rbac_policies(self):
419+
self.verify_list(self.proxy.rbac_policies,
420+
rbac_policy.RBACPolicy, paginated=False)
421+
422+
def test_rbac_policy_update(self):
423+
self.verify_update(self.proxy.update_rbac_policy,
424+
rbac_policy.RBACPolicy)
425+
399426
def test_router_create_attrs(self):
400427
self.verify_create(self.proxy.create_router, router.Router)
401428

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
import testtools
14+
15+
from openstack.network.v2 import rbac_policy
16+
17+
IDENTIFIER = 'IDENTIFIER'
18+
EXAMPLE = {
19+
'object_id': IDENTIFIER,
20+
'tenant_id': '5',
21+
'object_type': 'network',
22+
'action': 'access_as_shared',
23+
'target_tenant': '10',
24+
}
25+
26+
27+
class TestRBACPolicy(testtools.TestCase):
28+
29+
def test_basic(self):
30+
sot = rbac_policy.RBACPolicy()
31+
self.assertEqual('rbac_policy', sot.resource_key)
32+
self.assertEqual('rbac_policies', sot.resources_key)
33+
self.assertEqual('/rbac-policies', sot.base_path)
34+
self.assertEqual('network', sot.service.service_type)
35+
self.assertTrue(sot.allow_create)
36+
self.assertTrue(sot.allow_retrieve)
37+
self.assertTrue(sot.allow_update)
38+
self.assertTrue(sot.allow_delete)
39+
self.assertTrue(sot.allow_list)
40+
41+
def test_make_it(self):
42+
sot = rbac_policy.RBACPolicy(EXAMPLE)
43+
self.assertEqual(EXAMPLE['object_id'],
44+
sot.object_id)
45+
self.assertEqual(EXAMPLE['tenant_id'], sot.project_id)
46+
self.assertEqual(EXAMPLE['object_type'], sot.object_type)
47+
self.assertEqual(EXAMPLE['action'], sot.action)
48+
self.assertEqual(EXAMPLE['target_tenant'], sot.target_project_id)

0 commit comments

Comments
 (0)