コード例 #1
0
def test_threshold_percent_wrong_type(project_dict):
    project_dict['threshold_rules'][0]['threshold_percent'] = 'foobar'
    assert(verify_project_yaml(project_dict) is False)
コード例 #2
0
def test_threshold_rules_invalid_list_object(project_dict):
    del project_dict['threshold_rules'][0]['spend_basis']
    assert(verify_project_yaml(project_dict) is False)
コード例 #3
0
def test_alert_emails_wrong_type(project_dict):
    project_dict['alert_emails'] = '*****@*****.**'
    assert(verify_project_yaml(project_dict) is False)
コード例 #4
0
def test_threshold_rules_invalid_spend_basis(project_dict):
    project_dict['threshold_rules'][0]['spend_basis'] = 'foobar'
    assert(verify_project_yaml(project_dict) is False)
コード例 #5
0
def main(gcs_bucket, gcs_file_path, billing_account_id, default_pubsub_topic,
         mysql_host, mysql_port, mysql_user, mysql_pass, mysql_db, statsd_host,
         local_mode, dry_run):
    logformat = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
    logging.basicConfig(stream=sys.stdout,
                        level=logging.INFO,
                        format=logformat)

    # TODO - troubleshoot metric uploading with local container, and test it works on GKE as well.
    # Remove this line to see WARNINGs
    logging.getLogger('datadog.dogstatsd').setLevel(logging.ERROR)

    # Load config.yaml file
    if local_mode:
        with open("config.yaml", "r") as f:
            budget_dict = yaml.load(f, Loader=yaml.SafeLoader)

        setup_metrics("localhost")
    else:
        # Load yaml configuration file from GCS
        gcs_client = storage.Client()
        bucket = gcs_client.bucket(gcs_bucket)
        blob = bucket.blob(gcs_file_path)
        yaml_string = blob.download_as_string()
        budget_dict = yaml.load(yaml_string, Loader=yaml.SafeLoader)
        # Setup metrics with configured statsd host
        setup_metrics(statsd_host)

    # Setup gcp helper
    billing_client = billing_budgets_v1beta1.BudgetServiceClient()
    resource_manager_client = resource_manager.Client()
    gcp = GcpHelper(billing_client, resource_manager_client)

    # Setup mysql connection pool
    pool = PooledDB(creator=pymysql,
                    host=mysql_host,
                    user=mysql_user,
                    password=mysql_pass,
                    database=mysql_db,
                    autocommit=True,
                    blocking=True,
                    maxconnections=5)
    mysql_conn = pool.connection()

    # Iterate over all configured project budgets
    for project_dict in budget_dict['projects']:
        config_type = PLUTUS_CONFIG_TYPE_PROJECT

        if verify_project_yaml(project_dict):
            project = ProjectBudget(project_dict, config_type,
                                    billing_account_id, default_pubsub_topic)
            budget = gcp.get_and_update_or_create_budget(project)

            if budget is not None:
                with mysql_conn.cursor() as mysql_cursor:
                    upsert_budget(mysql_cursor, budget, project.project_id,
                                  config_type, project.alert_emails)

        else:
            log.error("Project config verification failed.")
            metrics.incr("error_count",
                         tags=[
                             "type:misconfig",
                             f"project_id:{project.project_id}",
                             f"config_type:{config_type}"
                         ])
            sys.exit(1)

    # Iterate over all configured parent folder id budgets
    for parent_dict in budget_dict['parent_folders']:
        parent_id = parent_dict['parent_folder_id']
        config_type = PLUTUS_CONFIG_TYPE_PARENT

        if verify_parent_yaml(parent_dict):
            parent_filter = {'parent.id': parent_id}

            for p in resource_manager_client.list_projects(parent_filter):
                # Overwrite 'project_id' key for each project under parent folder
                parent_dict['project_id'] = p.project_id
                project = ProjectBudget(parent_dict, config_type,
                                        billing_account_id,
                                        default_pubsub_topic)

                existing_project_budget_id = gcp.has_existing_project_budget(
                    project)

                if existing_project_budget_id is None:
                    # No project one off budget configured for this projectid
                    budget = gcp.get_and_update_or_create_budget(project)

                    if budget is not None:
                        with mysql_conn.cursor() as mysql_cursor:
                            upsert_budget(mysql_cursor, budget,
                                          project.project_id, config_type,
                                          project.alert_emails)
                else:
                    log.info(f"Skipping creating parent project budget for \
                              {p.project_id} since exiting plutus project budget found."
                             )

                    existing_parent_budget_id = gcp.has_existing_parent_budget(
                        parent_id, project)
                    if existing_parent_budget_id is not None:
                        # We have a configured plutus project budget. Delete parent budget
                        gcp.delete_budget(existing_parent_budget_id)

        else:
            log.error("Parent folder config verification failed.")
            metrics.incr("error_count",
                         tags=[
                             "type:misconfig", f"parent_id:{parent_id}",
                             f"config_type:{config_type}"
                         ])
            sys.exit(1)

    # Iterate over all configured label budgets
    for label_dict in budget_dict['labels']:
        config_type = PLUTUS_CONFIG_TYPE_LABEL

        if verify_labels_yaml(label_dict):

            labels_filter = {}
            for row in label_dict['label_list']:
                for key in row:
                    labels_filter[f"labels.{key}"] = row[key]

            # Find projects that match the labels
            for p in resource_manager_client.list_projects(
                    filter_params=labels_filter):
                # Overwrite the 'project_id' key for each project that matches labels
                label_dict['project_id'] = p.project_id
                project = ProjectBudget(label_dict, config_type,
                                        billing_account_id,
                                        default_pubsub_topic)

                existing_project_budget_id = gcp.has_existing_project_budget(
                    project)

                if existing_project_budget_id is None:
                    # No project one off budget configured for this projectid
                    budget = gcp.get_and_update_or_create_budget(project)

                    if budget is not None:
                        with mysql_conn.cursor() as mysql_cursor:
                            upsert_budget(mysql_cursor, budget,
                                          project.project_id, config_type,
                                          project.alert_emails)
                else:
                    log.info(f"Skipping creating label project budget for \
                              {p.project_id} since exiting plutus project budget found."
                             )

                    existing_labels_budget_id = gcp.has_existing_labels_budget(
                        project)
                    if existing_labels_budget_id is not None:
                        # We have a configured plutus project budget. Delete labels budget
                        gcp.delete_budget(existing_labels_budget_id)

        else:
            log.error("Labels config verification failed.")
            metrics.incr("error_count",
                         tags=["type:misconfig", f"config_type:{config_type}"])
            sys.exit(1)

    # Default logic removed for now because it will create hundreds of budgets
    # And we need to decide good thresholds for this
    '''
    default_dict = budget_dict['default']
    config_type = PLUTUS_CONFIG_TYPE_DEFAULT

    if verify_default_yaml(default_dict):
        # Query for all projects. If no budget exists for project, add a default budget
        for p in resource_manager_client.list_projects():
            project_id = p.project_id
            project_number = gcp.get_project_number(project_id)
            if project_number is not None:
                budgets = gcp.get_budgets_by_project(project, project_number)
                if len(budgets) == 0:
                    # No budget exists for this project, so we create one
                    default_dict['project_id'] = project_id

                    # TODO - add and test
                    # project = ProjectBudget(default_dict, config_type,
                    #                         billing_account_id, default_pubsub_topic)
                    log.info(f"Creating default budget for plutus-default-{project_id}")
                    # budget = gcp.create_budget(project)
                    # metrics.incr("default_budget_created_count", tags=[])

                    #if budget is not None:
                    #    with mysql_conn.cursor() as mysql_cursor:
                    #        upsert_budget(mysql_cursor, budget, project.project_id,
                    #                      config_type, project.alert_emails)

                # TODO - delete plutus-default budget if > 1 budget
                elif len(budgets) > 1:
                    # TODO - if one of the budgets is a plutus-default, delete via API and in Mysql
                    pass

    else:
        log.error("Default config verification failed.")
        metrics.incr("error_count", tags=["type:misconfig", f"config_type:{config_type}"])
        sys.exit(1)
    '''

    log.info("Plutus run complete.")
