Skip to content

Commit 0b88ea4

Browse files
Zuulopenstack-gerrit
authored andcommitted
Merge "identity: Remove duplicated _find_sdk_id method"
2 parents d7e7148 + 55fd501 commit 0b88ea4

4 files changed

Lines changed: 130 additions & 141 deletions

File tree

openstackclient/identity/v3/role.py

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -82,40 +82,20 @@ def _add_identity_and_resource_options_to_parser(parser):
8282
common.add_inherited_option_to_parser(parser)
8383

8484

85-
def _find_sdk_id(
86-
find_command, name_or_id, validate_actor_existence=True, **kwargs
87-
):
88-
try:
89-
resource = find_command(
90-
name_or_id=name_or_id, ignore_missing=False, **kwargs
91-
)
92-
93-
# Mimic the behavior of
94-
# openstackclient.identity.common._find_identity_resource()
95-
# and ignore if we don't have permission to find a resource.
96-
except sdk_exc.ForbiddenException:
97-
return name_or_id
98-
except sdk_exc.ResourceNotFound as exc:
99-
if not validate_actor_existence:
100-
return name_or_id
101-
raise exceptions.CommandError from exc
102-
return resource.id
103-
104-
10585
def _process_identity_and_resource_options(
10686
parsed_args, identity_client, validate_actor_existence=True
10787
):
10888
def _find_user():
10989
domain_id = (
110-
_find_sdk_id(
90+
common._find_sdk_id(
11191
identity_client.find_domain,
11292
name_or_id=parsed_args.user_domain,
11393
validate_actor_existence=validate_actor_existence,
11494
)
11595
if parsed_args.user_domain
11696
else None
11797
)
118-
return _find_sdk_id(
98+
return common._find_sdk_id(
11999
identity_client.find_user,
120100
name_or_id=parsed_args.user,
121101
validate_actor_existence=validate_actor_existence,
@@ -124,15 +104,15 @@ def _find_user():
124104

125105
def _find_group():
126106
domain_id = (
127-
_find_sdk_id(
107+
common._find_sdk_id(
128108
identity_client.find_domain,
129109
name_or_id=parsed_args.group_domain,
130110
validate_actor_existence=validate_actor_existence,
131111
)
132112
if parsed_args.group_domain
133113
else None
134114
)
135-
return _find_sdk_id(
115+
return common._find_sdk_id(
136116
identity_client.find_group,
137117
name_or_id=parsed_args.group,
138118
validate_actor_existence=validate_actor_existence,
@@ -141,15 +121,15 @@ def _find_group():
141121

142122
def _find_project():
143123
domain_id = (
144-
_find_sdk_id(
124+
common._find_sdk_id(
145125
identity_client.find_domain,
146126
name_or_id=parsed_args.project_domain,
147127
validate_actor_existence=validate_actor_existence,
148128
)
149129
if parsed_args.project_domain
150130
else None
151131
)
152-
return _find_sdk_id(
132+
return common._find_sdk_id(
153133
identity_client.find_project,
154134
name_or_id=parsed_args.project,
155135
validate_actor_existence=validate_actor_existence,
@@ -162,7 +142,7 @@ def _find_project():
162142
kwargs['system'] = parsed_args.system
163143
elif parsed_args.user and parsed_args.domain:
164144
kwargs['user'] = _find_user()
165-
kwargs['domain'] = _find_sdk_id(
145+
kwargs['domain'] = common._find_sdk_id(
166146
identity_client.find_domain,
167147
name_or_id=parsed_args.domain,
168148
validate_actor_existence=validate_actor_existence,
@@ -175,7 +155,7 @@ def _find_project():
175155
kwargs['system'] = parsed_args.system
176156
elif parsed_args.group and parsed_args.domain:
177157
kwargs['group'] = _find_group()
178-
kwargs['domain'] = _find_sdk_id(
158+
kwargs['domain'] = common._find_sdk_id(
179159
identity_client.find_domain,
180160
name_or_id=parsed_args.domain,
181161
validate_actor_existence=validate_actor_existence,
@@ -228,10 +208,10 @@ def take_action(self, parsed_args):
228208

229209
domain_id = None
230210
if parsed_args.role_domain:
231-
domain_id = _find_sdk_id(
211+
domain_id = common._find_sdk_id(
232212
identity_client.find_domain, name_or_id=parsed_args.role_domain
233213
)
234-
role = _find_sdk_id(
214+
role = common._find_sdk_id(
235215
identity_client.find_role,
236216
name_or_id=parsed_args.role,
237217
domain_id=domain_id,
@@ -328,7 +308,7 @@ def take_action(self, parsed_args):
328308

329309
create_kwargs = {}
330310
if parsed_args.domain:
331-
create_kwargs['domain_id'] = _find_sdk_id(
311+
create_kwargs['domain_id'] = common._find_sdk_id(
332312
identity_client.find_domain, name_or_id=parsed_args.domain
333313
)
334314

@@ -381,13 +361,13 @@ def take_action(self, parsed_args):
381361

382362
domain_id = None
383363
if parsed_args.domain:
384-
domain_id = _find_sdk_id(
364+
domain_id = common._find_sdk_id(
385365
identity_client.find_domain, parsed_args.domain
386366
)
387367
errors = 0
388368
for role in parsed_args.roles:
389369
try:
390-
role_id = _find_sdk_id(
370+
role_id = common._find_sdk_id(
391371
identity_client.find_role,
392372
name_or_id=role,
393373
domain_id=domain_id,
@@ -482,11 +462,11 @@ def take_action(self, parsed_args):
482462

483463
domain_id = None
484464
if parsed_args.role_domain:
485-
domain_id = _find_sdk_id(
465+
domain_id = common._find_sdk_id(
486466
identity_client.find_domain,
487467
name_or_id=parsed_args.role_domain,
488468
)
489-
role = _find_sdk_id(
469+
role = common._find_sdk_id(
490470
identity_client.find_role,
491471
name_or_id=parsed_args.role,
492472
domain_id=domain_id,
@@ -582,7 +562,7 @@ def take_action(self, parsed_args):
582562

583563
domain_id = None
584564
if parsed_args.domain:
585-
domain_id = _find_sdk_id(
565+
domain_id = common._find_sdk_id(
586566
identity_client.find_domain,
587567
name_or_id=parsed_args.domain,
588568
)
@@ -591,7 +571,7 @@ def take_action(self, parsed_args):
591571
if parsed_args.immutable is not None:
592572
update_kwargs["options"] = {"immutable": parsed_args.immutable}
593573

594-
role = _find_sdk_id(
574+
role = common._find_sdk_id(
595575
identity_client.find_role,
596576
name_or_id=parsed_args.role,
597577
domain_id=domain_id,
@@ -623,7 +603,7 @@ def take_action(self, parsed_args):
623603

624604
domain_id = None
625605
if parsed_args.domain:
626-
domain_id = _find_sdk_id(
606+
domain_id = common._find_sdk_id(
627607
identity_client.find_domain,
628608
name_or_id=parsed_args.domain,
629609
)

openstackclient/identity/v3/role_assignment.py

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
"""Identity v3 Assignment action implementations"""
1515

16-
from openstack import exceptions as sdk_exceptions
1716
from osc_lib.command import command
1817

1918
from openstackclient.i18n import _
@@ -51,15 +50,6 @@ def _get_ids(attr):
5150
)
5251

5352

54-
def _find_sdk_id(find_command, name_or_id, **kwargs):
55-
try:
56-
return find_command(
57-
name_or_id=name_or_id, ignore_missing=False, **kwargs
58-
).id
59-
except sdk_exceptions.ForbiddenException:
60-
return name_or_id
61-
62-
6353
class ListRoleAssignment(command.Lister):
6454
_description = _("List role assignments")
6555

@@ -135,34 +125,34 @@ def take_action(self, parsed_args):
135125
role_id = None
136126
role_domain_id = None
137127
if parsed_args.role_domain:
138-
role_domain_id = _find_sdk_id(
128+
role_domain_id = common._find_sdk_id(
139129
identity_client.find_domain,
140130
name_or_id=parsed_args.role_domain,
141131
)
142132
if parsed_args.role:
143-
role_id = _find_sdk_id(
133+
role_id = common._find_sdk_id(
144134
identity_client.find_role,
145135
name_or_id=parsed_args.role,
146136
domain_id=role_domain_id,
147137
)
148138

149139
user_domain_id = None
150140
if parsed_args.user_domain:
151-
user_domain_id = _find_sdk_id(
141+
user_domain_id = common._find_sdk_id(
152142
identity_client.find_domain,
153143
name_or_id=parsed_args.user_domain,
154144
)
155145

156146
user_id = None
157147
if parsed_args.user:
158-
user_id = _find_sdk_id(
148+
user_id = common._find_sdk_id(
159149
identity_client.find_user,
160150
name_or_id=parsed_args.user,
161151
domain_id=user_domain_id,
162152
)
163153
elif parsed_args.authuser:
164154
if auth_ref:
165-
user_id = _find_sdk_id(
155+
user_id = common._find_sdk_id(
166156
identity_client.find_user,
167157
name_or_id=auth_ref.user_id,
168158
)
@@ -173,21 +163,21 @@ def take_action(self, parsed_args):
173163

174164
domain_id = None
175165
if parsed_args.domain:
176-
domain_id = _find_sdk_id(
166+
domain_id = common._find_sdk_id(
177167
identity_client.find_domain,
178168
name_or_id=parsed_args.domain,
179169
)
180170

181171
project_domain_id = None
182172
if parsed_args.project_domain:
183-
project_domain_id = _find_sdk_id(
173+
project_domain_id = common._find_sdk_id(
184174
identity_client.find_domain,
185175
name_or_id=parsed_args.project_domain,
186176
)
187177

188178
project_id = None
189179
if parsed_args.project:
190-
project_id = _find_sdk_id(
180+
project_id = common._find_sdk_id(
191181
identity_client.find_project,
192182
name_or_id=common._get_token_resource(
193183
identity_client, 'project', parsed_args.project
@@ -196,21 +186,21 @@ def take_action(self, parsed_args):
196186
)
197187
elif parsed_args.authproject:
198188
if auth_ref:
199-
project_id = _find_sdk_id(
189+
project_id = common._find_sdk_id(
200190
identity_client.find_project,
201191
name_or_id=auth_ref.project_id,
202192
)
203193

204194
group_domain_id = None
205195
if parsed_args.group_domain:
206-
group_domain_id = _find_sdk_id(
196+
group_domain_id = common._find_sdk_id(
207197
identity_client.find_domain,
208198
name_or_id=parsed_args.group_domain,
209199
)
210200

211201
group_id = None
212202
if parsed_args.group:
213-
group_id = _find_sdk_id(
203+
group_id = common._find_sdk_id(
214204
identity_client.find_group,
215205
name_or_id=parsed_args.group,
216206
domain_id=group_domain_id,
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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 unittest import mock
14+
15+
from openstack import exceptions as sdk_exc
16+
from openstack.identity.v3 import user as _user
17+
from openstack.test import fakes as sdk_fakes
18+
from osc_lib import exceptions
19+
20+
from openstackclient.identity import common
21+
from openstackclient.tests.unit import utils as test_utils
22+
23+
24+
class TestFindSDKId(test_utils.TestCase):
25+
def setUp(self):
26+
super().setUp()
27+
self.user = sdk_fakes.generate_fake_resource(_user.User)
28+
self.identity_sdk_client = mock.Mock()
29+
self.identity_sdk_client.find_user = mock.Mock()
30+
31+
def test_find_sdk_id_validate(self):
32+
self.identity_sdk_client.find_user.side_effect = [self.user]
33+
34+
result = common._find_sdk_id(
35+
self.identity_sdk_client.find_user,
36+
name_or_id=self.user.id,
37+
validate_actor_existence=True,
38+
)
39+
self.assertEqual(self.user.id, result)
40+
41+
def test_find_sdk_id_no_validate(self):
42+
self.identity_sdk_client.find_user.side_effect = [self.user]
43+
44+
result = common._find_sdk_id(
45+
self.identity_sdk_client.find_user,
46+
name_or_id=self.user.id,
47+
validate_actor_existence=False,
48+
)
49+
self.assertEqual(self.user.id, result)
50+
51+
def test_find_sdk_id_not_found_validate(self):
52+
self.identity_sdk_client.find_user.side_effect = [
53+
sdk_exc.ResourceNotFound,
54+
]
55+
56+
self.assertRaises(
57+
exceptions.CommandError,
58+
common._find_sdk_id,
59+
self.identity_sdk_client.find_user,
60+
name_or_id=self.user.id,
61+
validate_actor_existence=True,
62+
)
63+
64+
def test_find_sdk_id_not_found_no_validate(self):
65+
self.identity_sdk_client.find_user.side_effect = [
66+
sdk_exc.ResourceNotFound,
67+
]
68+
69+
result = common._find_sdk_id(
70+
self.identity_sdk_client.find_user,
71+
name_or_id=self.user.id,
72+
validate_actor_existence=False,
73+
)
74+
self.assertEqual(self.user.id, result)
75+
76+
def test_find_sdk_id_forbidden_validate(self):
77+
self.identity_sdk_client.find_user.side_effect = [
78+
sdk_exc.ForbiddenException,
79+
]
80+
81+
result = common._find_sdk_id(
82+
self.identity_sdk_client.find_user,
83+
name_or_id=self.user.id,
84+
validate_actor_existence=True,
85+
)
86+
87+
self.assertEqual(self.user.id, result)
88+
89+
def test_find_sdk_id_forbidden_no_validate(self):
90+
self.identity_sdk_client.find_user.side_effect = [
91+
sdk_exc.ForbiddenException,
92+
]
93+
94+
result = common._find_sdk_id(
95+
self.identity_sdk_client.find_user,
96+
name_or_id=self.user.id,
97+
validate_actor_existence=False,
98+
)
99+
100+
self.assertEqual(self.user.id, result)

0 commit comments

Comments
 (0)