-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.py
451 lines (367 loc) · 16.9 KB
/
handler.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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
import os
import requests
import time
import boto3
import uuid
from json import loads, dumps
from requests.auth import HTTPBasicAuth
from json import dumps, loads
from datetime import datetime
# Globar parameters
headers = {"Content-type": "application/json", "Accept": "application/json"}
# Account credentials
boomi_account_id = os.environ["BOOMI_ACCOUNT_ID"]
boomi_username = os.environ["BOOMI_USERNAME"]
boomi_api_token = os.environ["BOOMI_API_TOKEN"]
# Boomi environment id
boomi_environments = {
"DEV": {
"environmentId": os.environ["DEV_ENV_ID"]
},
"QAT": {
"environmentId": os.environ["QAT_ENV_ID"]
},
"PROD": {
"environmentId": os.environ["PRD_ENV_ID"]
}
}
# Boomi AtomSphere API Endpoints
boomi_atom_sphere_api_endpoint = os.environ["BOOMI_API_ENDPOINT"]
boomi_component_meta_endpoint = "{}/{}/{}".format(boomi_atom_sphere_api_endpoint, boomi_account_id, "ComponentMetadata/query")
boomi_component_meta_more_endpoint = "{}/{}/{}".format(boomi_atom_sphere_api_endpoint, boomi_account_id, "ComponentMetadata/queryMore")
boomi_environment_endpoint = "{}/{}/{}".format(boomi_atom_sphere_api_endpoint, boomi_account_id, "Environment/query")
boomi_deploy_package_query_endpoint = "{}/{}/{}".format(boomi_atom_sphere_api_endpoint, boomi_account_id, "DeployedPackage/query")
boomi_deploy_package_endpoint = "{}/{}/{}".format(boomi_atom_sphere_api_endpoint, boomi_account_id, "DeployedPackage/")
class Error(Exception):
"""Base-class for all exceptions raised by this module"""
class ValueError(Error):
"""Base-class for value errors"""
class ConnectionError(Error):
"""Base-class for connection to Boomi AtomSphere API errors, especially for those not 2xx HTTP Response Status"""
def get_result(event, context):
token = ""
# Get input parameter
if event.get("pathParameters") and event.get("pathParameters").get("token"):
token = event.get("pathParameters").get("token")
else:
return {"statusCode": 400, "body": "Missing Token", "headers": {"Content-Type": "text/plain"}}
# Get result from DynamoDB
client = boto3.client('dynamodb')
response = client.get_item(
TableName="boomi-cd-table",
Key={
"token": {
'S': token
}
})
if response.get("Item"):
if response.get("Item").get("process_status").get("S") == "IN PROGRESS":
data = {
"token": token,
"input": loads(response.get("Item").get("input").get("S")),
"status": response.get("Item").get("process_status").get("S")
}
else:
data = {
"token": token,
"input": loads(response.get("Item").get("input").get("S")),
"status": response.get("Item").get("process_status").get("S"),
"component": loads(response.get("Item").get("component").get("S"))
}
else:
return {"statusCode": 400, "body": {"message": "Incorrect Token"}}
return {"statusCode": 200, "body": dumps(data, indent=2)}
def deploy(event, context):
# Get input parameter
input_parameter = loads(event.get("body"))
# Validate input parameter and environment
try:
validate_input_parameter(input_parameter)
except ValueError as ve:
return {"statusCode": 400, "body": str(ve), "headers": {"Content-Type": "text/plain"}}
except ConnectionError as ce:
return {"statusCode": 500, "body": str(ce), "headers": {"Content-Type": "text/plain"}}
except Exception as e:
return {"statusCode": 500, "body": "Process Error", "headers": {"Content-Type": "text/plain"}}
# Generate token and set into input_parameter for next process
token = str(uuid.uuid4())
input_parameter['token'] = token
# Create a new record in DynamoDB
client = boto3.client("dynamodb")
response = client.put_item(
TableName="boomi-cd-table",
Item={
"token": {
"S": token
},
"process_status": {
"S": "IN PROGRESS"
},
"input": {
"S": dumps(input_parameter)
}
})
# Push input parameter to SQS
client = boto3.client('sqs')
response = client.send_message(QueueUrl=os.environ["SQS_QUEUE_URL"], MessageBody=dumps(input_parameter))
# return token
return {"statusCode": 200, "body": dumps({"token": token})}
def process_deployment(event, context):
# Get input parameter
input_parameter = loads(event.get("Records")[0].get("body"))
print("Process deployment : {}".format(event))
# Set required values to local variables
token = input_parameter.get("token")
env_source = input_parameter.get('environment').get('source')
env_destination = input_parameter.get('environment').get('destination')
projects = input_parameter.get('projects')
notes = input_parameter.get('notes')
# Populate component detail
list_component = populate_component(projects)
# Get Package ID for all components
# list_component = populate_packageId(env_source, list_component)
populate_packageId(env_source, list_component)
# Deploy package to destination environment
# list_component = deploy_package(env_destination, list_component, notes)
print("Number of component in list = {}".format(len(list_component)))
deploy_package(env_destination, list_component, notes)
# Create a new record in DynamoDB
client = boto3.client("dynamodb")
response = client.update_item(
TableName="boomi-cd-table",
Key={
"token": {
"S": token
}
},
UpdateExpression="set component = :val1, process_status = :val2",
ExpressionAttributeValues={
":val1": {
"S": dumps(list_component)
},
":val2": {
"S": "DONE"
}
},
ReturnValues="UPDATED_NEW"
)
return {"statusCode": 200, "body": "Success", "Content-Type":"text/plain"}
def deploy_package(env_destination, list_component, notes):
now = datetime.now()
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
notes = "{} - {} ".format(dt_string, notes)
print("Number of component to be deployed = {}".format(len(list_component)))
for item in list_component:
id = item.get("packageId")
name = item.get("name")
payload = { "environmentId" : boomi_environments[env_destination]['environmentId'],
"packageId" : id,
"notes" : notes
}
print("{} to {} :: payload: {}".format(name, env_destination, payload))
response = requests.post(boomi_deploy_package_endpoint,
headers=headers,
auth=HTTPBasicAuth(boomi_username, boomi_api_token),
data=dumps(payload))
if response.status_code == 200:
item["status"] = "DEPLOYED"
print("{} :: Id: {} is DEPLOYED.".format(name, id))
elif response.status_code == 400:
item["status"] = "EXISTING"
print("{} :: Id: {} is use existing.".format(name, id))
else:
item["status"] = "ERROR"
item["errorMessage"] = response.text
print("{} :: Id: {} encounter error {}.".format(name, id, response.text))
time.sleep(1)
return list_component
def populate_packageId(env_source, list_component):
for item in list_component:
id = item.get("componentId")
name = item.get("name")
payload = {
"QueryFilter": {
"expression": {
"operator": "and",
"nestedExpression": [
{
"argument": [
id
],
"operator": "EQUALS",
"property": "componentId"
},
{
"argument": [
boomi_environments[env_source]['environmentId']
],
"operator": "EQUALS",
"property": "environmentId"
},
{
"argument": [
True
],
"operator": "EQUALS",
"property": "active"
}
]
}
}
}
response = requests.post(boomi_deploy_package_query_endpoint,
headers=headers,
auth=HTTPBasicAuth(boomi_username, boomi_api_token),
data=dumps(payload))
if response and response.text:
resp_body = loads(response.text)
if resp_body and resp_body.get('result') and len(resp_body.get('result')) > 0:
packageId = resp_body.get('result')[0]['packageId']
item["packageId"] = packageId
print("{} :: componentId: {} :: packageId: {}".format(name, id, packageId))
else:
print("{} :: componentId: {} :: packageId: N/A".format(name, id))
else:
print("{} :: Invalid Response: {} - {}".format(name, response, response.text))
time.sleep(1)
# return list_component
def populate_component(projects):
list_component = []
for item in projects:
payload = {
"QueryFilter": {
"expression": {
"operator": "and",
"nestedExpression": [
{
"argument": [
"{}%".format(item.get("componentPrefix"))
],
"operator": "LIKE",
"property": "name"
},
{
"argument": [
item.get("projectName")
],
"operator": "EQUALS",
"property": "folderName"
},
{
"argument": [
False
],
"operator": "EQUALS",
"property": "deleted"
},
{
"argument": [
True
],
"operator": "EQUALS",
"property": "currentVersion"
}
]
}
}
}
response = requests.post(boomi_component_meta_endpoint,
headers=headers,
auth=HTTPBasicAuth(boomi_username, boomi_api_token),
data=dumps(payload))
resp_body = loads(response.text)
if resp_body.get('queryToken'):
query_token = resp_body['queryToken']
else:
query_token = None
print("Number of total records in result : {}".format(resp_body['numberOfResults']))
list_component.extend(resp_body['result'])
has_next = False
if query_token:
has_next = True
while (has_next):
payload = query_token
response = requests.post(boomi_component_meta_more_endpoint,
headers=headers,
auth=HTTPBasicAuth(boomi_username, boomi_api_token),
data=query_token)
resp_body = loads(response.text)
if resp_body.get('queryToken'):
query_token = resp_body['queryToken']
else:
query_token = None
list_component.extend(resp_body['result'])
if query_token:
continue
else:
has_next = False
print("Number of total records in list : {}".format(len(list_component)))
return list_component
def verify_environment(environment_id):
is_valid = False
payload = {
"QueryFilter" :
{
"expression" :
{
"argument" : [environment_id],
"operator" : "EQUALS",
"property" : "id"
}
}
}
response = requests.post(boomi_environment_endpoint,
headers=headers,
auth=HTTPBasicAuth(boomi_username, boomi_api_token),
data=dumps(payload))
if response.status_code == 200:
response_body = loads(response.text)
if response_body.get("result"):
if response_body.get("result")[0]['id']:
is_valid = True
else:
raise ConnectionError("Response from Boomi AtomSphereAPI : {}\nError message: {}".format(response.status_code, response.text))
def validate_input_parameter(input_parameter):
# Check if input_parameter is available
if input_parameter is None:
raise ValueError("Error: Missing input parameter")
# Check if environment (source and destination) is available
if input_parameter.get("environment") is None:
raise ValueError("Error: Environment is mandatory")
else:
environment = input_parameter.get("environment")
if environment.get("source") is None:
raise ValueError("Error: Source Environment is mandatory")
if environment.get("destination") is None:
raise ValueError("Error: Destination Environment is mandatory")
# Check if projects is available
if input_parameter.get("projects") is None:
raise ValueError("Error: Projects is mandatory")
else:
projects = input_parameter.get("projects")
if len(projects) == 0:
raise ValueError("Error: Projects must have at least one project")
# Check value of source and destination environments
available_env = list(boomi_environments.keys())
available_source = ["DEV", "QAT"]
available_destination = ["QAT", "PROD"]
if input_parameter.get("environment").get('source') not in available_env:
raise ValueError("Error: Source environment is not available")
if input_parameter.get("environment").get('destination') not in available_env:
raise ValueError("Error: Destination environment is not available")
if input_parameter.get("environment").get('source') == input_parameter.get("environment").get('destination'):
raise ValueError("Error: Source and Destination environment cannot be same value")
if input_parameter.get("environment").get('source') not in available_source:
raise ValueError("Error: Source environment is only available for DEV and QAT")
if input_parameter.get("environment").get('destination') not in available_destination:
raise ValueError("Error: Destination environment is only available for QAT and PROD")
# Check project name and component prefix
for project in projects:
if project.get('projectName') is None:
raise ValueError("Error: Project Name is mandatory")
if project.get('componentPrefix') is None:
raise ValueError("Error: Component Prefix is mandatory")
env_source = input_parameter.get('environment').get('source')
verify_environment(boomi_environments[env_source]["environmentId"])
env_destination = input_parameter.get('environment').get('destination')
verify_environment(boomi_environments[env_destination]["environmentId"])