111 lines
3.5 KiB
Python
111 lines
3.5 KiB
Python
import os
|
|
import shutil
|
|
from typing import OrderedDict
|
|
import pandas as pd
|
|
import yaml
|
|
import requests
|
|
import json
|
|
|
|
#Search and replace in text files
|
|
#from pathlib2 import Path
|
|
|
|
managementZonesFile = open("./euprod/management-zone/management-zone.yaml")
|
|
managementZones = yaml.load(managementZonesFile)
|
|
managementZonesFile.close
|
|
|
|
|
|
|
|
for managementZone in managementZones["config"]:
|
|
|
|
for key, value in managementZone.items():
|
|
|
|
#name of the "configfolder"
|
|
configfolder = key
|
|
#name of the config file
|
|
configfile = value
|
|
#UI name --> first variable {{.name}}
|
|
for vars in managementZones[key]:
|
|
for key, value in vars.items():
|
|
if key == "name":
|
|
configname = value
|
|
##check for keys other than "name"
|
|
|
|
#Testprint for variables
|
|
print("Found " + configname + " in file " + configfile + " in folder "+configfolder)
|
|
|
|
spec_config = open("./euprod/management-zone/" + configfile,"r")
|
|
mgmtZone = json.load(spec_config)
|
|
spec_config.close
|
|
|
|
tagmap = set()
|
|
|
|
for conditions in mgmtZone["rules"]:
|
|
for condition in conditions["conditions"]:
|
|
if condition['comparisonInfo']['type'] == "TAG":
|
|
if isinstance(condition['comparisonInfo']['value'],str):
|
|
tagmap.add(condition['comparisonInfo']['value'])
|
|
if isinstance(condition['comparisonInfo']['value'],dict):
|
|
if condition['comparisonInfo']['operator'] =="TAG_KEY_EQUALS":
|
|
tagmap.add(condition['comparisonInfo']['value']['key'])
|
|
else:
|
|
tagmap.add(condition['comparisonInfo']['value']['value'])
|
|
|
|
|
|
##Write new variables
|
|
spec_config = open("./euprod/management-zone/" + configfile,"r")
|
|
filedata = spec_config.read()
|
|
spec_config.close
|
|
|
|
newfiledata = filedata
|
|
|
|
config={}
|
|
config_detail={}
|
|
data={}
|
|
config[configname] = configfile
|
|
data['config'] = [config]
|
|
data[configname]=[]
|
|
|
|
i=1
|
|
for tag in tagmap:
|
|
config_detail['tag'+str(i)]=[tag]
|
|
#data[configname]=[]
|
|
data[configname].append({'tag'+str(i):tag})
|
|
#data[configname]['tag'+str(i)]=[]
|
|
#[data[configname]['tag'+str(i)]].append(tag)
|
|
newfiledata = newfiledata.replace(tag,"{{.tag"+str(i) + "}}")
|
|
i += 1
|
|
|
|
|
|
|
|
config_detail['name']= configname
|
|
|
|
print(config_detail)
|
|
|
|
#data[configname] = [config_detail]
|
|
#data[configname]['name'] = {}
|
|
#[data[configname]['name']] = [configname]
|
|
data[configname].append({'name':configname})
|
|
|
|
|
|
deticatedMgmtZoneFile = open("./config/" + configfolder + "/management-zone/management-zone.yaml","w")
|
|
newMgmtZone = yaml.dump(data,deticatedMgmtZoneFile, default_flow_style=False, sort_keys=False)
|
|
deticatedMgmtZoneFile.close()
|
|
|
|
outfile = open("./config/" + configfolder + "/management-zone/" + configfile, 'w')
|
|
outfile.write(newfiledata)
|
|
outfile.close()
|
|
|
|
"""
|
|
try:
|
|
# Create target Directory
|
|
os.makedirs("./config/" + configfolder + "/management-zone")
|
|
print("Directory " , configfolder , "/management-zone Created ")
|
|
except FileExistsError:
|
|
print("Directory " , configfolder , "/management-zone already exists")
|
|
"""
|
|
|
|
|
|
##File Operations or read yaml and create a new one from dict?
|
|
#shutil.copyfile("./templates/management-zone.yaml","./config/"+configfolder+"/management-zone/management-zone.yaml")
|
|
|
|
|