def __process_configuration()

in control-tower-account-factory/src/handler.py [0:0]


    def __process_configuration(self):
        """process products that need to be provision or update across accounts"""

        self._log_info('Start process update configuration')
        products_to_provision = {}
        products_to_update = {}

        for product in self.configuration['products']:
            if 'product_name' in product and 'portfolio_name' in product and 'provision_name' in product and 'regions' in product and ('accounts' in product or 'organization_units' in product):
                # check if product version provided in configuration
                product_version = product['product_version'] if 'product_version' in product else None
                # obtain product id and artifact id
                product_id, artifact_id, path_id, version = self._get_product_id(product['product_name'], product_version, product['portfolio_name'])
                if product_id and artifact_id and path_id:
                    accounts = (product['accounts'] if 'accounts' in product else None)
                    organization_unit = (product['organization_units'] if 'organization_units' in product else None)
                    # get provisioned names for product
                    provision_name_list = self.__get_product_provision_list(product_id)
                    # get list of accounts where product need to be updated
                    account_list = self.__get_account_list(accounts, organization_unit)
                    for account in account_list:
                        # update product stackset constrain
                        if self._update_product_constraint(product_id, product['portfolio_name'], product['regions'], account):
                            provision_name = f'{account}-{product["provision_name"]}'
                            # make copy of product record
                            deploy_product = copy.deepcopy(product)
                            update_product = copy.deepcopy(product)
                            # if account on the list - update account
                            if provision_name in provision_name_list:
                                stack_set_name = provision_name_list[provision_name]['stack_set_name']
                                regions_list = self.__get_product_deployment_regions(stack_set_name)

                                update_regions = []
                                # check in which regions product was deployed
                                for region in product['regions']:
                                    # initialize new deployment in missin regions
                                    if region not in regions_list:
                                        if 'deployifnotexist' in product and product['deployifnotexist'] == True:
                                            self._log_info(f'Creating new stack instance for: {product["product_name"]} in {account} {region}')
                                            self.__add_stack_instance(stack_set_name, account, region)
                                        else:
                                            self._log_info(f'Skipped deployment for: {product["product_name"]} in {account} {region}')
                                    else:
                                        update_regions.append(region)

                                # for existing regions processd with updates
                                if update_regions:
                                    update_product['id'] = product_id
                                    update_product['artifact'] = artifact_id
                                    update_product['path'] = path_id
                                    update_product['provision_id'] = provision_name_list[provision_name]['id']
                                    update_product['regions'] = update_regions
                                    update_product['account'] = account
                                    update_product['version'] = version
                                    update_product['provision_name'] = provision_name

                                    if account in products_to_update:
                                        products_to_update[account].append(update_product)
                                    else:
                                        products_to_update[account] = [update_product]

                                    if account not in self.account_to_process:
                                        self.account_to_process.append(account)
                            else:
                                if 'deployifnotexist' in product and product['deployifnotexist'] == True:
                                    # if this is new account, new deployment will be created
                                    deploy_product['id'] = product_id
                                    deploy_product['artifact'] = artifact_id
                                    deploy_product['path'] = path_id
                                    deploy_product['account'] = account
                                    deploy_product['version'] = version
                                    deploy_product['provision_name'] = provision_name

                                    if account in products_to_provision:
                                        products_to_provision[account].append(deploy_product)
                                    else:
                                        products_to_provision[account] = [deploy_product]

                                    if account not in self.account_to_process:
                                        self.account_to_process.append(account)
                                else:
                                    self._log_info(f'Skipped deployment for: {product["product_name"]} in {account}')


        return  products_to_provision, products_to_update