-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy patheventSource.js
More file actions
202 lines (182 loc) · 6.22 KB
/
Copy patheventSource.js
File metadata and controls
202 lines (182 loc) · 6.22 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
192
193
194
195
196
197
198
199
200
201
202
// createEventSourceMapping
const { Logger } = require('@serverless-devs/core');
const uuid = require('uuid');
class EventSource {
constructor (aws) {
const lambda = aws.lambda();
const apigatewayv2 = aws.apigatewayv2();
this.lambda = async (functionKey, inputs) => {
return await new Promise((resolve, reject) => {
lambda[functionKey](inputs, (e, data) => {
if (e) {
reject(e);
}
resolve(data);
});
})
}
this.apigatewayv2 = async (functionKey, inputs) => {
return await new Promise((resolve, reject) => {
apigatewayv2[functionKey](inputs, (e, data) => {
if (e) {
reject(e);
}
resolve(data);
});
})
}
}
async handlerApi (apiName, functionArn) {
const { Items } = await this.apigatewayv2('getApis');
for (const item of Items) {
if (item.Name === apiName) {
return item;
}
}
Logger.log(`The API Gateway does not exist '${apiName}',start creating API automatically.`)
const createApiRes = await this.apigatewayv2('createApi', {
Name: apiName,
ProtocolType: 'HTTP',
Description: `S is automatically created.`,
Target: functionArn
});
Logger.log(`Successfully created API.`)
return createApiRes;
}
async handlerIntegration({ ApiId }, properties, functionArn) {
const {
Method = 'post',
PayloadFormatVersion,
TimeoutInMillis,
} = properties;
const { Items } = await this.apigatewayv2('getIntegrations', { ApiId });
for (const item of Items) {
if (item.IntegrationUri === functionArn) {
return await this.apigatewayv2('updateIntegration', {
ApiId,
IntegrationId: item.IntegrationId,
IntegrationMethod: Method,
PayloadFormatVersion,
TimeoutInMillis
})
}
}
Logger.log(`There is no '${functionArn}' in the Integration, start creating Integration automatically.`)
const createRes = await this.apigatewayv2('createIntegration', {
ApiId,
ConnectionType: 'INTERNET',
Description: 'S is automatically created.',
IntegrationMethod: Method,
IntegrationType: 'AWS_PROXY',
IntegrationUri: functionArn,
PayloadFormatVersion: PayloadFormatVersion || '2.0',
TimeoutInMillis: TimeoutInMillis || 30000,
});
Logger.log(`Successfully created Integration.`)
return createRes;
}
async handlerRoute({ ApiId }, properties, integrationId) {
const { Items } = await this.apigatewayv2('getRoutes', { ApiId });
const routeKey = `${properties.Method.toLocaleUpperCase()} ${properties.Path}`;
for (const item of Items) {
if (item.RouteKey === routeKey) {
return item;
}
}
Logger.log(`There is no '${routeKey}' in the Routes, start creating Route automatically.`)
const createRouteRes = await this.apigatewayv2('createRoute', {
ApiId,
RouteKey: routeKey,
AuthorizationType: 'NONE',
Target: `integrations/${integrationId}`
});
Logger.log(`Successfully created Route.`)
return createRouteRes;
}
async addApiTrigger (functionName, eventName, functionConfig, properties) {
const functionArn = functionConfig.FunctionArn;
const apiRes = await this.handlerApi(eventName, functionArn);
const integrationRes = await this.handlerIntegration(apiRes, properties, functionArn);
await this.handlerRoute(apiRes, properties, integrationRes.IntegrationId)
const arns = functionArn.split(':');
try {
await this.lambda('removePermission', {
FunctionName: functionName,
StatementId: this.getUUID(functionName)
});
} catch (e) {}
await this.lambda('addPermission', {
FunctionName: functionName,
StatementId: this.getUUID(functionName),
Action: 'lambda:InvokeFunction',
Principal: 'apigateway.amazonaws.com',
SourceArn: `arn:aws:execute-api:${arns[3]}:${arns[4]}:${apiRes.ApiId}/*/*${properties.Path}`
});
return {
ApiEndpoint: apiRes.ApiEndpoint,
Uri: `${apiRes.ApiEndpoint}${properties.Path}`
}
}
getUUID (functionName) {
return uuid.v5(functionName, '8bbea2bd-3bcf-5055-932f-5b38be2464b1')
}
async deploy (events, functionName, functionConfig) {
if (!(events && functionName)) {
return;
}
const resConfig = {};
for (const eventName in events) {
if (events[eventName].Type !== 'Api') {
const mes = `${events[eventName].Type} is not supported, so skip this configuration`;
Logger.log(mes, 'yellow');
resConfig[eventName] = { Message: mes };
continue;
}
Logger.log(`Start deploying ${eventName}.`);
const properties = events[eventName].Properties;
resConfig[eventName] = await this.addApiTrigger(functionName, eventName, functionConfig, properties);
Logger.log(`Successfully deploy ${eventName}.`);
}
return resConfig;
}
async removeApiTrigger (apiName, functionName) {
const { Items } = await this.apigatewayv2('getApis');
for (const item of Items) {
if (item.Name === apiName) {
try {
Logger.log(`Start remove API: ${apiName}`);
await this.apigatewayv2('deleteApi', { ApiId: item.ApiId });
Logger.log(`Successfully remove API.`)
} catch (e) {
Logger.log(`Delete Failed: ${e.message}`, 'red')
}
break;
}
}
Logger.log(`Start remove permission.`);
await this.lambda('removePermission', {
FunctionName: functionName,
StatementId: this.getUUID(functionName)
});
const mes = `Successfully remove Amazon API Gateway Trigger.`;
Logger.log(mes);
return { Message: mes };
}
async remove (events, functionName) {
if (!(events && functionName)) {
return;
}
const resConfig = {};
for (const eventName in events) {
if (events[eventName].Type !== 'Api') {
const mes = `${events[eventName].Type} is not supported, so skip this configuration`;
Logger.log(mes, 'yellow');
resConfig[eventName] = { Message: mes };
continue;
}
resConfig[eventName] = await this.removeApiTrigger(eventName, functionName);
}
return resConfig;
}
}
module.exports = EventSource