diff --git a/.gitignore b/.gitignore index ec8793c..4349215 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ bin/ output/ outboard/ +export/ .env .terraform.lock.hcl \ No newline at end of file diff --git a/.terraform/modules/modules.json b/.terraform/modules/modules.json new file mode 100644 index 0000000..2fb9ec9 --- /dev/null +++ b/.terraform/modules/modules.json @@ -0,0 +1 @@ +{"Modules":[{"Key":"m2","Source":"./output/20220117-132316_EMEA_PROD/credentials","Dir":""},{"Key":"","Source":"","Dir":"."},{"Key":"dynatrace_custom_service","Source":"./output/20220117-132316_EMEA_PROD/custom_services","Dir":""},{"Key":"dynatrace_service_naming","Source":"./output/20220117-132316_EMEA_PROD/naming/services","Dir":""},{"Key":"m1","Source":"./output/20220117-132316_EMEA_PROD/anomalies","Dir":""}]} \ No newline at end of file diff --git a/adapter.py b/adapter.py new file mode 100644 index 0000000..914365c --- /dev/null +++ b/adapter.py @@ -0,0 +1,86 @@ +from operator import rshift +import os +import shutil +from glob import glob +import hcl + + +# [AA 2022.12.20] Copy file +def copyFile(src, dst): + try: + shutil.copyfile(src, dst) + except: + print("[DEBUG]", "Copy error for %s ." % dst) + +# [AA 2021.12.20] Creating directory method +def createDir(path): + try: + os.makedirs(path) + except OSError: + print("[DEBUG]", "Directory %s already exists." % path) + else: + print("[DEBUG]", "Successfully created the directory %s." % path) + +# [AA 2021.12.20] Fill dictionary +def readFile(path): + with open(path, 'r', encoding='utf8') as cfg: + + # [AA 2021.12.01] Load the content of the particular resource file in eg: management_zones + obj = hcl.load(cfg) + + # [AA, EW 2021.12.01] Store resource type and resource name of that file into a dictionary + key = list(obj['resource'].keys())[0] + val = list(obj['resource'][key].keys())[0] + return key, val + + +# [AA, EW 2022.01.17] Load all resources and add them to a dictionary +def createFolderStructure(): + resourcePaths = {} + management_zones = [os.path.normpath(f).replace('\\', '/') for f in glob(sourceFolder + "**/management_zones/**.tf", recursive=True)] + for index, file in enumerate(management_zones): + environment = ("./"+os.path.dirname(os.path.dirname(file))).replace(sourceFolder, "") + splittedFilename = os.path.basename(file).split(".") + + # [AA 2022.01.20] Filter out module.tf and main.tf + if len(splittedFilename) == 5: + resourceID = splittedFilename[1] + moduleName, resourceName = readFile(file) + + # [AA 2022.01.20] Create direcotires and copy associated files + resourcePath = os.path.join(outputFolder + environment + "/", resourceName) + if not resourcePath in resourcePaths.keys(): + resourcePaths.setdefault("resourcePaths", []) + resourcePaths["resourcePaths"].append(resourcePath) + + createDir(resourcePath) + copyFile(file, os.path.join(resourcePath, os.path.basename(file))) + return resourcePaths + +def filterCondition(filename): + + return '/dashboards/' in filename or '/alerting_profiles/' in filename; + #'/dashboards/' in filename or + +def fillOtherResources(resourcePaths): + otherResources = [os.path.normpath(f).replace('\\', '/') for f in glob(sourceFolder + "**/**.tf", recursive=True)] + otherResources_iterator= filter(filterCondition, otherResources) + otherResource_filterd=list(otherResources_iterator) + for resourcePath in resourcePaths["resourcePaths"]: + for element in otherResource_filterd: + elementName = os.path.basename(element).split(".")[0].split(" ")[0] + resourceName = os.path.basename(resourcePath) + if elementName == resourceName: + copyFile("./" + element, resourcePath + "/" + os.path.basename(element)) + print("[DEBUG]", "File %s copied to %s." % (os.path.basename(element), resourcePath)) + + +# [AA, EW 2022.01.20] Set global variables +global cwd, targetFolder, templatesFolder, outputFolder, sourceFolder +cwd = os.getcwd() +sourceFolder = "./export/" +outputFolder = "./output/" + +if __name__ == "__main__": + # [AA, EW 2022.01.20] + fillOtherResources(createFolderStructure()) \ No newline at end of file diff --git a/main.tf b/main.tf deleted file mode 100644 index c9a352b..0000000 --- a/main.tf +++ /dev/null @@ -1,18 +0,0 @@ -terraform { - required_providers { - dynatrace = { - version = "1.9.1" - source = "dynatrace-oss/dynatrace" - } - } -} - -# module "configDir" { -# source = "git::https://github.com/arnauagithub/DynatraceTerraformConfiguration.git?ref=20211130-151123" -# } - - -module "m1" { source = "./output/20220117-132316_EMEA_PROD/anomalies" } -module "m2" { source = "./output/20220117-132316_EMEA_PROD/credentials" } -module "dynatrace_custom_service" { source = "./output/20220117-132316_EMEA_PROD/custom_services" } -module "dynatrace_service_naming" { source = "./output/20220117-132316_EMEA_PROD/naming/services" } \ No newline at end of file diff --git a/res/installTerraformPlugin.py b/res/installTerraformPlugin.py deleted file mode 100644 index 3846a72..0000000 --- a/res/installTerraformPlugin.py +++ /dev/null @@ -1,12 +0,0 @@ -import subprocess -import sys - -if(len(sys.argv) == 1): - try: - process = subprocess.Popen(["terraform", "init"]) - outs = process.wait(timeout=10*60) - print("[DEBUG]", "Process return code:", outs) - except subprocess.TimeoutExpired: - print("[DEBUG]", "Exception occured:", subprocess.TimeoutExpired) - print("[DEBUG]", "Killing process.") - process.kill() diff --git a/res/plugin.tf b/res/plugin.tf deleted file mode 100644 index 17cac3b..0000000 --- a/res/plugin.tf +++ /dev/null @@ -1,9 +0,0 @@ -terraform { - required_providers { - dynatrace = { - version = "1.9.1" - source = "dynatrace-oss/dynatrace" - } - } -} - diff --git a/san.py b/san.py deleted file mode 100644 index 3c6f1be..0000000 --- a/san.py +++ /dev/null @@ -1,3 +0,0 @@ -import subprocess -process = subprocess.Popen([".\\bin\\terraform-provider-dynatrace_v1.9.1.exe", "export", "dynatrace_alerting_profile"]) -print(process.returncode) diff --git a/templates/main.tf b/templates/main.tf deleted file mode 100644 index da9b57c..0000000 --- a/templates/main.tf +++ /dev/null @@ -1,12 +0,0 @@ -terraform { - required_providers { - dynatrace = { - version = "1.9.1" - source = "dynatrace-oss/dynatrace" - } - } -} - -# module "configDir" { -# source = "git::https://github.com/arnauagithub/DynatraceTerraformConfiguration.git?ref=20211130-151123" -# } diff --git a/templates/module.tf b/templates/module.tf deleted file mode 100644 index 17cac3b..0000000 --- a/templates/module.tf +++ /dev/null @@ -1,9 +0,0 @@ -terraform { - required_providers { - dynatrace = { - version = "1.9.1" - source = "dynatrace-oss/dynatrace" - } - } -} -