summaryrefslogtreecommitdiff
path: root/openstackclient
diff options
context:
space:
mode:
Diffstat (limited to 'openstackclient')
-rw-r--r--openstackclient/common/utils.py25
-rw-r--r--openstackclient/tests/common/test_utils.py12
2 files changed, 21 insertions, 16 deletions
diff --git a/openstackclient/common/utils.py b/openstackclient/common/utils.py
index c6ed6a71..daa65c25 100644
--- a/openstackclient/common/utils.py
+++ b/openstackclient/common/utils.py
@@ -100,22 +100,15 @@ def find_resource(manager, name_or_id, **kwargs):
else:
pass
- try:
- for resource in manager.list():
- # short circuit and return the first match
- if (resource.get('id') == name_or_id or
- resource.get('name') == name_or_id):
- return resource
- else:
- # we found no match, keep going to bomb out
- pass
- 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)
+ for resource in manager.list():
+ # short circuit and return the first match
+ if (resource.get('id') == name_or_id or
+ resource.get('name') == name_or_id):
+ return resource
+ else:
+ # we found no match, report back this error:
+ msg = "Could not find resource %s" % name_or_id
+ raise exceptions.CommandError(msg)
def format_dict(data):
diff --git a/openstackclient/tests/common/test_utils.py b/openstackclient/tests/common/test_utils.py
index 95bce458..2248d043 100644
--- a/openstackclient/tests/common/test_utils.py
+++ b/openstackclient/tests/common/test_utils.py
@@ -306,6 +306,18 @@ class TestFindResource(test_utils.TestCase):
self.manager.get.assert_called_with(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):
self.manager.get = mock.Mock(side_effect=Exception('Boom!'))
self.manager.find = mock.Mock(side_effect=NoUniqueMatch())