-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_task_api.py
More file actions
191 lines (163 loc) · 5.15 KB
/
create_task_api.py
File metadata and controls
191 lines (163 loc) · 5.15 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import boto3
import sys
import json
# Initialize the boto3 client for API Gateway
client = boto3.client('apigateway', region_name='us-east-1')
# Check if TaskManagerAPI already exists
response = client.get_rest_apis()
apis = response.get('items', [])
for api in apis:
if api.get('name') == 'TaskManagerAPI':
print('API already exists')
sys.exit(0)
# Create the Task Manager API
response = client.create_rest_api(
name='TaskManagerAPI',
description='API to manage tasks.',
endpointConfiguration={
'types': [
'REGIONAL',
]
}
)
api_id = response["id"]
# Get the root resource ID
resources = client.get_resources(restApiId=api_id)
root_id = [resource for resource in resources["items"] if resource["path"] == "/"][0]["id"]
# Create resources for tasks, create, delete, and auth
tasks = client.create_resource(
restApiId=api_id,
parentId=root_id,
pathPart='tasks'
)
tasks_resource_id = tasks["id"]
create_task = client.create_resource(
restApiId=api_id,
parentId=root_id,
pathPart='create'
)
create_task_resource_id = create_task["id"]
delete_task = client.create_resource(
restApiId=api_id,
parentId=root_id,
pathPart='delete'
)
delete_task_resource_id = delete_task["id"]
auth = client.create_resource(
restApiId=api_id,
parentId=root_id,
pathPart='auth'
)
auth_resource_id = auth["id"]
# Lambda client
lambda_client = boto3.client('lambda', region_name='us-east-1')
# IAM client for LabRole ARN
iam_client = boto3.client('iam')
lab_role = iam_client.get_role(RoleName='LabRole')['Role']['Arn']
# ----- Set up integrations for tasks -----
view_task_lambda_arn = lambda_client.get_function(FunctionName='viewTaskFunction')['Configuration']['FunctionArn']
view_task_uri = f'arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/{view_task_lambda_arn}/invocations'
client.put_method(
restApiId=api_id,
resourceId=tasks_resource_id,
httpMethod='GET',
authorizationType='NONE'
)
client.put_integration(
restApiId=api_id,
resourceId=tasks_resource_id,
httpMethod='GET',
credentials=lab_role,
integrationHttpMethod='POST',
type='AWS_PROXY',
uri=view_task_uri
)
# ----- Set up integration for creating tasks -----
add_task_lambda_arn = lambda_client.get_function(FunctionName='addTaskFunction')['Configuration']['FunctionArn']
add_task_uri = f'arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/{add_task_lambda_arn}/invocations'
client.put_method(
restApiId=api_id,
resourceId=create_task_resource_id,
httpMethod='POST',
authorizationType='NONE'
)
client.put_integration(
restApiId=api_id,
resourceId=create_task_resource_id,
httpMethod='POST',
credentials=lab_role,
integrationHttpMethod='POST',
type='AWS_PROXY',
uri=add_task_uri
)
# ----- Set up integration for deleting tasks -----
delete_task_lambda_arn = lambda_client.get_function(FunctionName='deleteTaskFunction')['Configuration']['FunctionArn']
delete_task_uri = f'arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/{delete_task_lambda_arn}/invocations'
client.put_method(
restApiId=api_id,
resourceId=delete_task_resource_id,
httpMethod='DELETE',
authorizationType='NONE'
)
client.put_integration(
restApiId=api_id,
resourceId=delete_task_resource_id,
httpMethod='DELETE',
credentials=lab_role,
integrationHttpMethod='POST',
type='AWS_PROXY',
uri=delete_task_uri
)
# ----- Set up integration for auth -----
auth_function_arn = lambda_client.get_function(FunctionName='authFunction')['Configuration']['FunctionArn']
auth_uri = f'arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/{auth_function_arn}/invocations'
client.put_method(
restApiId=api_id,
resourceId=auth_resource_id,
httpMethod='POST',
authorizationType='NONE'
)
client.put_integration(
restApiId=api_id,
resourceId=auth_resource_id,
httpMethod='POST',
credentials=lab_role,
integrationHttpMethod='POST',
type='AWS_PROXY',
uri=auth_uri
)
# ----- Set up OPTIONS methods for CORS -----
resources = {
'GET': tasks_resource_id,
'POST': create_task_resource_id,
'DELETE': delete_task_resource_id,
'AUTH': auth_resource_id
}
for method, resource_id in resources.items():
client.put_method(
restApiId=api_id,
resourceId=resource_id,
httpMethod='OPTIONS',
authorizationType='NONE'
)
client.put_integration(
restApiId=api_id,
resourceId=resource_id,
httpMethod='OPTIONS',
type='MOCK',
requestTemplates={
'application/json': '{"statusCode": 200}'
}
)
client.put_integration_response(
restApiId=api_id,
resourceId=resource_id,
httpMethod='OPTIONS',
statusCode='200',
responseParameters={
'method.response.header.Access-Control-Allow-Headers': '\'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token\'',
'method.response.header.Access-Control-Allow-Methods': '\'POST\',\'OPTIONS\',\'DELETE\',\'GET\'',
'method.response.header.Access-Control-Allow-Origin': '\'*\''
}
)
print("DONE")