From 3dc010d868da91a68d1c9e92166bf1513a469a31 Mon Sep 17 00:00:00 2001 From: Costas Tyfoxylos Date: Mon, 27 Nov 2023 13:40:39 +0100 Subject: [PATCH 1/2] Filter out gov regions by default. --- awsapilib/controltower/controltower.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/awsapilib/controltower/controltower.py b/awsapilib/controltower/controltower.py index 99466d5..79129a6 100755 --- a/awsapilib/controltower/controltower.py +++ b/awsapilib/controltower/controltower.py @@ -193,9 +193,12 @@ def region(self): return self._region @staticmethod - def get_available_regions(): + def get_available_regions(enable_gov_regions=False): """The regions that control tower can be active in. + Args: + enable_gov_regions: A boolean flag indicating whether to enable the gov regions + Returns: regions (list): A list of strings of the regions that control tower can be active in. @@ -205,9 +208,12 @@ def get_available_regions(): if not response.ok: LOGGER.error('Failed to retrieve the info') return [] - return [entry.get('id', '').split(':')[1] - for entry in response.json().get('prices') - if entry.get('id').startswith('controltower')] + regions = [entry.get('id', '').split(':')[1] + for entry in response.json().get('prices') + if entry.get('id').startswith('controltower')] + if not enable_gov_regions: + regions = [region for region in regions if 'gov' not in region.lower()] + return regions @property @validate_availability @@ -695,6 +701,7 @@ def get_ou_ids(parent_id): result.extend(get_ou_ids(org_ou.id)) except TypeError: continue + get_ou_ids(self.root_ou.id) return result @@ -1223,7 +1230,8 @@ def deploy(self, # pylint: disable=too-many-arguments,too-many-locals custom_ou_name: str = 'Sandbox', regions: Optional[list] = None, retries: int = 10, - wait: int = 1) -> bool: + wait: int = 1, + enable_gov_regions=False) -> bool: """Deploys control tower. Returns: @@ -1235,7 +1243,7 @@ def deploy(self, # pylint: disable=too-many-arguments,too-many-locals return True regions = self._validate_regions(regions or [self.region]) region_list = [{"Region": region, "RegionConfigurationStatus": "ENABLED" if region in regions else "DISABLED"} - for region in self.get_available_regions()] + for region in self.get_available_regions(enable_gov_regions)] validation = self._pre_deploy_check() self.logger.debug('Got validation response %s.', validation) if not all(list(entry.values()).pop().get('Result') == 'SUCCESS' for entry in validation): From 425a354f5038c9263176a0c47f4a25cab9f7e8c1 Mon Sep 17 00:00:00 2001 From: Costas Tyfoxylos Date: Tue, 28 Nov 2023 16:28:45 +0100 Subject: [PATCH 2/2] Filter out gov regions. --- awsapilib/controltower/controltower.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/awsapilib/controltower/controltower.py b/awsapilib/controltower/controltower.py index 79129a6..f5c660a 100755 --- a/awsapilib/controltower/controltower.py +++ b/awsapilib/controltower/controltower.py @@ -193,12 +193,9 @@ def region(self): return self._region @staticmethod - def get_available_regions(enable_gov_regions=False): + def get_available_regions(): """The regions that control tower can be active in. - Args: - enable_gov_regions: A boolean flag indicating whether to enable the gov regions - Returns: regions (list): A list of strings of the regions that control tower can be active in. @@ -210,9 +207,8 @@ def get_available_regions(enable_gov_regions=False): return [] regions = [entry.get('id', '').split(':')[1] for entry in response.json().get('prices') - if entry.get('id').startswith('controltower')] - if not enable_gov_regions: - regions = [region for region in regions if 'gov' not in region.lower()] + if all([entry.get('id').startswith('controltower'), + 'gov' not in entry.get('id')])] return regions @property @@ -1230,8 +1226,7 @@ def deploy(self, # pylint: disable=too-many-arguments,too-many-locals custom_ou_name: str = 'Sandbox', regions: Optional[list] = None, retries: int = 10, - wait: int = 1, - enable_gov_regions=False) -> bool: + wait: int = 1) -> bool: """Deploys control tower. Returns: @@ -1243,7 +1238,7 @@ def deploy(self, # pylint: disable=too-many-arguments,too-many-locals return True regions = self._validate_regions(regions or [self.region]) region_list = [{"Region": region, "RegionConfigurationStatus": "ENABLED" if region in regions else "DISABLED"} - for region in self.get_available_regions(enable_gov_regions)] + for region in self.get_available_regions()] validation = self._pre_deploy_check() self.logger.debug('Got validation response %s.', validation) if not all(list(entry.values()).pop().get('Result') == 'SUCCESS' for entry in validation):