commit 7595beeb846254b6db0e72f3cb3229c600ade3d7 Author: Arnel Arnautovic Date: Tue Sep 19 15:22:07 2023 +0200 first commit diff --git a/main.py b/main.py new file mode 100644 index 0000000..84d5bb6 --- /dev/null +++ b/main.py @@ -0,0 +1,76 @@ +#!/usr/bin/env python +""" +Input: This script takes a csv file containing the campaign name as input as following: + +attributes.csv + +name +----------------- +NIRA_Denmark_2023 +NIRA_France_2023 +. +. +. +----------------- + +Output: This script generates a terraform file for request naming rules. The terraform file is based on a template. + +The input .csv file shall be stored within the default input directory. + +Usage: main.py +""" +import configparser +import csv +import jinja2 +import logging +import pathlib +import re + + +FORMAT = '%(asctime)s %(message)s' +logging.basicConfig(format=FORMAT) +logger = logging.getLogger('main') +logger.setLevel(logging.INFO) + +config = configparser.ConfigParser() +config.read("config.ini") + +DEFAULT_INPUT_PATH = pathlib.Path(config['PATHS']['Input']).absolute() +DEFAULT_OUTPUT_PATH = pathlib.Path(config['PATHS']['Output']).absolute() +DEFAULT_TEMPLATES_PATH = pathlib.Path(config['PATHS']['Templates']).absolute() + +env = jinja2.Environment(loader=jinja2.FileSystemLoader(DEFAULT_TEMPLATES_PATH)) +template = env.get_template("template.request_attribute.tf") + +def checkDirectories(DEFAULT_INPUT_PATH, DEFAULT_OUTPUT_PATH, DEFAULT_TEMPLATES_PATH): + for dir in [DEFAULT_INPUT_PATH, DEFAULT_OUTPUT_PATH, DEFAULT_TEMPLATES_PATH]: + + try: + pathlib.Path.mkdir(dir) + logger.info("Directory created: %s", dir) + except FileExistsError: + logger.info("Directory already exists: %s", dir) + +def renderFile(file, DEFAULT_OUTPUT_PATH): + logger.info("Generating files for: %s", str(pathlib.PurePath(file).name)) + with open(file, newline='') as csvfile: + data = csv.DictReader(csvfile, delimiter=';') + + for row in data: + content = template.render(CampaignName = row["name"], + RequestName = re.search("", str(row["name"]))) #ToDo + filename = pathlib.PurePath.joinpath(DEFAULT_OUTPUT_PATH, str(row["name"]) + ".request_attribute.tf") + + with open(filename, mode='w+', encoding="utf-8") as output: + output.write(content) + logger.info("Generated: %s", pathlib.PurePath(filename).name) + + +if __name__ == '__main__': + checkDirectories(DEFAULT_INPUT_PATH, DEFAULT_OUTPUT_PATH, DEFAULT_TEMPLATES_PATH) + logger.info("Using default input directory: %s", DEFAULT_INPUT_PATH) + files = sorted(pathlib.Path(DEFAULT_INPUT_PATH).glob("*.csv")) + + for f in files: + logger.info("File found: %s", str(f)) + renderFile(f, DEFAULT_OUTPUT_PATH) \ No newline at end of file diff --git a/templates/template.request_naming.tf b/templates/template.request_naming.tf new file mode 100644 index 0000000..c4f69b4 --- /dev/null +++ b/templates/template.request_naming.tf @@ -0,0 +1,18 @@ +resource "dynatrace_request_naming" "{{CampaignName}}" { + enabled = true + naming_pattern = "{{RequestName}}" + conditions { + condition { + attribute = "SERVICE_REQUEST_ATTRIBUTE" + comparison { + # negate = false + string_request_attribute { + # case_sensitive = false + # match_on_child_calls = false + operator = "EXISTS" + request_attribute = "{{CampaignName}}" + } + } + } + } +}