Don't mask authorization errors

Project show with name argument returns 'Could not find resource'
error when the user is not authorized. It should report the
authorization error instead. This patch makes that change.

Change-Id: Iac3521f8a411060b0ec9ef46c8f0e1f3551e56ae
Closes-Bug: #1511625
This commit is contained in:
Hidekazu Nakamura 2015-11-06 00:47:38 +09:00 committed by Matthew Edmonds
parent 061037aaf1
commit 55b37d5e33
2 changed files with 21 additions and 16 deletions
openstackclient
common
tests/common

@ -100,22 +100,15 @@ def find_resource(manager, name_or_id, **kwargs):
else: else:
pass pass
try: for resource in manager.list():
for resource in manager.list(): # short circuit and return the first match
# short circuit and return the first match if (resource.get('id') == name_or_id or
if (resource.get('id') == name_or_id or resource.get('name') == name_or_id):
resource.get('name') == name_or_id): return resource
return resource else:
else: # we found no match, report back this error:
# we found no match, keep going to bomb out msg = "Could not find resource %s" % name_or_id
pass raise exceptions.CommandError(msg)
except Exception:
# in case the list fails for some reason
pass
# if we hit here, we've failed, report back this error:
msg = "Could not find resource %s" % name_or_id
raise exceptions.CommandError(msg)
def format_dict(data): def format_dict(data):

@ -306,6 +306,18 @@ class TestFindResource(test_utils.TestCase):
self.manager.get.assert_called_with(self.name) self.manager.get.assert_called_with(self.name)
self.manager.find.assert_called_with(name=self.name) self.manager.find.assert_called_with(name=self.name)
def test_find_resource_list_forbidden(self):
self.manager.get = mock.Mock(side_effect=Exception('Boom!'))
self.manager.find = mock.Mock(side_effect=Exception('Boom!'))
self.manager.list = mock.Mock(
side_effect=exceptions.Forbidden(403)
)
self.assertRaises(exceptions.Forbidden,
utils.find_resource,
self.manager,
self.name)
self.manager.list.assert_called_with()
def test_find_resource_find_no_unique(self): def test_find_resource_find_no_unique(self):
self.manager.get = mock.Mock(side_effect=Exception('Boom!')) self.manager.get = mock.Mock(side_effect=Exception('Boom!'))
self.manager.find = mock.Mock(side_effect=NoUniqueMatch()) self.manager.find = mock.Mock(side_effect=NoUniqueMatch())