Skip to content

Commit 55b37d5

Browse files
Hidekazu Nakamuraedmondsw
authored andcommitted
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
1 parent 061037a commit 55b37d5

2 files changed

Lines changed: 21 additions & 16 deletions

File tree

openstackclient/common/utils.py

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -100,22 +100,15 @@ def find_resource(manager, name_or_id, **kwargs):
100100
else:
101101
pass
102102

103-
try:
104-
for resource in manager.list():
105-
# short circuit and return the first match
106-
if (resource.get('id') == name_or_id or
107-
resource.get('name') == name_or_id):
108-
return resource
109-
else:
110-
# we found no match, keep going to bomb out
111-
pass
112-
except Exception:
113-
# in case the list fails for some reason
114-
pass
115-
116-
# if we hit here, we've failed, report back this error:
117-
msg = "Could not find resource %s" % name_or_id
118-
raise exceptions.CommandError(msg)
103+
for resource in manager.list():
104+
# short circuit and return the first match
105+
if (resource.get('id') == name_or_id or
106+
resource.get('name') == name_or_id):
107+
return resource
108+
else:
109+
# we found no match, report back this error:
110+
msg = "Could not find resource %s" % name_or_id
111+
raise exceptions.CommandError(msg)
119112

120113

121114
def format_dict(data):

openstackclient/tests/common/test_utils.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,18 @@ def test_find_resource_find_not_found(self):
306306
self.manager.get.assert_called_with(self.name)
307307
self.manager.find.assert_called_with(name=self.name)
308308

309+
def test_find_resource_list_forbidden(self):
310+
self.manager.get = mock.Mock(side_effect=Exception('Boom!'))
311+
self.manager.find = mock.Mock(side_effect=Exception('Boom!'))
312+
self.manager.list = mock.Mock(
313+
side_effect=exceptions.Forbidden(403)
314+
)
315+
self.assertRaises(exceptions.Forbidden,
316+
utils.find_resource,
317+
self.manager,
318+
self.name)
319+
self.manager.list.assert_called_with()
320+
309321
def test_find_resource_find_no_unique(self):
310322
self.manager.get = mock.Mock(side_effect=Exception('Boom!'))
311323
self.manager.find = mock.Mock(side_effect=NoUniqueMatch())

0 commit comments

Comments
 (0)