-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·115 lines (98 loc) · 4.26 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import subprocess
import json
def parse_state_list(command):
try:
output = subprocess.check_output(command, shell=True)
state_list = output
return output
#return list(filter(None,state_list))
except subprocess.CalledProcessError as e:
print("Error executing command: " + e.output)
return []
state_list = parse_state_list("terraform show -json")
state = json.loads(state_list)
raw_resources = state["values"]["root_module"]["resources"]
#print(state["values"]["root_module"]["child_modules"][1])
if "child_modules" in state["values"]["root_module"]:
for res in state["values"]["root_module"]["child_modules"]:
#print(res["child_modules"])
raw_resources.extend(res["resources"])
if "child_modules" in res:
for b in res["child_modules"]:
if "resources" in b:
print("ok")
raw_resources.extend(b["resources"])
properties = {
"azurerm_resource_group": ["name","location"],
"azurerm_virtual_network": ["name","address_space","dns_servers"],
"azurerm_subnet": ["name","address_prefixes","virtual_network_name"],
"azurerm_public_ip_prefix": ["name","sku","ip_prefix","prefix_length","zones"],
"azurerm_firewall_policy_rule_collection_group": ["name"],
"azurerm_virtual_network_peering": ["name","allow_forwarded_traffic","allow_gateway_transit","allow_virtual_network_access","use_remote_gateways"]
}
def extract_resource_values(resource):
# Use regular expressions to extract the values for the attributes
# that you are interested in from the state_info list
resource_values = {}
#print(resource['type'])
if resource['type'] in properties:
resource_values['type'] = resource['type']
values = properties.get(resource['type'])
for value in resource['values']:
if value in values:
resource_values[value] = resource['values'][value]
if resource_values:
return resource_values
def create_resource_table(resource_type, resources):
# Create a markdown table for the specified resource type, with the resource
# properties as the columns and the resource values as the rows
table = "## " + resource_type + "\n"
# Get the list of properties for the resource type
resource_properties = properties[resource_type]
# Generate the table header by iterating over the properties
table_header = ["|"]
for property in resource_properties:
table_header.append(property)
table_header.append("|")
table += "".join(table_header) + "\n"
# Generate the table row separator by iterating over the properties
table_row_separator = ["|"]
for _ in range(len(resource_properties)):
table_row_separator.append("-|")
table += "".join(table_row_separator) + "\n"
# Generate the table rows by iterating over the resources
for resource in resources:
# Generate the table row by iterating over the properties
table_row = ["|"]
for property in resource_properties:
# Convert the list of values to a string using an empty string as the separator
property_values = resource[property]
property_value = "".join(str(property_values))
table_row.append(property_value)
table_row.append("|")
table += "".join(table_row) + "\n"
return table
resources = []
# Iterate over the list of resources and print the resource type
for resource in raw_resources:
item = extract_resource_values(resource)
if item:
resources.append(item)
# Group the resources by their type
resources_by_type = {}
for resource in resources:
if resource["type"] not in resources_by_type:
resources_by_type[resource["type"]] = []
resources_by_type[resource["type"]].append(resource)
#print(resources_by_type)
# Create a markdown table for each group of resources
tables = []
for resource_type, resources in resources_by_type.items():
table = create_resource_table(resource_type, resources)
tables.append(table)
# Open the readme.md file in write mode
with open("readme.md", "w") as f:
# Iterate over the markdown tables
for table in tables:
# Write the contents of the table to the file
f.write(table)