示例#1
0
def dd_resource_to_terraform(args, dd_resource_ids):
    """
    """
    # dd_resources = get_dd_resources(args.type)
    # print(dd_resources)
    terraform_code = ""

    if args.input == "ALL":
        count = 0
        dd_resources = get_dd_resources(args.type)
        for dd_resource in dd_resources:
            count += 1
            # if count == 2 or count == 4 or count == 8 or count == 12 or count == 15: continue
            # print(count)
            # print(dd_resource)
            # print("==========================================================================")
            # convert monitor definition dict to terraform code.
            converter = Converter(datadog_type=args.type,
                                  json_dict=dd_resource)

            # Append terraform code to monitor platform file(according to monitor type).
            terraform_code += converter.to_Terraform_Code(
                args.type + '_' + str(count)) + "\n\n\n"
    else:
        if args.type != "dashboard": dd_resources = get_dd_resources(args.type)

        for dd_resource_group, dd_resource_group_ids in dd_resource_ids.items(
        ):
            count = 0
            terraform_code_monitors_group = ""  # output terraform code per dd resource group.

            for dd_resource_group_id in dd_resource_group_ids:
                count += 1

                if args.type != "dashboard":
                    dd_resource_dict = get_dd_resource_def(
                        dd_resource_group_id, dd_resources, args.type)
                else:
                    dd_resource_dict = dd_resource_remove_keys(
                        api.Dashboard.get(dd_resource_group_id), args.type)

                # convert monitor definition dict to terraform code.
                converter = Converter(datadog_type=args.type,
                                      json_dict=dd_resource_dict)

                # Append terraform code to monitor platform file(according to monitor type).
                terraform_code_monitors_group += converter.to_Terraform_Code(
                    dd_resource_group + '_' + str(count)) + "\n\n\n"

            # Write out terraform code for this group of dd resource...
            if args.all:
                with open(dd_resource_group + ".tf", "w") as f:
                    f.write(terraform_code_monitors_group)
                logging.info(
                    f"Terraform code file for group {dd_resource_group} has been created..."
                )

            terraform_code += f"#####\n#\n# {dd_resource_group}\n#\n#####\n" + terraform_code_monitors_group

    return terraform_code
示例#2
0
def main():
    """
    Main function used to convert datadog monitors into a terraform file according to there type.
    """
    #all_monitors_def = api.Monitor.get_all()
    # print(all_monitors_def)
    # for monitor_def in all_monitors_def:
        # print(f"Monitor {type(monitor_def)}=> \n {monitor_def}")
    #return
    #print("hererererer")
    # read monitors ids json file.
    args = get_arguments()          # get input arguments.
    set_logging(args.verbose)
    terraform_code = ""             # output terraform code.
    try:
        with open(args.input, "r") as f:
            monitors_list_dict = json.load(f)

        for monitor, monitor_ids in monitors_list_dict.items():
            count = 0
            terraform_code_monitors_group = ""       # output terraform code per monitors group.
            for monitor_id in monitor_ids:
                count += 1

                # extract monitor definition from datadog.
                monitor_def = get_monitor_by_id(monitor_id)
                # print(monitor_id,type(monitor_def),monitor_def.keys())      
                # monitor_def = api.Monitor.get(monitor_id)

                # reorg monitor dict.
                monitor_definition = redefined_monitor_definition(monitor_def)

                # convert monitor definition dict to terraform code.
                converter = Converter(datadog_type="monitor", json_dict=monitor_definition)

                # Append terraform code to monitor platform file(according to monitor type).
                terraform_code_monitors_group += converter.to_Terraform_Code(monitor + '_' + str(count)) + "\n\n\n"

            # Write out terraform code for this group of monitor..
            if args.all:
                with open(monitor+".tf","w") as f:
                    f.write(terraform_code_monitors_group)
                logging.info(f"Terraform code file for group {monitor} has been created...")

            terraform_code += f"#####\n#\n# {monitor}\n#\n#####\n" + terraform_code_monitors_group

        # Write out terraform code. 
        with open(args.output,args.mode) as f:
            f.write(terraform_code)

        logging.info(f"Terraform code file {args.output} has been created successfully...")

    except BaseException as e:
        logging.exception("Uncaught exception: %s: %s", type(e).__name__, str(e))
