160 lines
6.1 KiB
Python
160 lines
6.1 KiB
Python
import glob
|
|
import sys
|
|
import itertools
|
|
import jinja2
|
|
import re
|
|
import yaml
|
|
import os
|
|
from nested_lookup import nested_lookup
|
|
from pathlib import Path
|
|
|
|
# defines
|
|
OUTPUT_FOLDER_BASENAME = os.path.basename("output")
|
|
TEMPLATE_FOLDER_BASENAME = os.path.basename("jinja-templates")
|
|
TEMPLATE_FILES = {
|
|
"terraform-template.j2": ["builtin:service.keyRequest.errors.fivexx.rate"],
|
|
"terraform-template-2.j2":["builtin:service.keyRequest.errors.server.successCount"],
|
|
"terraform-template-3.j2":["builtin:service.keyRequest.errors.fivexx.count"],
|
|
"terraform-template-4.j2":["builtin:service.successes.server.rate"]
|
|
}
|
|
FOLDERS = {"My Journey":"DE-3",
|
|
"Connected Vehicle Platforms":"DE-7",
|
|
"My Life":"DE-4",
|
|
"China Services":"EC-DE"}
|
|
|
|
# methods
|
|
def homogenize_metrics(metricList):
|
|
for metric in metricList:
|
|
if len(metricList) < 4:
|
|
metricList.append('')
|
|
return metricList
|
|
|
|
def homogenize_services(serviceList):
|
|
for service in serviceList:
|
|
if len(serviceList) < 2:
|
|
serviceList.append('')
|
|
return serviceList
|
|
|
|
def homogenize_keyRequests(keyRequestLists):
|
|
for keyRequestList in keyRequestLists:
|
|
if len(keyRequestLists) < 2:
|
|
keyRequestLists.append('')
|
|
return keyRequestLists
|
|
|
|
def template_logic(metricList):
|
|
for template,metricTemplate in TEMPLATE_FILES.items():
|
|
if all([item in metricTemplate for item in metricList]):
|
|
# if set(metricList) == set(metricTemplate):
|
|
return template
|
|
print("Problem occured while matching metric: Exiting program...")
|
|
sys.exit(0)
|
|
|
|
# main
|
|
def main(configFile):
|
|
slo_id = None
|
|
slo_name = None
|
|
module_name = None
|
|
displayname = None
|
|
department = None
|
|
description = None
|
|
doc_url = None
|
|
slo_definition_tresholds_warning = None
|
|
slo_definition_tresholds_failure = None
|
|
environments = {}
|
|
template = None
|
|
metricList = []
|
|
serviceList = []
|
|
keyRequestList = []
|
|
|
|
try:
|
|
# read yaml file
|
|
with open(configFile) as cF:
|
|
slos = list(yaml.safe_load_all(cF))
|
|
except EnvironmentError:
|
|
print('Corrupt yaml file:' + str(configFile))
|
|
|
|
# fill values
|
|
for slo in slos:
|
|
slo_id = slo["slo_id"]
|
|
slo_name = slo["slo_name"]
|
|
module_name = slo_name
|
|
displayname = slo["displayname"]
|
|
description = slo["description"]
|
|
department = slo["department"]
|
|
doc_url = slo["doc_url"]
|
|
slo_definition_tresholds_warning = slo["slo_definition"]["tresholds"]["warning"]
|
|
slo_definition_tresholds_failure = slo["slo_definition"]["tresholds"]["failure"]
|
|
slo["slo_definition"].pop("tresholds")
|
|
|
|
for env in slo["slo_definition"]:
|
|
if "EMEA-Prod" in slo["slo_definition"]:
|
|
environments["EMEA-Prod"] = slo["slo_definition"][env]
|
|
if "NA-Prod" in slo["slo_definition"]:
|
|
environments["NA-Prod"] = slo["slo_definition"][env]
|
|
if "CN-Prod" in slo["slo_definition"]:
|
|
environments["CN-Prod"] = slo["slo_definition"][env]
|
|
|
|
# read template file
|
|
jinja_environment = jinja2.Environment(loader=jinja2.FileSystemLoader(TEMPLATE_FOLDER_BASENAME),
|
|
trim_blocks=True,
|
|
lstrip_blocks=True)
|
|
|
|
# generate terraform file
|
|
for i,(environment,ev) in enumerate(environments.items()):
|
|
folder_path = os.path.join(cwd,OUTPUT_FOLDER_BASENAME,environment.replace('-','_'),"slo")
|
|
if not os.path.exists(folder_path):
|
|
os.makedirs(folder_path)
|
|
template = template_logic(nested_lookup('metric',ev))
|
|
metricList = homogenize_metrics(nested_lookup('metric',ev))
|
|
|
|
for service in ev[i]["filter"]:
|
|
serviceList.append(nested_lookup('service',service))
|
|
keyRequestList.append(nested_lookup('keyRequests',service))
|
|
i = 0
|
|
|
|
with open(os.path.join(folder_path,module_name+".tf"),"w+") as file:
|
|
jinja_template = jinja_environment.get_template(template)
|
|
file.write(jinja_template.render(module=module_name,
|
|
slo_name=slo_name,
|
|
metric=metricList[0],
|
|
metricA=metricList[0],metricB=metricList[1],metricC=metricList[2],metricD=metricList[3],
|
|
description=description,
|
|
services='~",\n\t\t\t\t~"'.join(serviceList[0]),
|
|
servicesA='~",\n\t\t\t\t~"'.join(serviceList[0]),servicesB='~",\n\t\t\t\t~"'.join(serviceList[1]),
|
|
keyRequests='~",\n\t\t\t\t~"'.join(itertools.chain.from_iterable(keyRequestList[0])),
|
|
keyRequestsA='~",\n\t\t\t\t~"'.join(itertools.chain.from_iterable(keyRequestList[0])),
|
|
keyRequestsB='~",\n\t\t\t\t~"'.join(itertools.chain.from_iterable(keyRequestList[1])),
|
|
target=slo_definition_tresholds_failure,
|
|
warning=slo_definition_tresholds_warning))
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
# pre-initialization get current working directory
|
|
cwd = os.getcwd()
|
|
|
|
print(sys.argv)
|
|
if len(sys.argv) != 2:
|
|
print(".\convert.py <FOLDER>")
|
|
print("Example: .\convert.py DE-3")
|
|
print("Example: .\convert.py DE-3,DE-4,...")
|
|
sys.exit()
|
|
|
|
# init folder
|
|
for folderName,businessLine in FOLDERS.items():
|
|
businessLinePath = os.path.join(cwd,os.path.basename(businessLine))
|
|
if not os.path.exists(businessLinePath):
|
|
os.makedirs(businessLinePath)
|
|
|
|
# for each folder
|
|
for arg in [sys.argv[1]]:
|
|
# return path of slo config files
|
|
configFiles = glob.glob(pathname=os.path.join(cwd,os.path.basename(arg))+'\\*.yaml',
|
|
# root_dir=os.path.join(cwd,os.path.basename(arg)),
|
|
recursive=True)
|
|
|
|
# for each slo config file in folder
|
|
for configFile in configFiles:
|
|
main(configFile)
|