Beispiel #1
0
def populate_caches(
    redis,
    user_iam_role,
    iam_sync_principals,
    dummy_users_data,
    dummy_requests_data,
    policy_requests_table,
    iamrole_table,
    create_default_resources,
    s3,
    sns,
    sqs,
    iam,
    www_user,
    parliament,
):
    from asgiref.sync import async_to_sync

    from consoleme.celery_tasks import celery_tasks as celery
    from consoleme.lib.account_indexers import get_account_id_to_name_mapping
    from consoleme_default_plugins.plugins.celery_tasks import (
        celery_tasks as default_celery_tasks,
    )

    celery.cache_cloud_account_mapping()
    accounts_d = async_to_sync(get_account_id_to_name_mapping)()
    default_celery_tasks.cache_application_information()

    for account_id in accounts_d.keys():
        celery.cache_iam_resources_for_account(account_id)
        celery.cache_s3_buckets_for_account(account_id)
        celery.cache_sns_topics_for_account(account_id)
        celery.cache_sqs_queues_for_account(account_id)
        celery.cache_managed_policies_for_account(account_id)
        # celery.cache_resources_from_aws_config_for_account(account_id) # No select_resource_config in moto yet
    # Running cache_iam_resources_across_accounts ensures that all of the pre-existing roles in our
    # role cache are stored in (mock) S3
    celery.cache_iam_resources_across_accounts()
    celery.cache_policies_table_details()
    celery.cache_policy_requests()
    celery.cache_credential_authorization_mapping()
                executor.submit(celery.cache_s3_buckets_for_account, account_id),
                executor.submit(celery.cache_sns_topics_for_account, account_id),
                executor.submit(celery.cache_sqs_queues_for_account, account_id),
                executor.submit(celery.cache_managed_policies_for_account, account_id),
                executor.submit(
                    celery.cache_resources_from_aws_config_for_account, account_id
                ),
            ]
        )
    for future in concurrent.futures.as_completed(futures):
        try:
            data = future.result()
        except Exception as exc:
            print("%r generated an exception: %s" % (future, exc))

    celery.cache_policies_table_details()
    celery.cache_policy_requests()
    celery.cache_credential_authorization_mapping()
    # Forces writing config to S3
    celery.cache_iam_resources_across_accounts(
        wait_for_subtask_completion=False, run_subtasks=False
    )
    celery.cache_s3_buckets_across_accounts(
        wait_for_subtask_completion=False, run_subtasks=False
    )
    celery.cache_sns_topics_across_accounts(
        wait_for_subtask_completion=False, run_subtasks=False
    )
    celery.cache_sqs_queues_across_accounts(
        wait_for_subtask_completion=False, run_subtasks=False
    )