def main():
    """
    Main function used to convert a datadog json file into a terraform file.
    """
    # with open("../project8/dashboard.json") as f:
    # with open("../project8/mongodb.json") as f:
    # with open("../project8/mongo.json") as f:
    # with open("../project8/business.json") as f:
    # with open("../project8/uptime.json") as f:
    # with open("../project8/alb_performance.json") as f:
    # with open("../project8/status_errors.json") as f:
    # with open("../project8/status_errors.json") as f:
    # with open("../project8/response_time.json") as f:
    # with open("../project8/aws_elb.json") as f:
    # with open("../project8/jvm_metrics.json") as f:
    # with open("../project8/k8s_pods.json") as f:
    # with open("../project8/aws_ec2.json") as f:
    # with open("../project8/jvm_overview.json") as f:
    # with open("../project8/alb_cloned.json") as f:
    # with open("../project8/k8s_services.json") as f:
    # with open("../project8/aws_ec2_cloned.json") as f:
    # with open("../project8/trace_analytics.json") as f:
    # with open("../project8/system_metrics.json") as f:
    # with open("../project8/aws_mq.json") as f:
    # with open("../project8/aws_autoscaling.json") as f:
    # with open("../project8/aws_billing.json") as f:
    # with open("../project8/aws_s3.json") as f:
    # with open("../project8/azure_api.json") as f:
    # with open("../project8/azure_overview.json") as f:
    # with open("../project8/aws_document.json") as f:
    # with open("../project8/redis.json") as f:
    # with open("../project8/aws_kinesis.json") as f:
    # with open("../project8/aws_kinesis_firehose.json") as f:
    # with open("../project8/aws_lambda.json") as f:
    # with open("../project8/aws_rds.json") as f:
    # with open("../project8/aws_sqs.json") as f:
    # with open("../project8/aws_step_functions.json") as f:
    # with open("../project8/aws_trusted_advisor.json") as f:
    # with open("../project8/azure_app_service.json") as f:
    # with open("../project8/azure_batch.json") as f:
    # with open("../project8/azure_cosmosdb.json") as f:
    # with open("../project8/azur_dbmsql.json") as f:
    # with open("../project8/azure_dbpostgres.json") as f:
    # with open("../project8/azure_event_hub.json") as f:
    # with open("../project8/azure_functions.json") as f:
    # with open("../project8/azure_iot_hub.json") as f:
    # with open("../project8/azure_loadbalancing.json") as f:
    # with open("../project8/azure_logicapp.json") as f:
    # with open("../project8/azure_overview#1.json") as f:
    # with open("../project8/azure_databases.json") as f:
    # with open("../project8/azure_usage.json") as f:
    # with open("../project8/azure_vm.json") as f:
    # with open("../project8/azure_vm_scale.json") as f:
    # with open("../project8/azure_cont.json") as f:
    # with open("../project8/azure_coredns.json") as f:
    # with open("../project8/docker_overview.json") as f:
    # with open("../project8/host_count.json") as f:
    # with open("../project8/k8s_daemonset.json") as f:
    # with open("../project8/k8s_deployment.json") as f:
    # with open("../project8/k8s_replicaset.json") as f:
    # with open("../project8/k8s_overview.json") as f:
    # with open("../project8/run_errors.json") as f:
    # with open("../project8/rum_mobile.json") as f:
    # with open("../project8/system_diskio.json") as f:
    # with open("../project8/system_networking.json") as f:
    # with open("../project8/troubleshoot.json") as f:
    # with open("../project8/load_test.json") as f:
        # content = json.load(f)
    
    args = get_arguments()
    set_logging(args.verbose)
    
    try:
        datadog_type = args.type
        datadog_json = args.json
        terraform_file = args.output
        terraform_resource_name = args.resource

        # if datadog_type in ["dashboard","monitor"]:
        logging.info(f" Converting Json file {datadog_json} of type {datadog_type} ...")
        
        # read datadog json file.
        with open(datadog_json, "r") as f:
            datadog_json_dict = json.load(f)

        # initiliaze converter instance.
        converter = Converter(datadog_type=datadog_type, json_dict=datadog_json_dict)
        # converter = Dashboard(datadog_json_dict) if datadog_type == "dashboard" else Monitor(datadog_json_dict)

        # create terraform code.
        with open(terraform_file,"w") as f:
            f.write(converter.to_Terraform_Code(terraform_resource_name))

        logging.info(f" Terraform file [ {terraform_file} ] has been created")
        logging.info(f" For reformating run => [ terraform fmt {terraform_file} ]")

    except BaseException as e:
        logging.exception("Uncaught exception: %s: %s", type(e).__name__, str(e))
def dd_resources_to_terraform(args, dd_resource_ids):
    """
    Convert Datadog resource to terraform code.

        Args:
        . args:             input Arguments 
        . dd_resource_ids:  Datadog resource ID numbers to convert.

        Output:
        . return terraform code string that represent Datadog resource.
    """

    terraform_code = ""

    if args.input == "ALL":
        """
        In case we need to convert all Datadog resources of specific type.
        """
        count = 0
        dd_resources = fetch_dd_resources(
            args.type)  # Fetch all Datadog resources.
        for dd_resource in dd_resources:
            count += 1
            # convert Datadog resource definition dict to terraform code.
            converter = Converter(datadog_type=args.type,
                                  json_dict=dd_resource)

            logging.debug(f'Datadog resource {count} => {dd_resource}')

            # Append terraform code to.
            terraform_code += converter.to_Terraform_Code(
                args.type + '_' + str(count)) + "\n\n\n"
    else:
        if args.type != "dashboard":
            dd_resources = fetch_dd_resources(args.type)

        for dd_resource_group, dd_resource_group_ids in dd_resource_ids.items(
        ):
            count = 0
            terraform_code_monitors_group = ""  # output terraform code per dd resource group.

            for dd_resource_group_id in dd_resource_group_ids:
                count += 1

                if args.type != "dashboard":
                    dd_resource_dict = fetch_dd_resource_def(
                        dd_resource_group_id, dd_resources, args.type)
                else:
                    # dd_resource_dict = dd_resource_remove_keys(api.Dashboard.get(dd_resource_group_id), args.type)
                    dd_resource_dict = fetch_dd_resource(
                        dd_resource_group_id, args.type)

                # convert Datadog resource definition dict to terraform code.
                converter = Converter(datadog_type=args.type,
                                      json_dict=dd_resource_dict)

                logging.debug(
                    f'Datadog resource {count} => {dd_resource_dict}')

                # Append terraform code.
                terraform_code_monitors_group += converter.to_Terraform_Code(
                    dd_resource_group + '_' + str(count)) + "\n\n\n"

            # Write out terraform code for this group of dd resource...
            if args.group:
                with open(dd_resource_group + ".tf", "w") as f:
                    f.write(terraform_code_monitors_group)
                logging.info(
                    f"Terraform code file for group {dd_resource_group} has been created..."
                )

            terraform_code += f"#####\n#\n# {dd_resource_group}\n#\n#####\n" + terraform_code_monitors_group

    return terraform_code