-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsubCreateTopic.py
79 lines (73 loc) · 2.26 KB
/
subCreateTopic.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
import boto3
import json
def create_SNS_topic(name):
try:
client = boto3.client("sns")
result = client.create_topic(Name = name)
return result["TopicArn"]
except Exception as e:
print("Error occured: ", e)
return None
def subscribe_user(topic_ARN, protocol, endpoint):
try:
client = boto3.client("sns")
result = client.subscribe(TopicArn = topic_ARN, Protocol = protocol, Endpoint = endpoint)
return True
except Exception as e:
print("Error occured: ", e)
return False
def check_topic(topic_name):
try:
client = boto3.client("dynamodb")
result = client.get_item(
TableName = "topics",
Key = {"topicName" : {"S" : topic_name}},
ProjectionExpression = "ARN"
)
topic_DNE = result["Items"][0]["ARN"]["NULL"]
if topic_DNE:
# DNE = Does Not Exist
return "DNE"
else:
return result["Items"][0]["ARN"]["S"]
except Exception as e:
print("Error occured: ", e)
return "DNE"
def add_topic(topic_name):
try:
topic_ARN = create_SNS_topic(topic_name)
client = boto3.client("dynamodb")
result = client.put_item(
TableName="topics",
Item = {
"topicName" : {"S" : topic_name},
"ARN" : {"S" : topic_ARN}
}
)
return topic_ARN
except Exception as e:
print("Error occured: ", e)
return False
#def update_courses_DB(course_id):
# try:
# client = boto3.client("dynamodb")
# result = client.put_item(
# TableName = "Courses",
# Key = {"courseId" : {"S" : course_id}},
# UpdateExpression =
# )
# except Exception as e:
# print("Error occured: ", e)
# return False
def lambda_handler(event, context):
# TODO implement
topic_name = event["topic_name"]
protocol = event["protocol"]
endpoint = event["endpoint"]
topic_ARN = check_topic(topic_name)
to_return = False
if topic_ARN == "DNE":
topic_ARN = add_topic(topic_name)
to_return = subscribe_user(topic_ARN, protocol, endpoint)
# update_courses_DB(topic_name)
return to_return