Attempt to find resource by ID, without kwargs

In keystone, to look up resources we just need the ID. Supplying
query arguments in a get() request to the client will result in
a list being performed. The resulting query will always come up
with no hits since it'll be trying to find the user "name" even
if an ID is given.

Change-Id: I111b2730eefec83968ee89aa4a56cb518ca9b203
Closes-Bug: #1597246
This commit is contained in:
Steve Martinelli 2016-06-29 09:09:43 -07:00
parent 408d88f316
commit 7a5987cacf
2 changed files with 36 additions and 9 deletions

View File

@ -335,7 +335,20 @@ class TestFindResource(test_utils.TestCase):
self.manager.get = mock.Mock(return_value=self.expected)
result = utils.find_resource(self.manager, "2")
self.assertEqual(self.expected, result)
self.manager.get.assert_called_with(2)
self.manager.get.assert_called_with("2")
def test_find_resource_get_name_and_domain(self):
name = 'admin'
domain_id = '30524568d64447fbb3fa8b7891c10dd6'
# NOTE(stevemar): we need an iterable side-effect because the same
# function (manager.get()) is used twice, the first time an exception
# will happen, then the result will be found, but only after using
# the domain ID as a query arg
side_effect = [Exception('Boom!'), self.expected]
self.manager.get = mock.Mock(side_effect=side_effect)
result = utils.find_resource(self.manager, name, domain_id=domain_id)
self.assertEqual(self.expected, result)
self.manager.get.assert_called_with(name, domain_id=domain_id)
def test_find_resource_get_uuid(self):
uuid = '9a0dc2a0-ad0d-11e3-a5e2-0800200c9a66'

View File

@ -91,7 +91,24 @@ def find_resource(manager, name_or_id, **kwargs):
"""
# Try to get entity as integer id
# Case 1: name_or_id is an ID, we need to call get() directly
# for example: /projects/454ad1c743e24edcad846d1118837cac
# For some projects, the name only will work. For keystone, this is not
# enough information, and domain information is necessary.
try:
return manager.get(name_or_id)
except Exception:
pass
# Case 2: name_or_id is a name, but we have query args in kwargs
# for example: /projects/demo&domain_id=30524568d64447fbb3fa8b7891c10dd6
try:
return manager.get(name_or_id, **kwargs)
except Exception:
pass
# Case 3: Try to get entity as integer id. Keystone does not have integer
# IDs, they are UUIDs, but some things in nova do, like flavors.
try:
if isinstance(name_or_id, int) or name_or_id.isdigit():
return manager.get(int(name_or_id), **kwargs)
@ -106,12 +123,8 @@ def find_resource(manager, name_or_id, **kwargs):
else:
raise
# Try directly using the passed value
try:
return manager.get(name_or_id, **kwargs)
except Exception:
pass
# Case 4: Try to use find.
# Reset the kwargs here for find
if len(kwargs) == 0:
kwargs = {}
@ -152,7 +165,8 @@ def find_resource(manager, name_or_id, **kwargs):
else:
pass
# for client with no find function
# Case 5: For client with no find function, list all resources and hope
# to find a matching name or ID.
count = 0
for resource in manager.list():
if (resource.get('id') == name_or_id or