Skip to content

Commit 81fd5c9

Browse files
Updated the take_actions for unified limits
When user passes --region None, the find_resource of osc_lib calls get() of region. The get API of region ignores the name param returning all the regions in result. As the find_resource checks many cases against the result returned by get API. The output comes greater than 1, thus returning "More than one region ID exist" which is incorrect. However in case of region which cannot be filtered by name we do not require to check these many cases. The solution is to directly call the get method of APIs and returning No resource name exist with the xyz" on passing invaid parameter. And returning all in case of None. Thus created a new function get_resource which can be used in future too by these types of API's. Change-Id: Ib3f881d34a82af97199ce51bfbefc6f3f08599f1 Closes-bug: #1799153
1 parent 097b456 commit 81fd5c9

3 files changed

Lines changed: 84 additions & 12 deletions

File tree

openstackclient/identity/common.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,25 @@ def find_service(identity_client, name_type_or_id):
6868
raise exceptions.CommandError(msg % name_type_or_id)
6969

7070

71+
def get_resource(manager, name_type_or_id):
72+
# NOTE (vishakha): Due to bug #1799153 and for any another related case
73+
# where GET resource API does not support the filter by name,
74+
# osc_lib.utils.find_resource() method cannot be used because that method
75+
# try to fall back to list all the resource if requested resource cannot
76+
# be get via name. Which ends up with NoUniqueMatch error.
77+
# This new function is the replacement for osc_lib.utils.find_resource()
78+
# for resources does not support GET by name.
79+
# For example: identity GET /regions.
80+
"""Find a resource by id or name."""
81+
82+
try:
83+
return manager.get(name_type_or_id)
84+
except identity_exc.NotFound:
85+
# raise NotFound exception
86+
msg = _("No resource with name or id of '%s' exists")
87+
raise exceptions.CommandError(msg % name_type_or_id)
88+
89+
7190
def _get_token_resource(client, resource, parsed_name, parsed_domain=None):
7291
"""Peek into the user's auth token to get resource IDs
7392

openstackclient/identity/v3/limit.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,19 @@ def take_action(self, parsed_args):
7878
)
7979
region = None
8080
if parsed_args.region:
81-
region = utils.find_resource(
82-
identity_client.regions, parsed_args.region
83-
)
81+
val = getattr(parsed_args, 'region', None)
82+
if 'None' not in val:
83+
# NOTE (vishakha): Due to bug #1799153 and for any another
84+
# related case where GET resource API does not support the
85+
# filter by name, osc_lib.utils.find_resource() method cannot
86+
# be used because that method try to fall back to list all the
87+
# resource if requested resource cannot be get via name. Which
88+
# ends up with NoUniqueMatch error.
89+
# So osc_lib.utils.find_resource() function cannot be used for
90+
# 'regions', using common_utils.get_resource() instead.
91+
region = common_utils.get_resource(
92+
identity_client.regions, parsed_args.region
93+
)
8494

8595
limit = identity_client.limits.create(
8696
project,
@@ -136,6 +146,19 @@ def take_action(self, parsed_args):
136146
region = utils.find_resource(
137147
identity_client.regions, parsed_args.region
138148
)
149+
val = getattr(parsed_args, 'region', None)
150+
if 'None' not in val:
151+
# NOTE (vishakha): Due to bug #1799153 and for any another
152+
# related case where GET resource API does not support the
153+
# filter by name, osc_lib.utils.find_resource() method cannot
154+
# be used because that method try to fall back to list all the
155+
# resource if requested resource cannot be get via name. Which
156+
# ends up with NoUniqueMatch error.
157+
# So osc_lib.utils.find_resource() function cannot be used for
158+
# 'regions', using common_utils.get_resource() instead.
159+
region = common_utils.get_resource(
160+
identity_client.regions, parsed_args.region
161+
)
139162
project = None
140163
if parsed_args.project:
141164
project = utils.find_resource(

openstackclient/identity/v3/registered_limit.py

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,19 @@ def take_action(self, parsed_args):
6969
)
7070
region = None
7171
if parsed_args.region:
72-
region = utils.find_resource(
73-
identity_client.regions, parsed_args.region
74-
)
72+
val = getattr(parsed_args, 'region', None)
73+
if 'None' not in val:
74+
# NOTE (vishakha): Due to bug #1799153 and for any another
75+
# related case where GET resource API does not support the
76+
# filter by name, osc_lib.utils.find_resource() method cannot
77+
# be used because that method try to fall back to list all the
78+
# resource if requested resource cannot be get via name. Which
79+
# ends up with NoUniqueMatch error.
80+
# So osc_lib.utils.find_resource() function cannot be used for
81+
# 'regions', using common_utils.get_resource() instead.
82+
region = common_utils.get_resource(
83+
identity_client.regions, parsed_args.region
84+
)
7585

7686
registered_limit = identity_client.registered_limits.create(
7787
service,
@@ -153,9 +163,19 @@ def take_action(self, parsed_args):
153163
)
154164
region = None
155165
if parsed_args.region:
156-
region = utils.find_resource(
157-
identity_client.regions, parsed_args.region
158-
)
166+
val = getattr(parsed_args, 'region', None)
167+
if 'None' not in val:
168+
# NOTE (vishakha): Due to bug #1799153 and for any another
169+
# related case where GET resource API does not support the
170+
# filter by name, osc_lib.utils.find_resource() method cannot
171+
# be used because that method try to fall back to list all the
172+
# resource if requested resource cannot be get via name. Which
173+
# ends up with NoUniqueMatch error.
174+
# So osc_lib.utils.find_resource() function cannot be used for
175+
# 'regions', using common_utils.get_resource() instead.
176+
region = common_utils.get_resource(
177+
identity_client.regions, parsed_args.region
178+
)
159179

160180
registered_limits = identity_client.registered_limits.list(
161181
service=service,
@@ -222,9 +242,19 @@ def take_action(self, parsed_args):
222242

223243
region = None
224244
if parsed_args.region:
225-
region = utils.find_resource(
226-
identity_client.regions, parsed_args.region
227-
)
245+
val = getattr(parsed_args, 'region', None)
246+
if 'None' not in val:
247+
# NOTE (vishakha): Due to bug #1799153 and for any another
248+
# related case where GET resource API does not support the
249+
# filter by name, osc_lib.utils.find_resource() method cannot
250+
# be used because that method try to fall back to list all the
251+
# resource if requested resource cannot be get via name. Which
252+
# ends up with NoUniqueMatch error.
253+
# So osc_lib.utils.find_resource() function cannot be used for
254+
# 'regions', using common_utils.get_resource() instead.
255+
region = common_utils.get_resource(
256+
identity_client.regions, parsed_args.region
257+
)
228258

229259
registered_limit = identity_client.registered_limits.update(
230260
parsed_args.registered_limit_id,

0 commit comments

Comments
 (0)