-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
132 lines (115 loc) · 4.37 KB
/
main.py
File metadata and controls
132 lines (115 loc) · 4.37 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
"""This script is google cloud function that listens on a topic
and sends a text alert based on the payload passed to it in json)"""
import base64
import inspect
import ast
import logging
import logging.handlers
from decouple import config
from twilio.rest import Client
from twilio.base.exceptions import TwilioRestException
from google.cloud import secretmanager
#setting up loggers
logger = logging.getLogger('textAlert')
logger.setLevel(logging.DEBUG)
fh = logging.handlers.RotatingFileHandler(
config(
'LOGGING_LOCATION',
default='/var/log/textalert.log'
),
maxBytes=10240,
backupCount=5
)
fh.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
ch = logging.StreamHandler()
ch.setLevel(logging.WARNING)
ch.setFormatter(formatter)
logger.addHandler(fh)
logger.addHandler(ch)
def assign_secret_variable( secret_id, project_id=config('GCP_PROJECT'), version_id='latest'):
"""This funtion is used to fetch environement variables from google clouds seceret store"""
client = secretmanager.SecretManagerServiceClient()
name = f"projects/{project_id}/secrets/{secret_id}/versions/{version_id}"
# Build the resource name of the secret version.
name = f"projects/{project_id}/secrets/{secret_id}/versions/{version_id}"
# Access the secret version.
response = client.access_secret_version(request={"name": name})
# Return the secret payload.
#
# WARNING: Do not print the secret in a production environment - this
# function is to access the secret material.
payload = response.payload.data.decode("UTF-8")
return payload
def get_secret(secret_name):
""" A wrapper for the secret variable assignement to perform a get"""
response = assign_secret_variable(secret_name)
return response
def send_text(recipiant, payload):
""" the fuction to send data to the twillio API in order to deliver the text message """
message = {}
client = Client(payload['account_sid'], payload['auth_token'])
try:
message = client.messages.create(
from_=payload['from'],
body=payload['alert'],
to=recipiant
)
if inspect.isclass(message) is True:
logger.info(message.sid)
except TwilioRestException as error:
logger.debug(
'Exeption thrown HTTP %s error : %s | for more info %s ',
error.status,
error.msg,
error.uri
)
message = {
'uri' : error.uri,
'status' : error.status,
'msg' : error.msg,
'code' : error.code,
'method' : error.method,
'details' : error.details,
'error' : 'Text not sent',
'recipiant': recipiant
}
logger.error(message['code'])
logger.error(message['error'])
return message
def unpack_data(data):
"""Function to unpack data from its encoded form"""
result = ""
result = base64.b64decode(data).decode('utf-8')
# TODO: what is this eval doing? there should be a better way to do this in python. # pylint: disable=W0511
result = ast.literal_eval(result) #pylint disable:W0123
return result
def build_payload(alert):
"""Function to build payload to pass to twilio"""
payload = {}
payload['alert'] = alert
payload['account_sid'] = get_secret('TWILIO_ACCOUNT_SID')
payload['auth_token'] = get_secret('TWILIO_AUTH_TOKEN')
payload['from'] = get_secret('TWILIO_FROM')
return payload
def textalert(event, context):
"""Fucntion called by google cloud message """
message = ''
if 'data' in event:
data = unpack_data(event['data'])
else:
data = False
message = r"No data passed in event consumed, "
message = message + r"please check the producer is sending event[\'data\'\]"
logger.info(message)
return message
logger.info(" [x] Received %s | %s", data, context)
recipiants = data['recipiants']
payload = build_payload(data['alert'])
for recipiant in recipiants:
logger.info('texting %s : %s', recipiant, payload['alert'])
result = send_text(recipiant, payload)
logger.debug(result)
logger.info(" [x] Done")
return message