def get_accounts(self): for account in paginator(self.client.list_accounts): if not account.get('Status') == 'ACTIVE': LOGGER.warning('Account %s is not an Active AWS Account', account['Id']) continue self.account_ids.append(account) return self.account_ids
def list_scps(self, name): response = list(paginator(self.client.list_policies, Filter="SERVICE_CONTROL_POLICY")) print(response) try: return [policy for policy in response if policy['Name'] == name][0]['Id'] except IndexError: return []
def delete_all_base_stacks(self): for stack in paginator(self.client.list_stacks): if bool( re.search('adf-(global|regional)-base', stack.get('StackName'))): if stack.get( 'StackStatus') in StackProperties.clean_stack_status: LOGGER.warning('Removing Stack: %s', stack.get('StackName')) self.delete_stack(stack.get('StackName'))
def get_organization_map(self, org_structure, counter=0): for name, ou_id in org_structure.copy().items(): for organization_id in [organization_id['Id'] for organization_id in paginator(self.client.list_children, **{"ParentId":ou_id, "ChildType":"ORGANIZATIONAL_UNIT"})]: if organization_id in org_structure.values() and counter != 0: continue ou_name = self.describe_ou_name(organization_id) trimmed_path = Organizations.trim_policy_path("{0}/{1}".format(name, ou_name)) org_structure[trimmed_path] = organization_id counter = counter + 1 # Counter is greater than 4 here is the conditional as organizations cannot have more than 5 levels of nested OUs return org_structure if counter > 4 else self.get_organization_map(org_structure, counter)
def fetch_parameters_by_path(self, path): """Gets a Parameter(s) by Path from Parameter Store (Recursively) """ try: return paginator(self.client.get_parameters_by_path, Path=path, Recursive=True, WithDecryption=False) except self.client.exceptions.ParameterNotFound: raise ParameterNotFoundError( 'Parameter Path {0} Not Found'.format(path))
def fetch_parameters_by_path(self, path): """Gets a Parameter(s) by Path from Parameter Store (Recursively) """ try: LOGGER.debug('Fetching Parameters from path %s', path) return paginator(self.client.get_parameters_by_path, Path=path, Recursive=True, WithDecryption=False) except self.client.exceptions.ParameterNotFound as error: raise ParameterNotFoundError( f'Parameter Path {path} Not Found') from error
def get_account_ids_for_tags(self, tags): tag_filter = [] for key, value in tags.items(): if isinstance(value, list): values = value else: values = [value] tag_filter.append({'Key': key, 'Values': values}) account_ids = [] for resource in paginator(self.tags_client.get_resources, TagFilters=tag_filter, ResourceTypeFilters=['organizations']): arn = resource['ResourceARN'] account_id = arn.split('/')[::-1][0] account_ids.append(account_id) return account_ids
def get_organization_map(self, org_structure, counter=0): for name, ou_id in org_structure.copy().items(): # Skip accounts - accounts can't have children if not Organizations.is_ou_id(ou_id): continue # List OUs for organization_id in [ organization_id['Id'] for organization_id in paginator( self.client.list_children, **{ "ParentId": ou_id, "ChildType": "ORGANIZATIONAL_UNIT" }) ]: if organization_id in org_structure.values() and counter != 0: continue ou_name = self.describe_ou_name(organization_id) trimmed_path = Organizations.trim_policy_path( f"{name}/{ou_name}") org_structure[trimmed_path] = organization_id # List accounts for account_id in [ account_id['Id'] for account_id in paginator( self.client.list_children, **{ "ParentId": ou_id, "ChildType": "ACCOUNT" }) ]: if account_id in org_structure.values() and counter != 0: continue account_name = self.describe_account_name(account_id) trimmed_path = Organizations.trim_policy_path( f"{name}/{account_name}") org_structure[trimmed_path] = account_id counter = counter + 1 # Counter is greater than 5 here is the conditional as organizations cannot have more than 5 levels of nested OUs + 1 accounts "level" return org_structure if counter > 5 else self.get_organization_map( org_structure, counter)
def get_child_ous(self, parent_id): return paginator(self.client.list_organizational_units_for_parent, ParentId=parent_id)
def get_accounts_for_parent(self, parent_id): return paginator(self.client.list_accounts_for_parent, ParentId=parent_id)
def get_account_ids(self): for account in paginator(self.client.list_accounts): if account.get('Status') == 'ACTIVE': self.account_ids.append(account['Id']) return self.account_ids