Ejemplo n.º 1
0
    def run(self):
        tasks = list()
        for (
                region_name,
                shares_by_portfolio_account,
        ) in self.manifest.get_shares_by_region_portfolio_account(
                self.puppet_account_id, self.section).items():
            for (
                    portfolio_name,
                    shares_by_account,
            ) in shares_by_portfolio_account.items():
                for account_id, share in shares_by_account.items():

                    tasks.append(
                        create_share_for_account_launch_region_task.
                        CreateShareForAccountLaunchRegion(
                            manifest_file_path=self.manifest_file_path,
                            puppet_account_id=self.puppet_account_id,
                            account_id=account_id,
                            region=region_name,
                            portfolio=portfolio_name,
                            sharing_mode=share.get(self.section).get(
                                "sharing_mode",
                                config.get_global_sharing_mode_default(
                                    self.puppet_account_id),
                            ),
                        ))

        yield tasks
        self.write_output(self.params_for_results_display())
Ejemplo n.º 2
0
    def run(self):
        tasks = list()
        for (
                region_name,
                shares_by_portfolio_account,
        ) in self.manifest.get_shares_by_region_portfolio_account(
                self.puppet_account_id, self.section).items():
            for (
                    portfolio_name,
                    shares_by_account,
            ) in shares_by_portfolio_account.items():
                for account_id, share in shares_by_account.items():
                    i = "_".join([
                        str(self.puppet_account_id),
                        portfolio_name,
                        str(account_id),
                        region_name,
                    ])
                    portfolio_input = self.input().get("portfolios").get(i)

                    if portfolio_input is None:
                        raise Exception(
                            f"failed to get portfolios details for {i} in {self.input().get('portfolios')}"
                        )

                    portfolio = json.loads(portfolio_input.open("r").read())

                    tasks.append(
                        portfoliomanagement_tasks.
                        CreateShareForAccountLaunchRegion(
                            manifest_file_path=self.manifest_file_path,
                            puppet_account_id=self.puppet_account_id,
                            account_id=account_id,
                            region=region_name,
                            portfolio=portfolio_name,
                            portfolio_id=portfolio.get("portfolio_id"),
                            sharing_mode=share.get(self.section).get(
                                "sharing_mode",
                                config.get_global_sharing_mode_default(
                                    self.puppet_account_id),
                            ),
                        ))

        yield tasks
        self.write_output(self.params_for_results_display())
