Skip to content

Commit aa6aeac

Browse files
shalev67briancurtin
authored andcommitted
Add support for Role resource in Identity v3
This change adds the Role resource to Identity v3 and exposes the CRUD methods in the proxy. It also adds a small example to the documentation. Change-Id: I064c27fcd1d15bfd064c63b8c50bc2461b8c1156
1 parent 1652043 commit aa6aeac

File tree

5 files changed

+196
-0
lines changed

5 files changed

+196
-0
lines changed

examples/identity/list.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,10 @@ def list_regions(conn):
7171

7272
for region in conn.identity.regions():
7373
print(region)
74+
75+
76+
def list_roles(conn):
77+
print("List Roles:")
78+
79+
for role in conn.identity.roles():
80+
print(role)

openstack/identity/v3/_proxy.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from openstack.identity.v3 import policy as _policy
1818
from openstack.identity.v3 import project as _project
1919
from openstack.identity.v3 import region as _region
20+
from openstack.identity.v3 import role as _role
2021
from openstack.identity.v3 import service as _service
2122
from openstack.identity.v3 import trust as _trust
2223
from openstack.identity.v3 import user as _user
@@ -797,3 +798,80 @@ def update_region(self, region, **attrs):
797798
:rtype: :class:`~openstack.identity.v3.region.Region`
798799
"""
799800
return self._update(_region.Region, region, **attrs)
801+
802+
def create_role(self, **attrs):
803+
"""Create a new role from attributes
804+
805+
:param dict attrs: Keyword arguments which will be used to create
806+
a :class:`~openstack.identity.v3.role.Role`,
807+
comprised of the properties on the Role class.
808+
809+
:returns: The results of role creation.
810+
:rtype: :class:`~openstack.identity.v3.role.Role`
811+
"""
812+
return self._create(_role.Role, **attrs)
813+
814+
def delete_role(self, role, ignore_missing=True):
815+
"""Delete a role
816+
817+
:param role: The value can be either the ID of a role or a
818+
:class:`~openstack.identity.v3.role.Role` instance.
819+
:param bool ignore_missing: When set to ``False``
820+
:class:`~openstack.exceptions.ResourceNotFound` will be
821+
raised when the role does not exist.
822+
When set to ``True``, no exception will be thrown when
823+
attempting to delete a nonexistent role.
824+
825+
:returns: ``None``
826+
"""
827+
self._delete(_role.Role, role, ignore_missing=ignore_missing)
828+
829+
def find_role(self, name_or_id, ignore_missing=True):
830+
"""Find a single role
831+
832+
:param name_or_id: The name or ID of a role.
833+
:param bool ignore_missing: When set to ``False``
834+
:class:`~openstack.exceptions.ResourceNotFound` will be
835+
raised when the role does not exist.
836+
When set to ``True``, None will be returned when
837+
attempting to find a nonexistent role.
838+
:returns: One :class:`~openstack.identity.v3.role.Role` or None
839+
"""
840+
return self._find(_role.Role, name_or_id,
841+
ignore_missing=ignore_missing)
842+
843+
def get_role(self, role):
844+
"""Get a single role
845+
846+
:param role: The value can be the ID of a role or a
847+
:class:`~openstack.identity.v3.role.Role` instance.
848+
849+
:returns: One :class:`~openstack.identity.v3.role.Role`
850+
:raises: :class:`~openstack.exceptions.ResourceNotFound`
851+
when no matching role can be found.
852+
"""
853+
return self._get(_role.Role, role)
854+
855+
def roles(self, **query):
856+
"""Retrieve a generator of roles
857+
858+
:param kwargs \*\*query: Optional query parameters to be sent to limit
859+
the resources being returned. The options
860+
are: domain_id, name.
861+
:return: A generator of role instances.
862+
:rtype: :class:`~openstack.identity.v3.role.Role`
863+
"""
864+
return self._list(_role.Role, paginated=False, **query)
865+
866+
def update_role(self, role, **attrs):
867+
"""Update a role
868+
869+
:param role: Either the ID of a role or a
870+
:class:`~openstack.identity.v3.role.Role` instance.
871+
:param dict kwargs: The attributes to update on the role represented
872+
by ``value``. Only name can be updated
873+
874+
:returns: The updated role.
875+
:rtype: :class:`~openstack.identity.v3.role.Role`
876+
"""
877+
return self._update(_role.Role, role, **attrs)

openstack/identity/v3/role.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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.identity import identity_service
14+
from openstack import resource2 as resource
15+
16+
17+
class Role(resource.Resource):
18+
resource_key = 'role'
19+
resources_key = 'roles'
20+
base_path = '/roles'
21+
service = identity_service.IdentityService()
22+
23+
# capabilities
24+
allow_create = True
25+
allow_get = True
26+
allow_update = True
27+
allow_delete = True
28+
allow_list = True
29+
put_create = True
30+
31+
_query_mapping = resource.QueryParameters(
32+
'name', 'domain_id')
33+
34+
# Properties
35+
#: References the domain ID which owns the role; if a domain ID is not
36+
#: specified by the client, the Identity service implementation will
37+
#: default it to the domain ID to which the client's token is scoped.
38+
#: *Type: string*
39+
domain_id = resource.Body('domain_id')
40+
#: Unique role name, within the owning domain. *Type: string*
41+
name = resource.Body('name')
42+
#: The links for the service resource.
43+
links = resource.Body('links')

openstack/tests/unit/identity/v3/test_proxy.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from openstack.identity.v3 import policy
1919
from openstack.identity.v3 import project
2020
from openstack.identity.v3 import region
21+
from openstack.identity.v3 import role
2122
from openstack.identity.v3 import service
2223
from openstack.identity.v3 import trust
2324
from openstack.identity.v3 import user
@@ -242,3 +243,24 @@ def test_regions(self):
242243

243244
def test_region_update(self):
244245
self.verify_update(self.proxy.update_region, region.Region)
246+
247+
def test_role_create_attrs(self):
248+
self.verify_create(self.proxy.create_role, role.Role)
249+
250+
def test_role_delete(self):
251+
self.verify_delete(self.proxy.delete_role, role.Role, False)
252+
253+
def test_role_delete_ignore(self):
254+
self.verify_delete(self.proxy.delete_role, role.Role, True)
255+
256+
def test_role_find(self):
257+
self.verify_find(self.proxy.find_role, role.Role)
258+
259+
def test_role_get(self):
260+
self.verify_get(self.proxy.get_role, role.Role)
261+
262+
def test_roles(self):
263+
self.verify_list(self.proxy.roles, role.Role, paginated=False)
264+
265+
def test_role_update(self):
266+
self.verify_update(self.proxy.update_role, role.Role)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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.identity.v3 import role
16+
17+
IDENTIFIER = 'IDENTIFIER'
18+
EXAMPLE = {
19+
'domain_id': '1',
20+
'id': IDENTIFIER,
21+
'links': {'self': 'http://example.com/user1'},
22+
'name': '2',
23+
}
24+
25+
26+
class TestRole(testtools.TestCase):
27+
28+
def test_basic(self):
29+
sot = role.Role()
30+
self.assertEqual('role', sot.resource_key)
31+
self.assertEqual('roles', sot.resources_key)
32+
self.assertEqual('/roles', sot.base_path)
33+
self.assertEqual('identity', sot.service.service_type)
34+
self.assertTrue(sot.allow_create)
35+
self.assertTrue(sot.allow_get)
36+
self.assertTrue(sot.allow_update)
37+
self.assertTrue(sot.allow_delete)
38+
self.assertTrue(sot.allow_list)
39+
self.assertTrue(sot.put_create)
40+
41+
def test_make_it(self):
42+
sot = role.Role(**EXAMPLE)
43+
self.assertEqual(EXAMPLE['domain_id'], sot.domain_id)
44+
self.assertEqual(EXAMPLE['id'], sot.id)
45+
self.assertEqual(EXAMPLE['links'], sot.links)
46+
self.assertEqual(EXAMPLE['name'], sot.name)

0 commit comments

Comments
 (0)