コード例 #6
0
def test_pubsub_wrong_type(project_dict):
    project_dict['pubsub'] = 'foobar'
    assert(verify_project_yaml(project_dict) is False)
コード例 #7
0
def test_user_configured_display_name(project_dict):
    project_dict['display_name'] = 'user specified display name'
    assert(verify_project_yaml(project_dict) is False)
コード例 #8
0
def test_no_project_id(project_dict):
    del project_dict['project_id']
    assert(verify_project_yaml(project_dict) is False)
コード例 #9
0
def test_missing_include_credits(project_dict):
    del project_dict['include_credits']
    assert(verify_project_yaml(project_dict) is False)
コード例 #10
0
def test_missing_pubsub(project_dict):
    del project_dict['pubsub']
    assert(verify_project_yaml(project_dict) is False)
コード例 #11
0
def test_missing_threshold_rules(project_dict):
    del project_dict['threshold_rules']
    assert(verify_project_yaml(project_dict) is False)
コード例 #12
0
def test_missing_budget_amount(project_dict):
    del project_dict['budget_amount']
    assert(verify_project_yaml(project_dict) is False)
コード例 #13
0
def test_wrong_project_id_type(project_dict):
    project_dict['project_id'] = 1234
    assert(verify_project_yaml(project_dict) is False)
