From c346c7be7138edf4437ace2c7efdeb320d814332 Mon Sep 17 00:00:00 2001 From: renminmin Date: Mon, 25 Dec 2017 18:30:29 +0800 Subject: [PATCH] Fix show instance with integer name NotFound Fix trove cli 'show' instance with integer name error Change-Id: Ic6a943b4d5980017915dd2c0361636877376c5cb Closes-bug: #1740015 --- troveclient/tests/test_base.py | 4 ++++ troveclient/utils.py | 10 ++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/troveclient/tests/test_base.py b/troveclient/tests/test_base.py index e3e89707..bc409c26 100644 --- a/troveclient/tests/test_base.py +++ b/troveclient/tests/test_base.py @@ -375,6 +375,10 @@ class FindResourceTestCase(testtools.TestCase): output = utils.find_resource(self.manager, 'entity_three') self.assertEqual(self.manager.get('4242'), output) + def test_find_by_int_name(self): + output = utils.find_resource(self.manager, 9876) + self.assertEqual(self.manager.get('5678'), output) + class ResourceTest(testtools.TestCase): def setUp(self): diff --git a/troveclient/utils.py b/troveclient/utils.py index f4db7420..28096a75 100644 --- a/troveclient/utils.py +++ b/troveclient/utils.py @@ -214,10 +214,12 @@ def find_resource(manager, name_or_id): """Helper for the _find_* methods.""" # first try to get entity as integer id - # if the 'id' starts with '0' don't treat it as an int - if isinstance(name_or_id, int) or ( - name_or_id.isdigit() and not name_or_id.startswith('0')): - name_or_id = int(name_or_id) + # When the 'name_or_id' is int, covert it to string. + # Reason is that manager cannot find instance when name_or_id + # is integer and instance name is digital. + # Related to bug/1740015. + if isinstance(name_or_id, int): + name_or_id = six.text_type(name_or_id) elif sys.version_info <= (3, 0): name_or_id = encodeutils.safe_decode(name_or_id)