forked from amirams/spotinst-functions-examples
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathhandler.py
143 lines (134 loc) · 5.24 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
"""
This function will connect to your database and if you do not enter any URL parameters
it will return with all the values in your data table. If you enter 'number=1' into
the URL parameters then we will attempt to add a new row into the data table. First
we will check that all the neccessary parameters are entered into the URL then check
that the row does not already exsist in the data table. If both checks pass then
the new row will be inserted into the data table
To personalize this function you will need to enter your own MySQL credentials as well
as check the appropriate query paramneters that are needed for your data table throughout
this function.
"""
import mysql.connector
"""This function when called will print out all the values in the data table to the console
Args:
cursor: This holds the connection to the database and will execute the query then
hold the result to be printed
"""
def getAllRows(cursor):
query = "SELECT * FROM customers"
print(query)
cursor.execute(query)
for element in cursor:
print(element)
"""
This funcition takes in the query parameters and check that they are all completed.
It will return False if all params are there and True if even one is missing.
Here is where you should enter in your own data table columns that are required for
inserting a new row
Args:
queryparams: This is where the URL parameters live and are reference by name.
Returns:
(Bool): Will return False if all parameters are set in the URL
"""
def checkParams(queryparams):
try:
queryparams['firstName']
queryparams['lastName']
queryparams['email']
queryparams['instances']
except:
return True
else:
return False
"""
This function will take in the connection to the DB and the array of parameters to check
that the row that is being entered does not already exsist in the data table. If it does
the function will return True, if not it will return False
Args:
queryparams: This is where the URL parameters live and are reference by name.
cursor: This holds the connection to the database and will execute the
query then hold the result to be printed
Returns:
(Bool): Will return True if the row exsist in the data table
"""
def checkInTable(queryparams, cursor):
testString = "first_name='" + queryparams['firstName'] + "' AND last_name='" + queryparams['lastName'] + "' AND email='" + queryparams['email'] + "'"
testQuery = "SELECT COUNT(*) FROM customers WHERE " + testString
cursor.execute(testQuery)
for element in cursor:
if(element[0]>0):
return True
return False
"""
This function will insert a new row into the data table. You need the query parameters
to know what values to insert, you need the cursor to execute the Insert function and
you need the connection object to write those changes
Args:
queryparams: This is where the URL parameters live and are reference by name.
cursor: This holds the connection to the database and will execute the
query then hold the result to be printed
connection: This connection to the DB allows you to write the new value to the table
"""
def insertNewRow(queryparams, cursor, connection):
queryString = "( '" + queryparams['firstName'] + "', '" + queryparams['lastName'] + "', '" + queryparams['email'] + "', '" + queryparams['instances'] + "' )"
query = "INSERT INTO customers (first_name, last_name, email, instances) VALUES"+queryString
print(query)
cursor.execute(query)
connection.commit()
"""
This is the main function that is executed by the Serverless framework. It takes in
the URL parameters and return a serverless object that either return status code 400
if there was an error. Here is where you will need to configure your MySQL database
connection
Args:
arg: Serverless object that contains the URL parameters
Returns:
(Serverless Object): Serverless object that will have a status code 400 if there
was an error executing the code or 200 if not
"""
def main(event, context):
#configuring connection to Database
config = {
'user':{Your Username},
'password':{Your Password},
'host':{Your Host Name},
'database':{Your Database Name}
}
#creating a connection to write to database and a cursor to read from database
connection = mysql.connector.connect(**config)
cursor = connection.cursor(buffered=True)
#getting URL parameters
queryparams = event.get("query", {})
# Checking if there is 'number=1' in the URL params
try:
queryparams['number']
except:
# Getting all rows if there is no 'number=1'
getAllRows(cursor)
else:
#checking that all the parameters are met
if checkParams(queryparams):
return {
'statusCode': 400,
'body': 'Parameters not defined',
'headers': {"Content-Type": "application/json"}
}
#checking that the row being inserted does not exist in the table
elif checkInTable(queryparams, cursor):
return {
'statusCode': 400,
'body': 'You have already entered this row',
'headers': {"Content-Type": "application/json"}
}
#inserting the new row if all the parameters are met
else:
insertNewRow(queryparams, cursor, connection)
#closing both connections to the database
cursor.close()
connection.close()
return {
'statusCode': 200,
'body': 'Success',
'headers': {"Content-Type": "application/json"}
}