def run(self):
        self.info("running")
        for region_name, accounts in self.manifest.get_accounts_by_region(
        ).items():
            yield general_tasks.DeleteCloudFormationStackTask(
                account_id=self.puppet_account_id,
                region=region_name,
                stack_name="servicecatalog-puppet-shares",
            )

        for (
                region_name,
                sharing_policies,
        ) in self.manifest.get_sharing_policies_by_region().items():
            yield EnsureEventBridgeEventBusTask(
                puppet_account_id=self.puppet_account_id,
                region=region_name,
            )

            yield GeneratePolicies(
                manifest_file_path=self.manifest_file_path,
                puppet_account_id=self.puppet_account_id,
                should_use_sns=self.should_use_sns,
                should_use_product_plans=self.should_use_product_plans,
                include_expanded_from=self.include_expanded_from,
                single_account=self.single_account,
                is_dry_run=self.is_dry_run,
                execution_mode=self.execution_mode,
                region=region_name,
                sharing_policies=sharing_policies,
            )

        self.info("Finished generating policies")
        for (
                region_name,
                shares_by_portfolio_account,
        ) in self.manifest.get_shares_by_region_portfolio_account(
                self.puppet_account_id).items():
            for (
                    portfolio_name,
                    shares_by_account,
            ) in shares_by_portfolio_account.items():
                for account_id, share in shares_by_account.items():
                    yield 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,
                    )

        self.write_output(self.params_for_results_display())
def generate_shares(f):
    logger.info('Starting to generate shares for: {}'.format(f.name))
    tasks_to_run = []
    puppet_account_id = config.get_puppet_account_id()
    manifest = manifest_utils.load(f)

    task_defs = manifest_utils.convert_manifest_into_task_defs_for_launches(
        manifest, puppet_account_id, False, False, include_expanded_from=True)
    for task in task_defs:
        tasks_to_run.append(
            portfoliomanagement_tasks.CreateShareForAccountLaunchRegion(
                puppet_account_id=puppet_account_id,
                account_id=task.get('account_id'),
                region=task.get('region'),
                portfolio=task.get('portfolio'),
                expanded_from=task.get('expanded_from'),
                organization=task.get('organization'),
            ))

    spoke_local_portfolios_tasks = manifest_utils.convert_manifest_into_task_defs_for_spoke_local_portfolios(
        manifest, puppet_account_id, False, tasks_to_run)

    for task in spoke_local_portfolios_tasks:
        if isinstance(task,
                      portfoliomanagement_tasks.CreateSpokeLocalPortfolioTask):
            param_kwargs = task.param_kwargs
            tasks_to_run.append(
                portfoliomanagement_tasks.CreateShareForAccountLaunchRegion(
                    puppet_account_id=puppet_account_id,
                    account_id=param_kwargs.get('account_id'),
                    region=param_kwargs.get('region'),
                    portfolio=param_kwargs.get('portfolio'),
                    expanded_from=param_kwargs.get('expanded_from'),
                    organization=param_kwargs.get('organization'),
                ))

    runner.run_tasks_for_generate_shares(tasks_to_run)
Beispiel #3
0
def generate_shares(f):
    logger.info('Starting to generate shares for: {}'.format(f.name))
    tasks_to_run = []
    puppet_account_id = config.get_puppet_account_id()
    manifest = manifest_utils.load(f)
    accounts_by_id = {}
    for account in manifest.get('accounts'):
        accounts_by_id[account.get('account_id')] = account

    launch_tasks = manifest_utils_for_launches.generate_launch_tasks(
        manifest, puppet_account_id, False, False, include_expanded_from=False, single_account=None, is_dry_run=False
    )

    for launch_task in launch_tasks:
        t = manifest_utils_for_launches.generate_launch_task_defs_for_launch(
            launch_task.launch_name,
            launch_task.manifest,
            launch_task.puppet_account_id,
            launch_task.should_use_sns,
            launch_task.should_use_product_plans,
            launch_task.include_expanded_from,
            launch_task.single_account,
            launch_task.is_dry_run
        )
        tasks = launch_task.generate_provisions(t.get('task_defs'))
        for task_ in tasks:
            task = task_.param_kwargs
            a_id = task.get('account_id')
            tasks_to_run.append(
                portfoliomanagement_tasks.CreateShareForAccountLaunchRegion(
                    puppet_account_id=puppet_account_id,
                    account_id=a_id,
                    region=task.get('region'),
                    portfolio=task.get('portfolio'),
                    expanded_from=accounts_by_id.get(a_id).get('expanded_from'),
                    organization=accounts_by_id.get(a_id).get('organization'),
                )
            )

    spoke_local_portfolios_tasks = manifest_utils_for_spoke_local_portfolios.generate_spoke_local_portfolios_tasks(
        manifest, puppet_account_id, False, False, include_expanded_from=False, single_account=None, is_dry_run=False
    )

    # TODO fixme
    for spoke_local_portfolios_task in spoke_local_portfolios_tasks:
        t = manifest_utils_for_spoke_local_portfolios.generate_spoke_local_portfolios_tasks_for_spoke_local_portfolio(
            spoke_local_portfolios_task.spoke_local_portfolio_name,
            spoke_local_portfolios_task.manifest,
            spoke_local_portfolios_task.puppet_account_id,
            spoke_local_portfolios_task.should_use_sns,
            spoke_local_portfolios_task.should_use_product_plans,
            spoke_local_portfolios_task.include_expanded_from,
            spoke_local_portfolios_task.single_account,
            spoke_local_portfolios_task.is_dry_run,
        )

        sub_tasks = spoke_local_portfolios_task.generate_tasks(t.get('task_defs'))
        for sub_task in sub_tasks:
            if isinstance(sub_task, portfoliomanagement_tasks.CreateSpokeLocalPortfolioTask):
                param_kwargs = sub_task.param_kwargs
                a_id = param_kwargs.get('account_id')
                tasks_to_run.append(
                    portfoliomanagement_tasks.CreateShareForAccountLaunchRegion(
                        puppet_account_id=puppet_account_id,
                        account_id=param_kwargs.get('account_id'),
                        region=param_kwargs.get('region'),
                        portfolio=param_kwargs.get('portfolio'),
                        expanded_from=accounts_by_id.get(a_id).get('expanded_from'),
                        organization=accounts_by_id.get(a_id).get('organization'),
                    )
                )

    runner.run_tasks_for_generate_shares(tasks_to_run)