-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper.py
More file actions
332 lines (275 loc) · 11.1 KB
/
helper.py
File metadata and controls
332 lines (275 loc) · 11.1 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import os
from utils import *
from preprocess import *
# Setting the filter in the environment
def set_filter(env):
env.filters["camelize"] = camelize
env.filters["pascalize"] = pascalize
env.filters["snakify"] = snakify
env.filters["is_list"] = is_list
env.filters["quote"] = quote
env.filters["make_dot_string"] = make_dot_string
env.filters["eliminate_zeroes"] = eliminate_zeroes
env.filters["eliminate_zeroes_and_capitalize"] = eliminate_zeroes_and_capitalize
env.filters["eliminate_dots_and_capitalize"] = eliminate_dots_and_capitalize
env.filters["get_first"] = get_first
env.filters["eliminate_first"] = eliminate_first
# Generate the Terraform Resource File
def generate_resource_file(file, provider_name):
print(f"=== Started Creation of Resource File")
try:
config = yaml.full_load(open(f'./config/resources/{file}.yml'))
except Exception as e:
print("Error: ", e)
print(f"=== Error in Creation of Resource File")
return False
env = Environment(loader=FileSystemLoader('./templates'),
trim_blocks=True, lstrip_blocks=True)
set_filter(env)
template = env.get_template('resource.j2')
# to save the results
try:
with open(f"output/resources/resource_{provider_name}_{file}.go", "w") as fh:
fh.write(template.render(config))
except Exception as e:
print("Error: ", e)
print(f"=== Error in Creation of Resource File")
return False
print(f"=== Completed Creation of Resource File")
return True
# Function will be called by cli, which is responsible for generation of Resource File
def generate_resource(files, provider_name):
isDir = os.path.isdir('./output/resources')
if not isDir:
os.mkdir('./output/resources')
isDirPreprocess = os.path.isdir('./config/resources/preprocess')
if not isDirPreprocess:
os.mkdir('./config/resources/preprocess')
for file in files:
if file == "preprocess":
continue
if file[-4:] == ".yml":
file = file[:-4]
status = pre_process(file)
if not status:
exit()
status = generate_resource_file(file, provider_name)
if not status:
exit()
# Function will be called by cli, which is responsible for generation of Resource Test File
def generate_resource_test(files,provider_name):
for file in files:
if file == "preprocess":
continue
if file[-4:] == ".yml":
file = file[:-4]
status = generate_resource_test_file(file,provider_name)
if not status:
exit()
# Generate the Terraform Provider File
def generate_provider_file():
print(f"=== Started Creation of Provider File")
try:
config = yaml.full_load(open(f'./config/provider.yml'))
except Exception as e:
print("Error: ", e)
print(f"=== Error in Creation of Provider File")
return False
env = Environment(loader=FileSystemLoader('./templates'),
trim_blocks=True, lstrip_blocks=True)
set_filter(env)
template = env.get_template('provider.j2')
# to save the results
try:
with open(f"output/provider.go", "w") as fh:
fh.write(template.render(config))
except Exception as e:
print("Error: ", e)
print(f"=== Error in Creation of Provider File")
return False
print(f"=== Completed Creation of Provider File")
return True
# Function will be called by cli, which is responsible for generation of Provider File
def generate_provider():
status = generate_provider_file()
if not status:
exit()
# Function will be called by cli, which is responsible for generation of Provider Test File
def generate_provider_test():
status = pre_process_for_provider()
if not status:
exit()
status = generate_provider_test_file()
if not status:
exit()
# Generate the Terraform Datasource Test File
def generate_datasource_test_file(file, provider_name):
print(f"=== Started Creation of Datasource Test File named {file}")
try:
config = yaml.full_load(open(f'./config/datasources/{file}.yml'))
except Exception as e:
print("Error: ", e)
print(f"=== Error in Creation of Datasource Test File named {file}")
return False
env = Environment(loader=FileSystemLoader('./templates'),
trim_blocks=True, lstrip_blocks=True)
set_filter(env)
template = env.get_template('datasource_test.j2')
# to save the results
try:
with open(f"output/datasources/datasource_{provider_name}_{file}_test.go", "w") as fh:
fh.write(template.render(config))
except Exception as e:
print("Error: ", e)
print(f"=== Error in Creation of Datasource Test File named {file}")
return False
print(f"=== Completed Creation of Datasource Test File named {file}")
return True
# Function will be called by cli, which is responsible for generation of Datasource Test File
def generate_datasource_test(files,provider_name):
isDir = os.path.isdir('./output/datasources')
if not isDir:
os.mkdir('./output/datasources')
for file in files:
if file[-4:] == ".yml":
file = file[:-4]
status = generate_datasource_test_file(file,provider_name)
if not status:
exit()
# Generate the Terraform DataSource File
def generate_datasource_file(file,provider_name):
print(f"=== Started Creation of Datasource File named {file}")
try:
config = yaml.full_load(open(f'./config/datasources/{file}.yml'))
except Exception as e:
print("Error: ", e)
print(f"=== Error in Creation of Datasource File named {file}")
return False
env = Environment(loader=FileSystemLoader('./templates'),
trim_blocks=True, lstrip_blocks=True)
set_filter(env)
template = env.get_template('datasource.j2')
# to save the results
try:
with open(f"output/datasources/datasource_{provider_name}_{file}.go", "w") as fh:
fh.write(template.render(config))
except Exception as e:
print("Error: ", e)
print(f"=== Error in Creation of Datasource File named {file}")
return False
print(f"=== Completed Creation of Datasource File named {file}")
return True
# Function will be called by cli, which is responsible for generation of Datasource File
def generate_datasource(files,provider_name):
isDir = os.path.isdir('./output/datasources')
if not isDir:
os.mkdir('./output/datasources')
for file in files:
if file[-4:] == ".yml":
file = file[:-4]
status = generate_datasource_file(file,provider_name)
if not status:
exit()
# Generate the Terraform Model File
def generate_model_file(file, provider_name):
print(f"=== Started Creation of Model File for Resource named {file}")
try:
config = yaml.full_load(open(f'./config/resources/{file}.yml'))
except Exception as e:
print("Error: ", e)
print(f"=== Error in Creation of Model File for Resource named {file}")
return False
env = Environment(loader=FileSystemLoader('./templates'),
trim_blocks=True, lstrip_blocks=True)
set_filter(env)
template = env.get_template('model.j2')
# to save the results
try:
with open(f"output/models/{file}.go", "w") as fh:
fh.write(template.render(config))
except Exception as e:
print("Error: ", e)
print(f"=== Error in Creation of Model File for Resource named {file}")
return False
print(f"=== Completed Creation of Model File for Resource named {file}")
return True
# Function will be called by cli, which is responsible for generation of Model File
def generate_model(files,provider_name):
isDir = os.path.isdir('./output/models')
if not isDir:
os.mkdir('./output/models')
for file in files:
if file == "preprocess":
continue
if file[-4:] == ".yml":
file = file[:-4]
status = generate_model_file(file,provider_name)
if not status:
exit()
def generate_client_test(inputs):
generate_client(inputs, "client_test.j2", "_test.go", " test")
def generate_client(inputs, template="client.j2", postfix=".go", test = ""):
for file in inputs:
# check that file exists
file = file.split(".")[0] + ".yml"
fileExists = os.path.isfile(f"./config/client/{file}")
if not fileExists:
print(f"=== WARNING: ./config/client/{file} does not exist, skipping.")
continue
# parse yaml
print(f"=== INFO: Beginning creation of client{test} for {file}")
try:
config = yaml.full_load(open(f'./config/client/{file}'))
except Exception as e:
print(f"=== ERROR: Error parsing {file}. skipping.")
print(e)
continue
# load jinja environment
env = Environment(loader=FileSystemLoader('./templates'),
trim_blocks=True, lstrip_blocks=True)
template = env.get_template(f'{template}')
# render the template
filename = file.split(".")[0]
try:
with open(f"output/client/{filename}{postfix}", "w") as fh:
fh.write(template.render(config))
except Exception as e:
print(f"=== ERROR: This error may have happened due to insufficient write permissions or due to some syntatical error in the jinja template. Refer the error below for more info.")
print(e)
continue
print(f"=== INFO: Created - client{test} for {file}")
return
# Generate the Terraformer File
def generate_terraformer_file(file, provider_name):
print(f"=== Started Creation of Terraformer File named {file}")
try:
config = yaml.full_load(open(f'./config/terraformer/{file}.yml'))
except Exception as e:
print("Error: ", e)
print(f"=== Error in Creation of Terraformer File")
return False
env = Environment(loader=FileSystemLoader('./templates'),
trim_blocks=True, lstrip_blocks=True)
set_filter(env)
template = env.get_template('terraformer.j2')
# to save the results
try:
with open(f"output/terraformer/{file}.go", "w") as fh:
fh.write(template.render(config))
except Exception as e:
print("Error: ", e)
print(f"=== Error in Creation of Terraformer File named {file}")
return False
print(f"=== Completed Creation of Terraformer File named {file}")
return True
# Function will be called by cli, which is responsible for generation of Terraformer File
def generate_terraformer(files, provider_name):
isDir = os.path.isdir('./output/terraformer')
if not isDir:
os.mkdir('./output/terraformer')
for file in files:
if file[-4:] == ".yml":
file = file[:-4]
status = generate_terraformer_file(file, provider_name)
if not status:
exit()