-
Notifications
You must be signed in to change notification settings - Fork 607
/
Copy pathapigateway.go
373 lines (314 loc) · 10.9 KB
/
apigateway.go
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
Copyright 2020 Cortex Labs, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package aws
import (
"fmt"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/apigatewayv2"
"github.com/cortexlabs/cortex/pkg/lib/errors"
)
// CreateAPIGateway Creates a new API Gateway with the default stage
func (c *Client) CreateAPIGateway(name string, tags map[string]string) (string, error) {
createAPIResponse, err := c.APIGatewayV2().CreateApi(&apigatewayv2.CreateApiInput{
Name: aws.String(name),
ProtocolType: aws.String(apigatewayv2.ProtocolTypeHttp),
Tags: aws.StringMap(tags),
})
if err != nil {
return "", errors.Wrap(err, "failed to create api gateway")
}
if createAPIResponse.ApiId == nil {
return "", errors.ErrorUnexpected("failed to create api gateway")
}
_, err = c.APIGatewayV2().CreateStage(&apigatewayv2.CreateStageInput{
ApiId: createAPIResponse.ApiId,
AutoDeploy: aws.Bool(true),
StageName: aws.String("$default"),
Tags: aws.StringMap(tags),
})
if err != nil {
c.DeleteAPIGateway(*createAPIResponse.ApiId) // best effort cleanup
return "", errors.Wrap(err, "failed to create $default api gateway stage")
}
return *createAPIResponse.ApiId, nil
}
// GetVPCLinkByTag Gets a VPC Link by tag (returns nil if there are no matches)
func (c *Client) GetVPCLinkByTag(tagName string, tagValue string) (*apigatewayv2.VpcLink, error) {
var nextToken *string
for {
vpcLinks, err := c.APIGatewayV2().GetVpcLinks(&apigatewayv2.GetVpcLinksInput{
NextToken: nextToken,
})
if err != nil {
return nil, errors.Wrap(err, "failed to get vpc links")
}
for _, vpcLink := range vpcLinks.Items {
for tag, value := range vpcLink.Tags {
if tag == tagName && *value == tagValue {
return vpcLink, nil
}
}
}
nextToken = vpcLinks.NextToken
if nextToken == nil {
break
}
}
return nil, nil
}
// GetAPIGatewayByTag Gets an API Gateway by tag (returns nil if there are no matches)
func (c *Client) GetAPIGatewayByTag(tagName string, tagValue string) (*apigatewayv2.Api, error) {
var nextToken *string
for {
apis, err := c.APIGatewayV2().GetApis(&apigatewayv2.GetApisInput{
NextToken: nextToken,
})
if err != nil {
return nil, errors.Wrap(err, "failed to get api gateways")
}
for _, api := range apis.Items {
for tag, value := range api.Tags {
if tag == tagName && *value == tagValue {
return api, nil
}
}
}
nextToken = apis.NextToken
if nextToken == nil {
break
}
}
return nil, nil
}
// DeleteVPCLinkByTag Deletes a VPC Link by tag (returns the deleted VPC Link, or nil if it was not found )
func (c *Client) DeleteVPCLinkByTag(tagName string, tagValue string) (*apigatewayv2.VpcLink, error) {
vpcLink, err := c.GetVPCLinkByTag(tagName, tagValue)
if err != nil {
return nil, err
} else if vpcLink == nil {
return nil, nil
}
_, err = c.APIGatewayV2().DeleteVpcLink(&apigatewayv2.DeleteVpcLinkInput{
VpcLinkId: vpcLink.VpcLinkId,
})
if err != nil {
return nil, errors.Wrap(err, "failed to delete vpc link "+*vpcLink.VpcLinkId)
}
return vpcLink, nil
}
// DeleteAPIGatewayByTag Deletes an API Gateway by tag (returns whether or not the API Gateway existed)
func (c *Client) DeleteAPIGatewayByTag(tagName string, tagValue string) (*apigatewayv2.Api, error) {
apiGateway, err := c.GetAPIGatewayByTag(tagName, tagValue)
if err != nil {
return nil, err
} else if apiGateway == nil {
return nil, nil
}
err = c.DeleteAPIGateway(*apiGateway.ApiId)
if err != nil {
return nil, err
}
return apiGateway, nil
}
// DeleteAPIGateway Deletes an API Gateway by ID (returns an error if the API Gateway does not exist)
func (c *Client) DeleteAPIGateway(apiGatewayID string) error {
// Delete mappings in case user added a custom domain name (otherwise this will block API Gateway deletion)
err := c.DeleteAPIGatewayMappings(apiGatewayID)
if err != nil {
return err
}
_, err = c.APIGatewayV2().DeleteApi(&apigatewayv2.DeleteApiInput{
ApiId: aws.String(apiGatewayID),
})
if err != nil {
return errors.Wrap(err, "failed to delete api gateway "+apiGatewayID)
}
return nil
}
// DeleteAPIGatewayMappingsForDomainName deletes all API mappings that point to the provided api gateway from the provided domain name
func (c *Client) DeleteAPIGatewayMappingsForDomainName(apiGatewayID string, domainName string) error {
var nextToken *string
for {
apiMappings, err := c.APIGatewayV2().GetApiMappings(&apigatewayv2.GetApiMappingsInput{
DomainName: aws.String(domainName),
NextToken: nextToken,
})
if err != nil {
return errors.Wrap(err, "failed to get api mappings")
}
for _, apiMapping := range apiMappings.Items {
if *apiMapping.ApiId != apiGatewayID {
continue
}
_, err := c.APIGatewayV2().DeleteApiMapping(&apigatewayv2.DeleteApiMappingInput{
DomainName: aws.String(domainName),
ApiMappingId: apiMapping.ApiMappingId,
})
if err != nil {
return errors.Wrap(err, fmt.Sprintf("failed to delete api mapping %s in domain %s", *apiMapping.ApiMappingId, domainName))
}
}
nextToken = apiMappings.NextToken
if nextToken == nil {
break
}
}
return nil
}
// DeleteAPIGatewayMappings deletes all API mappings that point to the provided api gateway
func (c *Client) DeleteAPIGatewayMappings(apiGatewayID string) error {
var nextToken *string
for {
domainNames, err := c.APIGatewayV2().GetDomainNames(&apigatewayv2.GetDomainNamesInput{
NextToken: nextToken,
})
if err != nil {
return errors.Wrap(err, "failed to get domain names")
}
for _, domainName := range domainNames.Items {
err := c.DeleteAPIGatewayMappingsForDomainName(apiGatewayID, *domainName.DomainName)
if err != nil {
return err
}
}
nextToken = domainNames.NextToken
if nextToken == nil {
break
}
}
return nil
}
// GetVPCLinkIntegration gets the VPC Link integration in an API Gateway, or nil if unable to find it
func (c *Client) GetVPCLinkIntegration(apiGatewayID string, vpcLinkID string) (*apigatewayv2.Integration, error) {
var nextToken *string
for {
integrations, err := c.APIGatewayV2().GetIntegrations(&apigatewayv2.GetIntegrationsInput{
ApiId: &apiGatewayID,
NextToken: nextToken,
})
if err != nil {
return nil, errors.Wrap(err, "failed to get api gateway integrations for api gateway "+apiGatewayID)
}
// find integration which is connected to the VPC link
for _, integration := range integrations.Items {
if *integration.ConnectionId == vpcLinkID {
return integration, nil
}
}
nextToken = integrations.NextToken
if nextToken == nil {
break
}
}
return nil, nil
}
// GetRouteIntegrationID returns the integration which is attached to a endpoint route, or empty string if unable to find it
func (c *Client) GetRouteIntegrationID(apiGatewayID string, endpoint string) (string, error) {
route, err := c.GetRoute(apiGatewayID, endpoint)
if err != nil {
return "", err
}
if route == nil {
return "", nil
}
return ExtractRouteIntegrationID(route), nil
}
// ExtractRouteIntegrationID extracts the integration ID which is attached to a route, or "" if no route is attached
func ExtractRouteIntegrationID(route *apigatewayv2.Route) string {
if route == nil || route.Target == nil {
return ""
}
// trim of prefix of integrationID.
// Note: Integrations get attached to routes via a target of the format integrations/<integrationID>
integrationID := strings.TrimPrefix(*route.Target, "integrations/")
return integrationID
}
// GetRoute retrieves the route matching an endpoint, or nil if unable to find it
func (c *Client) GetRoute(apiGatewayID string, endpoint string) (*apigatewayv2.Route, error) {
var nextToken *string
for {
routes, err := c.APIGatewayV2().GetRoutes(&apigatewayv2.GetRoutesInput{
ApiId: &apiGatewayID,
NextToken: nextToken,
})
if err != nil {
return nil, errors.Wrap(err, "failed to get api gateway routes for api gateway "+apiGatewayID)
}
// find route which matches the endpoint
for _, route := range routes.Items {
if *route.RouteKey == "ANY "+endpoint {
return route, nil
}
}
nextToken = routes.NextToken
if nextToken == nil {
break
}
}
return nil, nil
}
// CreateRoute creates a new route and attaches the route to the integration
func (c *Client) CreateRoute(apiGatewayID string, integrationID string, endpoint string) error {
_, err := c.APIGatewayV2().CreateRoute(&apigatewayv2.CreateRouteInput{
ApiId: &apiGatewayID,
RouteKey: aws.String("ANY " + endpoint),
Target: aws.String("integrations/" + integrationID),
})
if err != nil {
return errors.Wrap(err, fmt.Sprintf("failed to create %s route for api gateway %s with integration %s", endpoint, apiGatewayID, integrationID))
}
return nil
}
// CreateHTTPIntegration creates new HTTP integration for API Gateway, returns integration ID
func (c *Client) CreateHTTPIntegration(apiGatewayID string, targetEndpoint string) (string, error) {
integrationResponse, err := c.APIGatewayV2().CreateIntegration(&apigatewayv2.CreateIntegrationInput{
ApiId: &apiGatewayID,
IntegrationType: aws.String("HTTP_PROXY"),
IntegrationUri: &targetEndpoint,
PayloadFormatVersion: aws.String("1.0"),
IntegrationMethod: aws.String("ANY"),
})
if err != nil {
return "", errors.Wrap(err, fmt.Sprintf("failed to create api gateway integration for endpoint %s in api gateway %s", targetEndpoint, apiGatewayID))
}
return *integrationResponse.IntegrationId, nil
}
// DeleteIntegration deletes an integration from API Gateway
func (c *Client) DeleteIntegration(apiGatewayID string, integrationID string) error {
_, err := c.APIGatewayV2().DeleteIntegration(&apigatewayv2.DeleteIntegrationInput{
ApiId: &apiGatewayID,
IntegrationId: &integrationID,
})
if err != nil {
return errors.Wrap(err, fmt.Sprintf("failed to delete api gateway integration %s in api gateway %s", integrationID, apiGatewayID))
}
return nil
}
// DeleteRoute deletes a route from API Gateway, and returns the deleted route (or nil if it wasn't found)
func (c *Client) DeleteRoute(apiGatewayID string, endpoint string) (*apigatewayv2.Route, error) {
route, err := c.GetRoute(apiGatewayID, endpoint)
if err != nil {
return nil, err
} else if route == nil {
return nil, nil
}
_, err = c.APIGatewayV2().DeleteRoute(&apigatewayv2.DeleteRouteInput{
ApiId: &apiGatewayID,
RouteId: route.RouteId,
})
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("failed to delete api gateway route %s with endpoint %s in api gateway %s", *route.RouteId, endpoint, apiGatewayID))
}
return route, nil
}