コード例 #14
0
def test_pubsub_topic_wrong_type(project_dict):
    project_dict['pubsub_topic'] = 1234
    assert(verify_project_yaml(project_dict) is False)
コード例 #15
0
def test_budget_type_wrong_type(project_dict):
    project_dict['budget_type'] = 1234
    assert(verify_project_yaml(project_dict) is False)
コード例 #16
0
def test_yes_project_id(project_dict):
    assert(verify_project_yaml(project_dict) is True)
コード例 #17
0
def test_budget_amount_wrong_type(project_dict):
    project_dict['budget_amount'] = 'foobar'
    assert(verify_project_yaml(project_dict) is False)
コード例 #18
0
import sys
import yaml

from plutus.budget_manager.verify import (verify_project_yaml,
                                          verify_parent_yaml,
                                          verify_labels_yaml
                                          # verify_default_yaml
                                          )

# test config file for errors as a requirement for merging
with open(sys.argv[1], "r") as f:
    budget_dict = yaml.load(f, Loader=yaml.SafeLoader)

    for project_dict in budget_dict['projects']:
        if verify_project_yaml(project_dict):
            pass
        else:
            print(f"Error validating {project_dict}.")
            sys.exit(1)

    for parent_dict in budget_dict['parent_folders']:
        if verify_parent_yaml(parent_dict):
            pass
        else:
            print(f"Error validating {parent_dict}.")
            sys.exit(1)

    for label_dict in budget_dict['labels']:
        if verify_labels_yaml(label_dict):
            pass
        else:
コード例 #19
0
def test_verify_project_yaml(config_dict):
    assert(verify_project_yaml(config_dict['good']) is True)
    assert(verify_project_yaml(config_dict['missing_project_id']) is False)
    assert(verify_project_yaml(config_dict['project_id_wrong_type']) is False)
    assert(verify_project_yaml(config_dict['missing_budget_type']) is False)
    assert(verify_project_yaml(config_dict['missing_budget_amount']) is False)
    assert(verify_project_yaml(config_dict['missing_threshold_rules']) is False)
    assert(verify_project_yaml(config_dict['missing_include_credits']) is False)
    assert(verify_project_yaml(config_dict['missing_pubsub']) is False)
    assert(verify_project_yaml(config_dict['user_configured_display_name']) is False)
    assert(verify_project_yaml(config_dict['budget_type_wrong_type']) is False)
    assert(verify_project_yaml(config_dict['budget_amount_wrong_type']) is False)
    assert(verify_project_yaml(config_dict['pubsub_wrong_type']) is False)
    assert(verify_project_yaml(config_dict['include_credits_wrong_type']) is False)
    assert(verify_project_yaml(config_dict['budget_type_wrong_val']) is False)
    assert(verify_project_yaml(config_dict['products_wrong_type']) is False)
    assert(verify_project_yaml(config_dict['alert_emails_wrong_type']) is False)
    assert(verify_project_yaml(config_dict['threshold_rules_wrong_type']) is False)
    assert(verify_project_yaml(config_dict['threshold_rules_invalid_spend_basis']) is False)
    assert(verify_project_yaml(config_dict['threshold_percent_wrong_type']) is False)
    assert(verify_project_yaml(config_dict['threshold_rules_invalid_list_object']) is False)
    assert(verify_project_yaml(config_dict['pubsub_topic_wrong_type']) is False)