-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda_function.py
174 lines (142 loc) · 6.17 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
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
"""
This Lambda function enables API Gateway to use the GET and DELETE methods on your
dynamodb table.
Client <--> API Gateway <--> Lambda func <--> DynamoDB Table
"""
import json
import boto3
import pprint
from decimal import Decimal
from boto3.dynamodb.conditions import Key, Attr
from botocore.exceptions import ClientError
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('<Enter Table Name Here>') #define which dynamodb table to access
class DecimalEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, Decimal):
return int(obj)
return json.JSONEncoder.default(self, obj)
def query_table(ClassToFind):
queryreturn = table.query( # perform query
IndexName="ClassName-globo-index",
KeyConditionExpression= Key("ClassName").eq(ClassToFind),
ScanIndexForward=False
)
return queryreturn
def delete_item_operation(ClassPredictionID):
queryreturn = table.query( # perform query
IndexName="ClassID-globo-index",
KeyConditionExpression= Key("ClassPredictionID").eq(ClassPredictionID)
)
if not queryreturn['Items']:
not_found = "NOT_FOUND: Class details not found in the database."
return {
'statusCode': 500,
'headers': {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token',
'Access-Control-Allow-Credentials': 'true',
'Content-Type': 'application/json'
},
'body': json.dumps(not_found, indent=4, cls = DecimalEncoder)
}
elif queryreturn['Items']:
date = str(queryreturn['Items'][0]['PredictionDate'])
try:
response = table.delete_item(
Key={
'PredictionDate': date,
'ClassPredictionID': ClassPredictionID
},
ConditionExpression = "attribute_exists(ClassPredictionID)",
ReturnValues="ALL_OLD"
)
except ClientError as e:
if e.response['Error']['Code'] == "ConditionalCheckFailedException":
error = "Delete class details FAILED:" + " Moth item does not exist in the database."
return {
'statusCode': 500,
'headers': {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token',
'Access-Control-Allow-Credentials': 'true',
'Content-Type': 'application/json'
},
'body': json.dumps(error, indent=4, cls = DecimalEncoder)
}
else:
raise e
else:
if 'Attributes' in response:
success = "SUCCESS: Moth item deleted successfully from the database."
return {
'statusCode': 200,
'headers': {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token',
'Access-Control-Allow-Credentials': 'true',
'Content-Type': 'application/json'
},
'body': json.dumps(success, indent=4, cls = DecimalEncoder)
}
else:
return "Class details not found in the database."
def delete_all_items():
scan = table.scan()
with table.batch_writer() as batch:
for item in scan['Items']:
batch.delete_item(Key={'PredictionDate': item['PredictionDate'],
'ClassPredictionID': item['ClassPredictionID']})
del_all_success = "SUCCESS: Deleted all class details successfully."
return {
'statusCode': 200,
'headers': {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token',
'Access-Control-Allow-Credentials': 'true',
'Content-Type': 'application/json'
},
'body': json.dumps(del_all_success, indent=4, cls = DecimalEncoder)
}
def get_operation(ClassName):
if str(ClassName) == "AAW":
aaw_data = query_table('AAW')
response = aaw_data['Items']
elif str(ClassName) == "ECLW":
eclw_data = query_table('ECLW')
response = eclw_data['Items']
elif str(ClassName) == "FAW":
faw_data = query_table('FAW')
response = faw_data['Items']
elif str(ClassName) == "ALL":
body = table.scan()
response = body['Items']
else:
return {
'statusCode': '404',
'body': 'Class not found.'
}
return {
'statusCode': 200,
'headers': {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token',
'Access-Control-Allow-Credentials': 'true',
'Content-Type': 'application/json'
},
'body': json.dumps(response, indent=4, cls = DecimalEncoder)
}
def lambda_handler(event, context):
operation = event['httpMethod']
if operation == 'GET':
ClassName = event['queryStringParameters']['ClassName']
return get_operation(ClassName)
elif operation == 'DELETE':
try:
if str(event['queryStringParameters']['DeleteAll']) == "ALL":
return delete_all_items()
except KeyError:
ClassPredictionID = event['queryStringParameters']['ClassPredictionID']
return delete_item_operation(ClassPredictionID)
else:
return None