Skip to content

Commit 856f641

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "Refactor utility to find identity resources"
2 parents 166c06a + d733e45 commit 856f641

1 file changed

Lines changed: 41 additions & 56 deletions

File tree

‎openstackclient/identity/common.py‎

Lines changed: 41 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from keystoneclient.v3 import groups
2121
from keystoneclient.v3 import projects
2222
from keystoneclient.v3 import users
23+
2324
from openstackclient.common import exceptions
2425
from openstackclient.common import utils
2526

@@ -43,74 +44,58 @@ def find_service(identity_client, name_type_or_id):
4344

4445

4546
def find_domain(identity_client, name_or_id):
46-
"""Find a domain.
47+
return _find_identity_resource(identity_client.domains, name_or_id,
48+
domains.Domain)
4749

48-
If the user does not have permissions to access the v3 domain API, e.g.,
49-
if the user is a project admin, assume that the domain given is the id
50-
rather than the name. This method is used by the project list command,
51-
so errors accessing the domain will be ignored and if the user has
52-
access to the project API, everything will work fine.
5350

54-
Closes bugs #1317478 and #1317485.
55-
"""
56-
try:
57-
dom = utils.find_resource(identity_client.domains, name_or_id)
58-
if dom is not None:
59-
return dom
60-
except identity_exc.Forbidden:
61-
pass
62-
return domains.Domain(None, {'id': name_or_id, 'name': name_or_id})
51+
def find_group(identity_client, name_or_id):
52+
return _find_identity_resource(identity_client.groups, name_or_id,
53+
groups.Group)
6354

6455

65-
def find_group(identity_client, name_or_id):
66-
"""Find a group.
56+
def find_project(identity_client, name_or_id):
57+
return _find_identity_resource(identity_client.projects, name_or_id,
58+
projects.Project)
6759

68-
If the user does not have permissions to to perform a list groups call,
69-
e.g., if the user is a project admin, assume that the group given is the
70-
id rather than the name. This method is used by the role add command to
71-
allow a role to be assigned to a group by a project admin who does not
72-
have permission to list groups.
73-
"""
74-
try:
75-
group = utils.find_resource(identity_client.groups, name_or_id)
76-
if group is not None:
77-
return group
78-
except identity_exc.Forbidden:
79-
pass
80-
return groups.Group(None, {'id': name_or_id, 'name': name_or_id})
8160

61+
def find_user(identity_client, name_or_id):
62+
return _find_identity_resource(identity_client.users, name_or_id,
63+
users.User)
8264

83-
def find_project(identity_client, name_or_id):
84-
"""Find a project.
8565

86-
If the user does not have permissions to to perform a list projects
87-
call, e.g., if the user is a project admin, assume that the project
88-
given is the id rather than the name. This method is used by the role
89-
add command to allow a role to be assigned to a user by a project admin
90-
who does not have permission to list projects.
91-
"""
92-
try:
93-
project = utils.find_resource(identity_client.projects, name_or_id)
94-
if project is not None:
95-
return project
96-
except identity_exc.Forbidden:
97-
pass
98-
return projects.Project(None, {'id': name_or_id, 'name': name_or_id})
66+
def _find_identity_resource(identity_client_manager, name_or_id,
67+
resource_type):
68+
"""Find a specific identity resource.
9969
70+
Using keystoneclient's manager, attempt to find a specific resource by its
71+
name or ID. If Forbidden to find the resource (a common case if the user
72+
does not have permission), then return the resource by creating a local
73+
instance of keystoneclient's Resource.
10074
101-
def find_user(identity_client, name_or_id):
102-
"""Find a user.
75+
The parameter identity_client_manager is a keystoneclient manager,
76+
for example: keystoneclient.v3.users or keystoneclient.v3.projects.
77+
78+
The parameter resource_type is a keystoneclient resource, for example:
79+
keystoneclient.v3.users.User or keystoneclient.v3.projects.Project.
80+
81+
:param identity_client_manager: the manager that contains the resource
82+
:type identity_client_manager: `keystoneclient.base.CrudManager`
83+
:param name_or_id: the resources's name or ID
84+
:type name_or_id: string
85+
:param resource_type: class that represents the resource type
86+
:type resource_type: `keystoneclient.base.Resource`
87+
88+
:returns: the resource in question
89+
:rtype: `keystoneclient.base.Resource`
10390
104-
If the user does not have permissions to to perform a list users call,
105-
e.g., if the user is a project admin, assume that the user given is the
106-
id rather than the name. This method is used by the role add command to
107-
allow a role to be assigned to a user by a project admin who does not
108-
have permission to list users.
10991
"""
92+
11093
try:
111-
user = utils.find_resource(identity_client.users, name_or_id)
112-
if user is not None:
113-
return user
94+
identity_resource = utils.find_resource(identity_client_manager,
95+
name_or_id)
96+
if identity_resource is not None:
97+
return identity_resource
11498
except identity_exc.Forbidden:
11599
pass
116-
return users.User(None, {'id': name_or_id, 'name': name_or_id})
100+
101+
return resource_type(None, {'id': name_or_id, 'name': name_or_id})

0 commit comments

Comments
 (0)