-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlambda_function.py
executable file
·137 lines (113 loc) · 5.32 KB
/
lambda_function.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
# Copyright 2019 getcarrier.io
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
from os import environ
from email_client import EmailClient
from email_notifications import ApiEmailNotification
from ui_email_notification import UIEmailNotification
from time import sleep
from typing import Union
def lambda_handler(event: Union[list, dict], context):
try:
args = parse_args(event)
print(args)
if not args['notification_type']:
raise Exception('notification_type parameter is not passed')
# Send notification
if args['notification_type'] == 'api':
# Check required params
if not all([args['influx_host'], args['smtp_user'], args['test'], args['test_type'],
args['notification_type'], args['smtp_password'], args['user_list']]):
raise Exception('Some required parameters not passed')
email = ApiEmailNotification(args).email_notification()
elif args['notification_type'] == 'ui':
if not all([args['test_id'], args['report_id']]):
raise Exception('test_id and report_id are required for UI reports')
email = UIEmailNotification(args).ui_email_notification()
else:
raise Exception('Incorrect value for notification_type: {}. Must be api or ui'
.format(args['notification_type']))
EmailClient(args).send_email(email)
except Exception as e:
from traceback import format_exc
print(format_exc())
return {
'statusCode': 500,
'body': json.dumps(str(e))
}
return {
'statusCode': 200,
'body': json.dumps('Email has been sent')
}
def parse_args(event: Union[list, dict]):
if isinstance(event, list):
_event = event
else:
_event = [event]
args = {}
# Galloper or AWS Lambda service
event = _event[0] if not _event[0].get('body') else json.loads(_event[0]['body'])
# Galloper
args['galloper_url'] = environ.get("galloper_url") if not event.get('galloper_url') else event.get('galloper_url')
args['token'] = environ.get("token") if not event.get('token') else event.get('token')
args['project_id'] = environ.get("project_id") if not event.get('project_id') else event.get('project_id')
# Influx Config
args['influx_host'] = environ.get("influx_host") if not event.get('influx_host') else event.get('influx_host')
args['influx_port'] = event.get("influx_port", 8086)
args['influx_user'] = event.get("influx_user", "")
args['influx_password'] = event.get("influx_password", "")
# Influx DBs
args['comparison_db'] = event.get("comparison_db")
args['influx_db'] = event.get("influx_db")
# SMTP Config
args['smtp_port'] = environ.get("smtp_port", 465) if not event.get('smtp_port') else event.get('smtp_port')
args['smtp_host'] = environ.get("smtp_host", "smtp.gmail.com") if not event.get('smtp_host') else event.get(
'smtp_host')
args['smtp_user'] = environ.get('smtp_user') if not event.get('smtp_user') else event.get('smtp_user')
args['smtp_sender'] = args['smtp_user'] if not event.get('smtp_sender') else event.get('smtp_sender')
if not event.get('notification_type'):
args['notification_type'] = environ.get('notification_type')
else:
args['notification_type'] = event.get('notification_type')
if args['notification_type'] == 'ui':
args['test_type'] = event.get('test_suite')
if args['notification_type'] == 'api':
args['test_type'] = event.get('test_type')
args['type'] = args['test_type']
if args['notification_type'] == "api":
args['smtp_password'] = event.get("smtp_password")
else:
args['smtp_password'] = event.get('smtp_password')["value"]
# Test Config
args['users'] = event.get('users', 1)
args['test'] = event.get('test')
args['simulation'] = event.get('test')
args['env'] = event.get('env')
# summary data
args["test_data"] = event.get('test_data', {})
# Notification Config
args['user_list'] = event.get('user_list')
args['test_limit'] = event.get("test_limit", 5)
args['comparison_metric'] = event.get("comparison_metric", 'pct95')
# ui data
args['test_id'] = event.get('test_id')
args['report_id'] = event.get('report_id')
# SLA's
args['performance_degradation_rate'] = event.get('performance_degradation_rate')
args['missed_threshold_rate'] = event.get('missed_threshold_rate')
args['reasons_to_fail_report'] = event.get('reasons_to_fail_report')
args['performance_degradation_rate_qg'] = event.get('performance_degradation_rate_qg')
args['missed_thresholds_qg'] = event.get('missed_thresholds_qg')
args['status'] = event.get('status')
args['quality_gate_config'] = event.get('quality_gate_config')
return args