Ejemplo n.º 3
0
    def generate_tasks(self, task_defs):
        if len(task_defs) == 0:
            raise Exception(
                "The configuration for this share does not include any target accounts"
            )
        first_task_def = task_defs[0]
        logger.info('first_task_def')
        portfolio = first_task_def.get('portfolio')
        tasks = []

        for task_def in task_defs:
            product_generation_method = task_def.get(
                'product_generation_method', 'copy')

            if task_def.get(
                    'status'
            ) == constants.SPOKE_LOCAL_PORTFOLIO_STATUS_TERMINATED:
                tasks.append(
                    portfoliomanagement.DeletePortfolio(
                        account_id=task_def.get('account_id'),
                        region=task_def.get('region'),
                        portfolio=portfolio,
                        product_generation_method=product_generation_method,
                        puppet_account_id=task_def.get('puppet_account_id'),
                    ))
            elif task_def.get(
                    'status') == constants.SPOKE_LOCAL_PORTFOLIO_STATUS_SHARED:
                create_spoke_local_portfolio_task_params = {
                    'account_id': task_def.get('account_id'),
                    'region': task_def.get('region'),
                    'portfolio': portfolio,
                    'organization': task_def.get('organization')
                }
                create_spoke_local_portfolio_task = portfoliomanagement.CreateSpokeLocalPortfolioTask(
                    **create_spoke_local_portfolio_task_params, )
                tasks.append(create_spoke_local_portfolio_task)

                create_spoke_local_portfolio_task_as_dependency_params = {
                    'account_id': task_def.get('account_id'),
                    'region': task_def.get('region'),
                    'portfolio': portfolio,
                    'organization': task_def.get('organization'),
                }

                if len(task_def.get('associations', [])) > 0:
                    create_associations_for_portfolio_task = portfoliomanagement.CreateAssociationsForPortfolioTask(
                        **
                        create_spoke_local_portfolio_task_as_dependency_params,
                        associations=task_def.get('associations'),
                        puppet_account_id=task_def.get('puppet_account_id'),
                        should_use_sns=task_def.get('should_use_sns'),
                    )
                    tasks.append(create_associations_for_portfolio_task)

                launch_constraints = task_def.get('constraints',
                                                  {}).get('launch', [])

                if product_generation_method == 'import':
                    import_into_spoke_local_portfolio_task = portfoliomanagement.ImportIntoSpokeLocalPortfolioTask(
                        **
                        create_spoke_local_portfolio_task_as_dependency_params,
                        puppet_account_id=task_def.get('puppet_account_id'),
                    )
                    tasks.append(import_into_spoke_local_portfolio_task)
                else:
                    copy_into_spoke_local_portfolio_task = portfoliomanagement.CopyIntoSpokeLocalPortfolioTask(
                        **
                        create_spoke_local_portfolio_task_as_dependency_params,
                        puppet_account_id=task_def.get('puppet_account_id'),
                    )
                    tasks.append(copy_into_spoke_local_portfolio_task)

                if len(launch_constraints) > 0:
                    create_launch_role_constraints_for_portfolio_task_params = {
                        'launch_constraints': launch_constraints,
                        'puppet_account_id': task_def.get('puppet_account_id'),
                        'should_use_sns': task_def.get('should_use_sns'),
                    }
                    create_launch_role_constraints_for_portfolio = portfoliomanagement.CreateLaunchRoleConstraintsForPortfolio(
                        **
                        create_spoke_local_portfolio_task_as_dependency_params,
                        **
                        create_launch_role_constraints_for_portfolio_task_params,
                        product_generation_method=product_generation_method,
                    )
                    tasks.append(create_launch_role_constraints_for_portfolio)
        return tasks
    def requires(self):
        tasks = list()
        organization = self.manifest.get_account(
            self.account_id).get("organization")
        task_def = self.manifest.get(self.section_name).get(
            self.spoke_local_portfolio_name)
        product_generation_method = task_def.get(
            "product_generation_method",
            constants.PRODUCT_GENERATION_METHOD_DEFAULT)
        sharing_mode = task_def.get("sharing_mode",
                                    constants.SHARING_MODE_DEFAULT)

        if task_def.get(
                "status") == constants.SPOKE_LOCAL_PORTFOLIO_STATUS_TERMINATED:
            tasks.append(
                portfoliomanagement_tasks.DeletePortfolio(
                    manifest_file_path=self.manifest_file_path,
                    spoke_local_portfolio_name=self.spoke_local_portfolio_name,
                    account_id=self.account_id,
                    region=self.region,
                    portfolio=self.portfolio,
                    product_generation_method=product_generation_method,
                    puppet_account_id=self.puppet_account_id,
                ))
        elif task_def.get(
                "status") == constants.SPOKE_LOCAL_PORTFOLIO_STATUS_SHARED:

            create_spoke_local_portfolio_task_params = dict(
                manifest_file_path=self.manifest_file_path,
                puppet_account_id=self.puppet_account_id,
                account_id=self.account_id,
                region=self.region,
                portfolio=self.portfolio,
                organization=organization,
                sharing_mode=sharing_mode,
            )

            create_spoke_local_portfolio_task = portfoliomanagement_tasks.CreateSpokeLocalPortfolioTask(
                **create_spoke_local_portfolio_task_params)
            tasks.append(create_spoke_local_portfolio_task)

        create_spoke_local_portfolio_task_as_dependency_params = dict(
            manifest_file_path=self.manifest_file_path,
            account_id=self.account_id,
            region=self.region,
            portfolio=self.portfolio,
            organization=organization,
        )

        if len(task_def.get("associations", [])) > 0:
            create_associations_for_portfolio_task = portfoliomanagement_tasks.CreateAssociationsForSpokeLocalPortfolioTask(
                **create_spoke_local_portfolio_task_as_dependency_params,
                spoke_local_portfolio_name=self.spoke_local_portfolio_name,
                sharing_mode=sharing_mode,
                associations=task_def.get("associations"),
                puppet_account_id=self.puppet_account_id,
            )
            tasks.append(create_associations_for_portfolio_task)

        launch_constraints = task_def.get("constraints", {}).get("launch", [])

        if product_generation_method == "import":
            import_into_spoke_local_portfolio_task = portfoliomanagement_tasks.ImportIntoSpokeLocalPortfolioTask(
                **create_spoke_local_portfolio_task_as_dependency_params,
                spoke_local_portfolio_name=self.spoke_local_portfolio_name,
                sharing_mode=sharing_mode,
                puppet_account_id=self.puppet_account_id,
            )
            tasks.append(import_into_spoke_local_portfolio_task)
        else:
            copy_into_spoke_local_portfolio_task = portfoliomanagement_tasks.CopyIntoSpokeLocalPortfolioTask(
                **create_spoke_local_portfolio_task_as_dependency_params,
                spoke_local_portfolio_name=self.spoke_local_portfolio_name,
                sharing_mode=sharing_mode,
                puppet_account_id=self.puppet_account_id,
            )
            tasks.append(copy_into_spoke_local_portfolio_task)

        if len(launch_constraints) > 0:
            create_launch_role_constraints_for_portfolio_task_params = dict(
                launch_constraints=launch_constraints,
                puppet_account_id=self.puppet_account_id,
            )
            create_launch_role_constraints_for_portfolio = portfoliomanagement_tasks.CreateLaunchRoleConstraintsForSpokeLocalPortfolioTask(
                **create_spoke_local_portfolio_task_as_dependency_params,
                **create_launch_role_constraints_for_portfolio_task_params,
                spoke_local_portfolio_name=self.spoke_local_portfolio_name,
                sharing_mode=sharing_mode,
                product_generation_method=product_generation_method,
            )
            tasks.append(create_launch_role_constraints_for_portfolio)
        return tasks