diff options
-rw-r--r-- | novaclient/tests/test_utils.py | 13 | ||||
-rw-r--r-- | novaclient/utils.py | 16 |
2 files changed, 20 insertions, 9 deletions
diff --git a/novaclient/tests/test_utils.py b/novaclient/tests/test_utils.py index ad0dff5e..a7660b8b 100644 --- a/novaclient/tests/test_utils.py +++ b/novaclient/tests/test_utils.py @@ -42,9 +42,15 @@ class FakeManager(base.ManagerWithFind): resources = [ FakeResource('1234', {'name': 'entity_one'}), FakeResource(UUID, {'name': 'entity_two'}), - FakeResource('5678', {'name': '9876'}) + FakeResource('5678', {'name': '9876'}), + FakeResource('01234', {'name': 'entity_three'}) ] + is_alphanum_id_allowed = None + + def __init__(self, alphanum_id_allowed=False): + self.is_alphanum_id_allowed = alphanum_id_allowed + def get(self, resource_id): for resource in self.resources: if resource.id == str(resource_id): @@ -117,6 +123,11 @@ class FindResourceTestCase(test_utils.TestCase): output = utils.find_resource(display_manager, 'entity_three') self.assertEqual(output, display_manager.get('4242')) + def test_find_in_alphanum_allowd_manager_by_str_id_(self): + alphanum_manager = FakeManager(True) + output = utils.find_resource(alphanum_manager, '01234') + self.assertEqual(output, alphanum_manager.get('01234')) + class _FakeResult(object): def __init__(self, name, value): diff --git a/novaclient/utils.py b/novaclient/utils.py index 5de47869..c7f7a15f 100644 --- a/novaclient/utils.py +++ b/novaclient/utils.py @@ -254,7 +254,14 @@ def print_dict(d, dict_property="Property", dict_value="Value", wrap=0): def find_resource(manager, name_or_id, **find_args): """Helper for the _find_* methods.""" - # first try to get entity as integer id + # for str id which is not uuid (for Flavor search currently) + if getattr(manager, 'is_alphanum_id_allowed', False): + try: + return manager.get(name_or_id) + except exceptions.NotFound: + pass + + # try to get entity as integer id try: return manager.get(int(name_or_id)) except (TypeError, ValueError, exceptions.NotFound): @@ -270,13 +277,6 @@ def find_resource(manager, name_or_id, **find_args): except (TypeError, ValueError, exceptions.NotFound): pass - # for str id which is not uuid (for Flavor search currently) - if getattr(manager, 'is_alphanum_id_allowed', False): - try: - return manager.get(name_or_id) - except exceptions.NotFound: - pass - try: try: return manager.find(human_id=name_or_id, **find_args) |