-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda_function.py
133 lines (116 loc) · 4.21 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
from kafka.admin import KafkaAdminClient, NewTopic, ConfigResource, ConfigResourceType
import kafka
import boto3
import os
import json
def lambda_handler(event, context):
try:
whichfunction = event["queryStringParameters"]["whichfunction"]
bsrv = event["queryStringParameters"]["bsrv"]
a=""
if whichfunction=='listtopics':
a=getTopicList(bsrv)
elif whichfunction=='gettopicconfig':
topicname=event["queryStringParameters"]["topicname"]
a=getTopicConfig(topicname,bsrv)
elif whichfunction=='createtopic':
topicname=event["queryStringParameters"]["topicname"]
retention=event["queryStringParameters"]["retention"]
localretention=event["queryStringParameters"]["localretention"]
partition=event["queryStringParameters"]["partition"]
rf=event["queryStringParameters"]["rf"]
a=createTopic(bsrv,topicname,retention,localretention,partition,rf)
elif whichfunction=='describetopic':
topicname=event["queryStringParameters"]["topicname"]
a=describeTopic(topicname,bsrv)
return {
'statusCode': 200,
'body': json.dumps(a)
}
except Exception as e:
# Return a 400 error to the client
return {
"statusCode": 400,
"body": json.dumps({
"error": str(e)
})
}
def getTopicConfig(topicname,bootstrapservers):
try:
admin_client = KafkaAdminClient(bootstrap_servers=bootstrapservers, client_id='test')
topicconfig = admin_client.describe_configs(config_resources=[ConfigResource(ConfigResourceType.TOPIC, topicname)])
config_list = str(topicconfig)
return {
'statusCode': 200,
'body': json.dumps(config_list)
}
except Exception as e:
return {
"statusCode": 400,
"body": json.dumps({
"error": str(e)
})
}
def describeTopic(topicname,bootstrapservers):
try:
admin_client = KafkaAdminClient(bootstrap_servers=bootstrapservers, client_id='test')
topicconfig = admin_client.describe_topics([topicname])
return {
'statusCode': 200,
'body': topicconfig
}
except Exception as e:
return {
"statusCode": 400,
"body": json.dumps({
"error": str(e)
})
}
def getTopicList(bootstrapservers):
try:
admin_client = KafkaAdminClient(
bootstrap_servers=bootstrapservers,
client_id='test'
)
topic_list = []
topiclist=admin_client.list_topics()
return {
'statusCode': 200,
'body': json.dumps(topiclist)
}
except Exception as e:
return {
"statusCode": 400,
"body": json.dumps({
"error": str(e)
})
}
def createTopic(bootstrapservers,topicname,retention,localretention,partition,rf):
try:
admin_client = KafkaAdminClient(bootstrap_servers=bootstrapservers, client_id='test')
if int(localretention) < int(retention):
#Tiered storage enabled topic
topic_configs={'retention.ms': int(retention),'remote.storage.enable':'true','local.retention.ms':int(localretention)}
else:
topic_configs={'retention.ms': int(retention)}
topic_list = [
NewTopic(
name=topicname,
num_partitions=int(partition),
replication_factor=int(rf),
#topic_configs={'retention.ms': int(retention),'remote.storage.enable':'true','local.retention.ms':int(localretention)}
topic_configs=topic_configs
)
]
admin_client.create_topics(new_topics=topic_list, validate_only=False)
return {
'statusCode': 200,
'body': json.dumps("Topic :" + topicname + " created successfully")
}
except Exception as e:
return {
"statusCode": 400,
"body": json.dumps({
"error": str(e)
})
}