|
@@ -0,0 +1,71 @@
|
|
|
+import logging
|
|
|
+import sys
|
|
|
+import os
|
|
|
+import csv
|
|
|
+try:
|
|
|
+ import hcl2
|
|
|
+except ModuleNotFoundError:
|
|
|
+ print("Module 'hcl2' is not installed. Try 'pip3 install python-hcl2'")
|
|
|
+
|
|
|
+'''
|
|
|
+#read in folders
|
|
|
+# loop through folders
|
|
|
+#find .hcl files
|
|
|
+#pull out ports
|
|
|
+# display to user and write to csv file
|
|
|
+'''
|
|
|
+
|
|
|
+def get_current_dir():
|
|
|
+ current_dir = os.path.dirname(os.path.realpath(__file__))
|
|
|
+ relative_path = os.path.dirname(current_dir)
|
|
|
+ correct_path = os.path.join(relative_path, "base/")
|
|
|
+
|
|
|
+ return correct_path
|
|
|
+
|
|
|
+def get_files(base_dir):
|
|
|
+ print("Looking in " + base_dir +" for TF hcl files")
|
|
|
+ r = []
|
|
|
+ for root, dirs, files in os.walk(base_dir):
|
|
|
+ for name in files:
|
|
|
+ filepath = root + os.sep + name
|
|
|
+ if filepath.endswith(".tf") and not filepath.endswith("vars.tf") and not filepath.endswith("amis.tf") and not filepath.endswith("outputs.tf"):
|
|
|
+ r.append(os.path.join(root, name))
|
|
|
+ return r
|
|
|
+
|
|
|
+def parse_tf_files(files):
|
|
|
+ my_resources = []
|
|
|
+ for file in files:
|
|
|
+ #print(file)
|
|
|
+ with open(file, 'r') as open_file:
|
|
|
+ try:
|
|
|
+ dict = hcl2.load(open_file)
|
|
|
+ except:
|
|
|
+ pass
|
|
|
+
|
|
|
+ for resources in dict.get("resource", []):
|
|
|
+ for resource in resources.keys():
|
|
|
+ for resource_name in resources[resource].keys():
|
|
|
+ for item_name in resources[resource][resource_name].keys():
|
|
|
+ if item_name == "from_port" or item_name == "to_port":
|
|
|
+ if resources[resource][resource_name]["from_port"] == resources[resource][resource_name]["to_port"]:
|
|
|
+ my_resources.append(file + "," + resource_name + "," + item_name + "," + str(resources[resource][resource_name][item_name][0]) + "," + resources[resource][resource_name]["protocol"][0])
|
|
|
+ break
|
|
|
+ else:
|
|
|
+ my_resources.append(file + "," + resource_name + "," + item_name + "," + str(resources[resource][resource_name][item_name][0]) + "," + resources[resource][resource_name]["protocol"][0])
|
|
|
+ print("Found "+ str(len(my_resources)) + " port references.")
|
|
|
+ return my_resources
|
|
|
+
|
|
|
+def dedup_generate_csv(my_resources):
|
|
|
+ #dedup_resources =
|
|
|
+ with open('xdr_port_references.csv', mode='w') as xdr_port_references:
|
|
|
+ writer = csv.writer(xdr_port_references)
|
|
|
+ for item in my_resources:
|
|
|
+ writer.writerow(item)
|
|
|
+ print(item)
|
|
|
+
|
|
|
+if __name__ == "__main__":
|
|
|
+ correct_path = get_current_dir()
|
|
|
+ files = get_files(correct_path)
|
|
|
+ my_resources = parse_tf_files(files)
|
|
|
+ dedup_generate_csv(my_resources)
|
|
|
+ sys.exit(0)
|