-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathWorkingLambda.py
131 lines (98 loc) · 3.63 KB
/
WorkingLambda.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
import json
import random
import boto3
#dynamo client
dynamodb = boto3.client('dynamodb')
#tableName = 'flux3000-feature-requests'
tableName = os.environ['TABLE_NAME']
#sns client
sns = boto3.client('sns')
# get topic arn from environment variable
topicarn = os.environ['TOPICARN']
#topicarn = 'arn:aws:sns:us-west-2:161615149547:FeatureRequests'
def get_named_parameter(event, name):
return next(item for item in event['parameters'] if item['name'] == name)['value']
def get_named_property(event, name):
return next(item for item in event['requestBody']['content']['application/json']['properties'] if item['name'] == name)['value']
def createFeatureRequest(event):
print("calling method: create feature request")
featureRequestName = get_named_parameter(event, 'featureRequestName')
featureRequestDescription = get_named_parameter(event, 'featureRequestDescription')
customerName = get_named_parameter(event, 'customerName')
print (event)
# TODO: implement creating featureRequest
featureRequestID = str(random.randint(1, 99999))
item = {
'featureRequestID': {"S": featureRequestID},
'featureRequestName': {"S": featureRequestName},
'featureRequestDescription': {"S": featureRequestDescription},
'customerName': {"S": customerName}
}
dynamodb.put_item(
TableName=tableName,
Item=item
)
response = sns.publish(
TopicArn=topicarn,
Message=f"your feature request {featureRequestName} has been created and assigned to [email protected]",
Subject="Feature Request Successfully Created"
)
print(response['MessageId'])
return {
'body': f"Created request {featureRequestID} in {tableName}"
}
def updateFeatureRequest(event):
print(event)
featureRequestID = str(get_named_parameter(event, 'featureRequestID'))
customerName = get_named_parameter(event, 'customerName')
# TODO: impliment delete from dynamo here?
key = {
'featureRequestID': {"S": featureRequestID},
}
attribute_updates = {
'customerName': {'Value': {'S': customerName}}
}
dynamodb.update_item(
TableName=tableName,
Key=key,
AttributeUpdates=attribute_updates
)
response = sns.publish(
TopicArn=topicarn,
Message=f"your feature request {featureRequestID} has been updated by [email protected]",
Subject="Feature Request Successfully Updated"
)
print(response['MessageId'])
# Return a success message
return {
'body': f"Updated request {featureRequestID} in {tableName}"
}
def lambda_handler(event, context):
result = ''
response_code = 200
action_group = event['actionGroup']
api_path = event['apiPath']
print ("lambda_handler == > api_path: ",api_path)
if api_path == '/createFeatureRequest':
result = createFeatureRequest(event)
elif api_path == '/updateFeatureRequest':
result = updateFeatureRequest(event)
else:
response_code = 404
result = f"Unrecognized api path: {action_group}::{api_path}"
response_body = {
'application/json': {
'body': json.dumps(result)
}
}
print ("Event:", event)
action_response = {
'actionGroup': event['actionGroup'],
'apiPath': event['apiPath'],
# 'httpMethod': event['HTTPMETHOD'],
'httpMethod': event['httpMethod'],
'httpStatusCode': response_code,
'responseBody': response_body
}
api_response = {'messageVersion': '1.0', 'response': action_response}
return api_response