From 74559f2a91197aa38ae768910efe4379e585e1ce Mon Sep 17 00:00:00 2001 From: Bert Blommers Date: Sat, 5 Jun 2021 17:53:06 +0100 Subject: [PATCH] Organisations - Backport re.fullmatch for Py2 (#3990) --- moto/organizations/models.py | 10 +++++----- moto/organizations/utils.py | 8 ++++++++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/moto/organizations/models.py b/moto/organizations/models.py index 09d94f80..0b21a5eb 100644 --- a/moto/organizations/models.py +++ b/moto/organizations/models.py @@ -657,13 +657,13 @@ class OrganizationsBackend(BaseBackend): ) def _get_resource_for_tagging(self, resource_id): - if re.compile(utils.OU_ID_REGEX).fullmatch(resource_id) or re.fullmatch( - utils.ROOT_ID_REGEX, resource_id - ): + if utils.fullmatch( + re.compile(utils.OU_ID_REGEX), resource_id + ) or utils.fullmatch(utils.ROOT_ID_REGEX, resource_id): resource = next((a for a in self.ou if a.id == resource_id), None) - elif re.compile(utils.ACCOUNT_ID_REGEX).fullmatch(resource_id): + elif utils.fullmatch(re.compile(utils.ACCOUNT_ID_REGEX), resource_id): resource = next((a for a in self.accounts if a.id == resource_id), None) - elif re.compile(utils.POLICY_ID_REGEX).fullmatch(resource_id): + elif utils.fullmatch(re.compile(utils.POLICY_ID_REGEX), resource_id): resource = next((a for a in self.policies if a.id == resource_id), None) else: raise InvalidInputException( diff --git a/moto/organizations/utils.py b/moto/organizations/utils.py index cec34834..dc0686cd 100644 --- a/moto/organizations/utils.py +++ b/moto/organizations/utils.py @@ -1,6 +1,7 @@ from __future__ import unicode_literals import random +import re import string from moto.core import ACCOUNT_ID @@ -84,3 +85,10 @@ def make_random_policy_id(): # from 8 to 128 lower-case letters or digits. # e.g. 'p-k2av4a8a' return "p-" + "".join(random.choice(CHARSET) for x in range(POLICY_ID_SIZE)) + + +def fullmatch(regex, s, flags=0): + """Emulate python-3.4 re.fullmatch().""" + m = re.match(regex, s, flags=flags) + if m and m.span()[1] == len(s): + return m