From 0e37e002ab5db85320886a3de18a2644fbe71b50 Mon Sep 17 00:00:00 2001 From: Changlong Liu Date: Fri, 29 May 2020 13:58:10 +0800 Subject: [PATCH 1/4] fix issue #11658 --- sdk/core/azure-mgmt-core/azure/mgmt/core/tools.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sdk/core/azure-mgmt-core/azure/mgmt/core/tools.py b/sdk/core/azure-mgmt-core/azure/mgmt/core/tools.py index 18502db70977..f08569440f3a 100644 --- a/sdk/core/azure-mgmt-core/azure/mgmt/core/tools.py +++ b/sdk/core/azure-mgmt-core/azure/mgmt/core/tools.py @@ -204,7 +204,9 @@ def is_valid_resource_id(rid, exception_type=None): """ is_valid = False try: - is_valid = rid and resource_id(**parse_resource_id(rid)).lower() == rid.lower() + parsed_id = parse_resource_id(rid) + is_valid = rid and resource_id(**parsed_id).lower() == rid.lower() and all( + len(v) > 0 for k, v in parsed_id.items() if rid.find('/{}/'.format(k)) >= 0) except KeyError: pass if not is_valid and exception_type: From a5b1a74d35bd641c806f15a31427482f805b48be Mon Sep 17 00:00:00 2001 From: Changlong Liu Date: Fri, 29 May 2020 15:30:22 +0800 Subject: [PATCH 2/4] use regex --- sdk/core/azure-mgmt-core/azure/mgmt/core/tools.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/sdk/core/azure-mgmt-core/azure/mgmt/core/tools.py b/sdk/core/azure-mgmt-core/azure/mgmt/core/tools.py index f08569440f3a..ce671e572516 100644 --- a/sdk/core/azure-mgmt-core/azure/mgmt/core/tools.py +++ b/sdk/core/azure-mgmt-core/azure/mgmt/core/tools.py @@ -30,13 +30,13 @@ _LOGGER = logging.getLogger(__name__) _ARMID_RE = re.compile( - "(?i)/subscriptions/(?P[^/]*)(/resourceGroups/(?P[^/]*))?" - "(/providers/(?P[^/]*)/(?P[^/]*)/(?P[^/]*)(?P.*))?" + "(?i)/subscriptions/(?P[^/]+)(/resourceGroups/(?P[^/]+))?" + "(/providers/(?P[^/]+)/(?P[^/]*)/(?P[^/]+)(?P.*))?" ) _CHILDREN_RE = re.compile( - "(?i)(/providers/(?P[^/]*))?/" - "(?P[^/]*)/(?P[^/]*)" + "(?i)(/providers/(?P[^/]+))?/" + "(?P[^/]*)/(?P[^/]+)" ) _ARMNAME_RE = re.compile("^[^<>%&:\\?/]{1,260}$") @@ -204,9 +204,7 @@ def is_valid_resource_id(rid, exception_type=None): """ is_valid = False try: - parsed_id = parse_resource_id(rid) - is_valid = rid and resource_id(**parsed_id).lower() == rid.lower() and all( - len(v) > 0 for k, v in parsed_id.items() if rid.find('/{}/'.format(k)) >= 0) + is_valid = rid and resource_id(**parse_resource_id(rid)).lower() == rid.lower() except KeyError: pass if not is_valid and exception_type: From 71128cb50a673358ccb8113b905696501fbb850d Mon Sep 17 00:00:00 2001 From: Changlong Liu Date: Thu, 4 Jun 2020 14:38:55 +0800 Subject: [PATCH 3/4] add tests --- sdk/core/azure-mgmt-core/tests/test_tools.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/sdk/core/azure-mgmt-core/tests/test_tools.py b/sdk/core/azure-mgmt-core/tests/test_tools.py index 871b78bd754f..e3c5cbb102a1 100644 --- a/sdk/core/azure-mgmt-core/tests/test_tools.py +++ b/sdk/core/azure-mgmt-core/tests/test_tools.py @@ -239,9 +239,12 @@ def test_resource_parse(self): invalid_ids = [ '/subscriptions/fakesub/resourceGroups/myRg/type1/name1', '/subscriptions/fakesub/resourceGroups/myRg/providers/Microsoft.Provider/foo', - '/subscriptions/fakesub/resourceGroups/myRg/providers/namespace/type/name/type1' + '/subscriptions/fakesub/resourceGroups/myRg/providers/namespace/type/name/type1', + '/subscriptions/fakesub/resourceGroups/', + '/subscriptions//resourceGroups/' ] for invalid_id in invalid_ids: + print(invalid_id) self.assertFalse(is_valid_resource_id(invalid_id)) tests = [ From 65322a6fb0020ff847ae948946e57aa0f9dca5e7 Mon Sep 17 00:00:00 2001 From: Changlong Liu Date: Thu, 4 Jun 2020 14:41:25 +0800 Subject: [PATCH 4/4] revert print --- sdk/core/azure-mgmt-core/tests/test_tools.py | 1 - 1 file changed, 1 deletion(-) diff --git a/sdk/core/azure-mgmt-core/tests/test_tools.py b/sdk/core/azure-mgmt-core/tests/test_tools.py index e3c5cbb102a1..831bf9b4835c 100644 --- a/sdk/core/azure-mgmt-core/tests/test_tools.py +++ b/sdk/core/azure-mgmt-core/tests/test_tools.py @@ -244,7 +244,6 @@ def test_resource_parse(self): '/subscriptions//resourceGroups/' ] for invalid_id in invalid_ids: - print(invalid_id) self.assertFalse(is_valid_resource_id(invalid_id)) tests = [