-
Notifications
You must be signed in to change notification settings - Fork 2
/
terraform.py
216 lines (180 loc) · 8.92 KB
/
terraform.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
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
import json
def createEnvironment(test, bucket):
jsonData = test.testDetail
for index, environment in enumerate(jsonData["environments"]):
bucket.allEnvironments[environment["id"]] = "runscope_environment." + str(index) + editName(bucket.jsonData["name"]) + "_" + editName(jsonData["name"]) + "_" + editName(environment["name"]) + ".id"
test.dataToFile += """resource "runscope_environment" "{}{}_{}_{}" {{
bucket_id = \"${{var.bucket_id}}\"
test_id = \"${{runscope_test.{}.id}}\"
name = \"{}\"
regions = {}
retry_on_failure = {}
initial_variables = {}
script = {}
verify_ssl = {}
preserve_cookies = {}
integrations = {}
remote_agents = {}
{}
}}\n\n""".format(index, editName(bucket.jsonData["name"]), editName(jsonData["name"]), editName(environment["name"]), editName(jsonData["name"]), environment["name"], json.dumps(environment["regions"]), str(environment["retry_on_failure"]).lower(), (editAssertions(environment["initial_variables"]) if environment["initial_variables"] != None else "{}") if environment["parent_environment_id"] == None else editAssertions(getIntialValues(environment["parent_environment_id"], bucket.sharedEnvironments)), json.dumps(environment["script"]) if environment["script"] != None else "\"\"", str(environment["verify_ssl"]).lower(), str(environment["preserve_cookies"]).lower(), json.dumps(getIntegrations(environment["integrations"])), editAssertions(environment["remote_agents"]) if len(environment["remote_agents"]) > 0 else [], extension(environment) if bucket.extension else "")
def createSharedEnvironment(bucket):
for environment in bucket.sharedEnvironments:
bucket.allEnvironments[environment["id"]] = "var.shared_environment_" + editName(bucket.jsonData["name"]) + "_" + editName(environment["name"])
bucket.dataToFile += """resource \"runscope_environment\" \"shared_environment_{}_{}\" {{
bucket_id = \"${{runscope_bucket.{}.id}}\"
name = \"{}\"
regions = {}
retry_on_failure = {}
initial_variables = {}
script = {}
verify_ssl = {}
preserve_cookies = {}
integrations = {}
remote_agents = {}
{}
}}\n\n""".format(editName(bucket.jsonData["name"]), editName(environment["name"]), editName(bucket.jsonData["name"]), environment["name"], json.dumps(environment["regions"]), str(environment["retry_on_failure"]).lower(), editAssertions(environment["initial_variables"]) if environment["initial_variables"] != None else "{}", json.dumps(environment["script"]) if environment["script"] != None else "\"\"", str(environment["verify_ssl"]).lower(), str(environment["preserve_cookies"]).lower(), json.dumps(getIntegrations(environment["integrations"])), editAssertions(environment["remote_agents"]) if len(environment["remote_agents"]) > 0 else [], extension(environment) if bucket.extension else "")
def createSchedule(test, bucket):
jsonData = test.testDetail
for index, schedule in enumerate(jsonData["schedules"]):
test.dataToFile += """resource \"runscope_schedule\" \"schedule{}_{}\" {{
bucket_id = \"${{var.bucket_id}}\"
test_id = \"${{runscope_test.{}.id}}\"
interval = \"{}\"
environment_id = \"${{{}}}\"
note = \"{}\"
}}\n\n""".format(index, editName(jsonData["name"]), editName(jsonData["name"]), schedule["interval"], bucket.allEnvironments[schedule["environment_id"]], schedule["note"] if schedule["note"] != None else "")
def createTestStep(test, bucket):
jsonData = test.testDetail
for index, step in enumerate(jsonData["steps"]):
test.dataToFile += """resource \"runscope_step\" \"step{}_{}\" {{
bucket_id = \"${{var.bucket_id}}\"
test_id = \"${{runscope_test.{}.id}}\"
step_type = \"{}\"
url = \"{}\"
method = \"{}\"
{}
assertions = [{}
]
variables = [{}
]
scripts = {}
before_scripts = {}
body = {}
{}
}}\n\n""".format(index, editName(jsonData["name"]), editName(jsonData["name"]), step["step_type"], step["url"], step["method"], getHeaders(jsonData, step["headers"], bucket), editAssertions(step["assertions"]), editAssertions(step["variables"]), json.dumps(step["scripts"]) if "scripts" in step and step["scripts"] != [''] else [], json.dumps(step["before_scripts"]) if "before_scripts" in step else [], json.dumps(step["body"]) if "body" in step and step["body"] != None else "\"\"", dependsOn(index, jsonData["name"]))
def createTest(test):
jsonData = test.jsonData
test.dataToFile += """resource \"runscope_test\" \"{}\" {{
name = \"{}\"
description = {}
bucket_id = \"${{var.bucket_id}}\"
}}\n\n""".format(editName(jsonData["name"]), jsonData["name"], json.dumps(jsonData["description"]) if jsonData["description"] != None else "\"\"")
def createBucket(bucket):
bucket.dataToFile += """resource \"runscope_bucket\" \"{}\" {{
name = \"{}\"
team_uuid = \"{}\"
}}\n\n""".format(editName(bucket.jsonData["name"]), bucket.jsonData["name"], bucket.jsonData["team"]["id"])
def createModule(bucket, folder):
bucket.dataToFile += """module \"tests_{}\" {{
source = \"./{}\"
bucket_id = \"${{runscope_bucket.{}.id}}\"
{}
}}""".format(editName(bucket.jsonData["name"]), folder, editName(bucket.jsonData["name"]), getSharedEnv(bucket))
def getSharedEnv(bucket):
result = ""
for env in bucket.sharedEnvironments:
result += "shared_environment_" + editName(bucket.jsonData["name"]) + "_" + editName(env["name"]) + " = \"${runscope_environment.shared_environment_" + editName(bucket.jsonData["name"]) + "_" + editName(env["name"]) + ".id}\"\n"
return result
def makeMainFile(access_token):
print("\nCreating init file")
f = open("main.tf", "w")
f.write("""provider "runscope" {
access_token = "%s"
}""" % (access_token))
f.close()
def getIntegrations(jsonData):
result = []
for i in jsonData:
result.append(i["id"])
return result
def createVariables(folderName, bucket):
f = open(folderName + "/variables.tf", "w")
sharedEnvironments = ""
for env in bucket.sharedEnvironments:
sharedEnvironments += """variable "{}" {{}}\n""".format("shared_environment_" + editName(bucket.jsonData["name"]) + "_" + editName(env["name"]))
f.write("""variable "bucket_id" {{}}\n{}""".format(sharedEnvironments))
f.close()
def editAssertions(jsonText):
if type(jsonText) != list:
jsonText = [jsonText]
result = ""
for i in jsonText:
result += "\n\t {\n"
for j in i.keys():
result += "\t {} = \"{}\",\n".format(j, "" if i[j] == "\"\"" else i[j])
result += "\t },"
return result
def editName(fileName):
result = ""
for char in fileName:
if char in " /?":
result += "_"
elif not char.isalnum():
continue
else:
result += char
return result
def dependsOn(index, stepName):
if index != 0:
return """depends_on = ["{}{}_{}"]""".format("runscope_step.step", index -1, editName(stepName))
else:
return ""
def getIntialValues(envID, sharedEnvironments):
for sharedEnv in sharedEnvironments:
if envID == sharedEnv["id"]:
return sharedEnv["initial_variables"]
def editEnvironments(bucket):
bucket.editedEnvironments = True
for environment in bucket.sharedEnvironments:
if environment["headers"] != None:
for key in environment["headers"]:
environment["headers"][key] = environment["headers"][key].pop() # we get array with value ['value'], but we just want a single value
def extension(environment):
return """
webhooks = {}
emails =
{{
notify_all = {}
notify_on = \"{}\"
notify_threshold = {}
recipients = [{}
]
}}""".format(json.dumps(environment["webhooks"]) if environment["webhooks"] != None else [], str(environment["emails"]["notify_all"]).lower(), environment["emails"]["notify_on"] if environment["emails"]["notify_on"] != None else "", environment["emails"]["notify_threshold"], editAssertions(environment["emails"]["recipients"]))
def useSharedEnv(jsonData, sharedEnvironments):
for schedule in jsonData["schedules"]:
envID = schedule["environment_id"]
for sharedEnv in sharedEnvironments:
if envID == sharedEnv["id"]:
return True
for env in jsonData["environments"]:
if env["parent_environment_id"] == sharedEnv["id"]:
return True
return False
def getHeaders(jsonData, headers, bucket):
result = {}
if useSharedEnv(jsonData, bucket.sharedEnvironments):
if not bucket.editedEnvironments:
editEnvironments(bucket)
for env in bucket.sharedEnvironments:
if env["headers"] != None: result.update(env["headers"])
if headers != None:
for key in headers:
headers[key] = headers[key].pop() # we get array with value ['value'], but we just want a single value
result.update(headers)
resultText = "headers\t\t = {"
for key in result:
resultText += """
header = \"{}\"
value = \"{}\"\n""".format(key, result[key])
resultText += "\t}"
return "" if not result else resultText