Repository: Netflix/consoleme
File: consoleme/celery/celery_tasks.py (Python)

Code before:
    if (
            config.region == config.get("celery.active_region")
            and config.get("environment") == "prod"
    ):

        @sts_conn('iam')
        def _get_delivery_channels(**kwargs) -> list:
            """Gets the delivery channels in the account/region -- calls are wrapped with CloudAux"""
            return kwargs.pop("client").get_account_summary(**kwargs)

        if config.region == config.get("celery.active_region"):
            accounts_d: list = aws.get_account_ids_to_names()
            for account_id in accounts_d.keys():
                iam_summary = _get_delivery_channels(
                    account_number=account_id,
                    assume_role=config.get("policies.role_name"),
                    region=config.region,
                )
                log_data = {
                    "function": function,
                    "message": "Total number of IAM roles and IAM roles quota.",
                    "iam_roles": iam_summary["SummaryMap"]["Roles"],
                    "iam_quota": iam_summary["SummaryMap"]["RolesQuota"],
                    "aws_account": account_id,
                }
                stats.count(f"{function}.success", tags={"total_iam_roles": iam_summary["SummaryMap"]["Roles"],
                                                         "role_quota": iam_summary["SummaryMap"]["RolesQuota"],
                                                         "account_id": account_id})
                log.debug(log_data)

    log_data = {
        "function": function,
        "message": "Completed : looking for IAM Role limits",
    }
    log.debug(log_data)
    stats.count(f"{function}.success")
    return True


schedule_30_minute = timedelta(seconds=1800)
schedule_45_minute = timedelta(seconds=2700)
schedule_6_hours = timedelta(hours=6)
schedule_minute = timedelta(minutes=1)
schedule_5_minutes = timedelta(minutes=5)
schedule_24_hours = timedelta(hours=24)

if config.get("development", False):
    # If debug mode, we will set up the schedule to run the next minute after the job starts
    time_to_start = datetime.utcnow() + timedelta(minutes=1)
    dev_schedule = crontab(hour=time_to_start.hour, minute=time_to_start.minute)
    schedule_30_minute = dev_schedule

Review comment (refactor):
Try to make naming as consistent as possible. For example, in log data it's called `iam_roles`, in the stats.count tag, it's called "total_iam_roles". Same for "iam_quota". Making the names consistent here helps with readability and maintainability, and removes another layer of abstraction

Code after:
    ) in ["prod", "dev"]:

        @sts_conn("iam")
        def _get_delivery_channels(**kwargs) -> list:
            """Gets the delivery channels in the account/region -- calls are wrapped with CloudAux"""
            return kwargs.pop("client").get_account_summary(**kwargs)

        if config.region == config.get("celery.active_region"):
            # First, get list of accounts
            accounts_d: list = aws.get_account_ids_to_names()
            num_accounts = len(accounts_d.keys())
            num_roles = 0
            for account_id in accounts_d.keys():
                try:
                    iam_summary = _get_delivery_channels(
                        account_number=account_id,
                        assume_role=config.get("policies.role_name"),
                        region=config.region,
                    )
                    num_iam_roles = iam_summary["SummaryMap"]["Roles"]
                    iam_role_quota = iam_summary["SummaryMap"]["RolesQuota"]
                    iam_role_quota_ratio = num_iam_roles / iam_role_quota

                    num_roles += num_iam_roles
                    log_data = {
                        "function": function,
                        "message": "IAM role quota for account",
                        "num_iam_roles": num_iam_roles,
                        "iam_role_quota": iam_role_quota,
                        "iam_role_quota_ratio": iam_role_quota_ratio,
                        "account_id": account_id,
                    }
                    stats.count(
                        f"{function}.quota_ratio",
                        tags={
                            "num_iam_roles": num_iam_roles,
                            "iam_role_quota": iam_role_quota,
                            "iam_role_quota_ratio": iam_role_quota_ratio,
                            "account_id": account_id,
                        },
                    )
                    log.debug(log_data)
                except ClientError as e:
                    log_data = {
                        "function": function,
                        "message": "Error retrieving IAM quota",
                        "account_id": account_id,
                        "error": e,
                    }
                    stats.count(f"{function}.error", tags={"account_id": account_id})
                    log.error(log_data, exc_info=True)