87 lines
3.6 KiB
Python
87 lines
3.6 KiB
Python
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
|
|
resourceName = os.path.basename(file).split(".")[0].split(" ")[0]
|
|
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 management_zones
|
|
|
|
def filterCondition(filename):
|
|
return '/dashboards/' in filename or '/alerting_profiles/' in filename;
|
|
#'/dashboards/' in filename or
|
|
|
|
def fillOtherResources(management_zones):
|
|
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 management_zone in management_zones:
|
|
for element in otherResource_filterd:
|
|
elementName = os.path.basename(element).split(".")[0].split(" ")[0]
|
|
management_zone_folder_name = os.path.basename(management_zone).split(".")[0].split(" ")[0]
|
|
folder = os.path.dirname(os.path.dirname(management_zone)).replace("export","")
|
|
if elementName == management_zone_folder_name:
|
|
copyFile("./" + element, "./output" + folder + "/" + management_zone_folder_name + "/" + os.path.basename(element))
|
|
print("[DEBUG]", "File %s copied to %s." % (os.path.basename(element), "./output" + folder + "/" + os.path.basename(element)))
|
|
|
|
|
|
# [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()) |