-
Notifications
You must be signed in to change notification settings - Fork 33
/
contract.go
425 lines (379 loc) · 11.6 KB
/
contract.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
package apigateway
import (
"errors"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/apigateway"
"github.com/aws/aws-sdk-go/service/lambda"
"strings"
)
// GetRestAPIInput endpoint URL inpit
type GetRestAPIInput struct {
Name *string `json:",inline"`
StageName *string ` json:",inline"`
}
func (i *GetRestAPIInput) Init() error {
if i.StageName == nil {
i.StageName = aws.String("e2e")
}
return nil
}
func (i *GetRestAPIInput) Validate() error {
if i.Name == nil {
return errors.New("name was empty")
}
return nil
}
// GetRestAPIInput endpoint URL output
type GetRestAPIOutput struct {
*apigateway.RestApi
EndpointURL string ` json:",inline"`
}
// SetupRestAPIInput represent a request to setup API with specified resources
type SetupRestAPIInput struct {
apigateway.CreateRestApiInput ` json:",inline"`
Resources []*SetupResourceInput
apigateway.CreateDeploymentInput ` json:",inline"`
Authorizers []*CreateAuthorizerInput
GatewayResponse []*apigateway.PutGatewayResponseInput
Redeploy bool
}
type CreateAuthorizerInput struct {
apigateway.CreateAuthorizerInput
FunctionName string
}
// Diff computes patches
func (i CreateAuthorizerInput) Diff(source *apigateway.Authorizer) []*apigateway.PatchOperation {
var result = make([]*apigateway.PatchOperation, 0)
if patch, ok := patchString(source.AuthorizerUri, i.AuthorizerUri, "/authorizerUri"); ok {
result = append(result, patch)
}
if patch, ok := patchString(source.AuthType, i.AuthType, "/authType"); ok {
result = append(result, patch)
}
if patch, ok := patchString(source.Type, i.Type, "/authorizerType"); ok {
result = append(result, patch)
}
if patch, ok := patchString(source.Name, i.Name, "/name"); ok {
result = append(result, patch)
}
if patch, ok := patchString(source.AuthorizerCredentials, i.AuthorizerCredentials, "/authorizerCredentials"); ok {
result = append(result, patch)
}
if patch, ok := patchString(source.IdentitySource, i.IdentitySource, "/identitySource"); ok {
result = append(result, patch)
}
if patch, ok := patchString(source.IdentityValidationExpression, i.IdentityValidationExpression, "/identityValidationExpression"); ok {
result = append(result, patch)
}
if patch, ok := pathInt64(source.AuthorizerResultTtlInSeconds, i.AuthorizerResultTtlInSeconds, "/authorizerResultTtlInSeconds"); ok {
result = append(result, patch)
}
return result
}
// SetupRestAPIInput represent setup API response
type SetupRestAPIOutput struct {
*apigateway.RestApi
Resources []*SetupResourceOutput
Authorizers []*apigateway.Authorizer
Stage *apigateway.Stage
EndpointURL string
Region string
}
// SetupResourceInput represents resource input
type SetupResourceInput struct {
Path string
apigateway.CreateResourceInput `json:",inline"`
Methods []*ResourceMethod
}
// ResourceMethod represents resource method
type ResourceMethod struct {
FunctionName string
Authorizer string
HttpMethod string
*apigateway.PutMethodInput
*apigateway.PutIntegrationInput
*lambda.AddPermissionInput
}
// SetupResourceOutput represents setup resource output
type SetupResourceOutput struct {
*apigateway.Resource `json:",inline"`
ResourceMethods map[string]*apigateway.Method
}
// RemoveRestAPI removes API for supplied name
type RemoveRestAPIInput struct {
Name *string
}
func (i *RemoveRestAPIInput) Validate() error {
if i.Name == nil {
return errors.New("name was empty")
}
return nil
}
func (i *SetupRestAPIInput) Init() error {
if len(i.Resources) == 0 {
return nil
}
var URIs = make(map[string]bool)
var resources = make([]*SetupResourceInput, 0)
if len(i.Authorizers) > 0 {
for _, auth := range i.Authorizers {
_ = auth.Init()
}
}
for _, resource := range i.Resources {
if err := resource.Init(); err != nil {
return err
}
URIs[resource.Path] = true
parts := strings.Split(resource.Path, "/")
for i := 1; i < len(parts)-1; i++ {
ancestor := strings.Join(parts[0:i+1], "/")
if URIs[ancestor] {
continue
}
ancestorResource := &SetupResourceInput{Path: ancestor}
_ = ancestorResource.Init()
resources = append(resources, ancestorResource)
}
resources = append(resources, resource)
}
i.Resources = resources
return nil
}
// Validate checks is input is valud
func (i *SetupRestAPIInput) Validate() error {
if len(i.Resources) == 0 {
return fmt.Errorf("resources was empty")
}
for _, resource := range i.Resources {
if err := resource.Validate(); err != nil {
return err
}
}
createDeployment := &i.CreateDeploymentInput
if createDeployment.StageName == nil {
createDeployment.StageName = aws.String("e2e")
}
return nil
}
// Init initialize input
func (i *SetupResourceInput) Init() error {
if i.Path == "" {
if i.PathPart != nil {
i.Path = "/" + *i.PathPart
} else {
i.Path = "/"
}
}
if i.PathPart == nil {
parts := strings.Split(i.Path, "/")
if len(parts) > 1 {
i.PathPart = &parts[len(parts)-1]
}
}
if len(i.Methods) == 0 {
return nil
}
for _, method := range i.Methods {
if err := method.Init(); err != nil {
return err
}
}
return nil
}
func (i *SetupResourceInput) Validate() error {
resourceInput := i.CreateResourceInput
if resourceInput.PathPart == nil {
return fmt.Errorf("pathPart was empty")
}
if len(i.Methods) > 0 {
for _, method := range i.Methods {
if err := method.Validate(); err != nil {
return err
}
}
}
return nil
}
// ParentPath returns parent path
func (i *SetupResourceInput) ParentPath() string {
if i.PathPart == nil {
return i.Path
}
index := strings.LastIndex(i.Path, *i.PathPart)
if index == -1 {
return i.Path
}
result := string(i.Path[:index])
if result == "/" {
return result
}
if strings.HasSuffix(result, "/") {
return string(result[:len(result)-1])
}
return result
}
/*
Initialise resource with the following defaults
<ul>
<li>AuthorizationType: NONE</li>
<li>HttpMethod: ANY</li>
<li>ApiKeyRequired: false</li>
<li>IntegrationHttpMethod: POST</li>
<li>Type: AWS_PROXY</li>
<li>Uri: arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/${function.arn}/invocations</li>
</ul>
It supports data substitution on uri attribute i.e.
- $function.arn
- $function.region
- $function.accountID
- $restAPI.SessionID
- $uuid.next
*/
func (i *ResourceMethod) Init() error {
methodInput := i.PutMethodInput
if methodInput == nil {
i.PutMethodInput = &apigateway.PutMethodInput{}
methodInput = i.PutMethodInput
}
if methodInput.AuthorizationType == nil {
methodInput.AuthorizationType = aws.String("NONE")
}
if methodInput.ApiKeyRequired == nil {
methodInput.ApiKeyRequired = aws.Bool(false)
}
integrationInput := i.PutIntegrationInput
if integrationInput == nil {
i.PutIntegrationInput = &apigateway.PutIntegrationInput{}
integrationInput = i.PutIntegrationInput
}
if i.HttpMethod != "" {
methodInput.HttpMethod = &i.HttpMethod
integrationInput.HttpMethod = &i.HttpMethod
}
if integrationInput.HttpMethod == nil {
integrationInput.HttpMethod = methodInput.HttpMethod
}
if integrationInput.IntegrationHttpMethod == nil {
integrationInput.IntegrationHttpMethod = aws.String("POST")
}
if i.FunctionName != "" {
if integrationInput.Type == nil {
integrationInput.Type = aws.String("AWS_PROXY")
}
if integrationInput.Uri == nil {
integrationInput.Uri = aws.String("arn:aws:apigateway:${function.region}:lambda:path/2015-03-31/functions/${function.arn}/invocations")
}
permissionInput := i.AddPermissionInput
if i.AddPermissionInput == nil {
i.AddPermissionInput = &lambda.AddPermissionInput{}
permissionInput = i.AddPermissionInput
}
if permissionInput.FunctionName == nil {
permissionInput.FunctionName = &i.FunctionName
}
if permissionInput.Action == nil {
permissionInput.Action = aws.String("lambda:InvokeFunction")
}
if permissionInput.Principal == nil {
permissionInput.Principal = aws.String("apigateway.amazonaws.com")
}
if permissionInput.StatementId == nil {
permissionInput.StatementId = aws.String("${uuid.next}")
}
if permissionInput.SourceArn == nil {
permissionInput.SourceArn = aws.String("arn:aws:execute-api:${function.region}:${function.accountID}:${restAPI.SessionID}/*/*/*")
}
}
return nil
}
func (i *CreateAuthorizerInput) Init() error {
if i.FunctionName != "" {
i.AuthorizerUri = aws.String("arn:aws:apigateway:${authorizer.region}:lambda:path/2015-03-31/functions/${authorizer.arn}/invocations")
}
if i.AuthType == nil {
i.AuthType = aws.String("custom")
}
if i.AuthorizerResultTtlInSeconds == nil {
i.AuthorizerResultTtlInSeconds = aws.Int64(0)
}
return nil
}
func (i *ResourceMethod) Validate() error {
if i.HttpMethod == "" {
return fmt.Errorf("httpMethod was empty")
}
if i.Type == nil {
return fmt.Errorf("type was empty")
}
return nil
}
// SetupMethodInput setups method input
type SetupMethodInput apigateway.PutMethodInput
// Diff computes patches
func (i SetupMethodInput) Diff(source *apigateway.Method) []*apigateway.PatchOperation {
var result = make([]*apigateway.PatchOperation, 0)
if patch, ok := patchBool(source.ApiKeyRequired, i.ApiKeyRequired, "/apiKeyRequired"); ok {
result = append(result, patch)
}
if patch, ok := patchString(source.AuthorizationType, i.AuthorizationType, "/authorizationType"); ok {
result = append(result, patch)
}
if patch, ok := patchString(source.AuthorizerId, i.AuthorizerId, "/authorizerId"); ok {
result = append(result, patch)
}
if patch, ok := patchString(source.OperationName, i.OperationName, "/operationName"); ok {
result = append(result, patch)
}
if patch, ok := patchString(source.RequestValidatorId, i.RequestValidatorId, "/requestValidatorId"); ok {
result = append(result, patch)
}
return result
}
type PutIntegrationInput apigateway.PutIntegrationInput
// Diff computes patches
func (i PutIntegrationInput) Diff(source *apigateway.Integration) []*apigateway.PatchOperation {
var result = make([]*apigateway.PatchOperation, 0)
if patch, ok := patchString(source.Uri, i.Uri, "/uri"); ok {
result = append(result, patch)
}
if patch, ok := patchString(source.Credentials, i.Credentials, "/credentials"); ok {
result = append(result, patch)
}
if patch, ok := patchString(source.Type, i.Type, "/integrationType"); ok {
result = append(result, patch)
}
if patch, ok := patchString(source.ConnectionId, i.ConnectionId, "/connectionId"); ok {
result = append(result, patch)
}
if patch, ok := patchString(source.ConnectionType, i.ConnectionType, "/connectionType"); ok {
result = append(result, patch)
}
if patch, ok := patchString(source.ContentHandling, i.ContentHandling, "/contentHandling"); ok {
result = append(result, patch)
}
if i.PassthroughBehavior != nil {
if patch, ok := patchString(source.PassthroughBehavior, i.PassthroughBehavior, "/passthroughBehavior"); ok {
result = append(result, patch)
}
}
return result
}
type SetupDeploymentInput apigateway.CreateDeploymentInput
func (i *SetupDeploymentInput) Diff(source *apigateway.CreateDeploymentInput) []*apigateway.PatchOperation {
var result = make([]*apigateway.PatchOperation, 0)
if patch, ok := patchString(source.StageName, i.StageName, "/stageName"); ok {
result = append(result, patch)
}
if patch, ok := patchString(source.Description, i.Description, "/description"); ok {
result = append(result, patch)
}
if patch, ok := patchString(source.StageDescription, i.StageDescription, "/stageDescription"); ok {
result = append(result, patch)
}
if patch, ok := patchBool(source.CacheClusterEnabled, i.CacheClusterEnabled, "/cacheClusterEnabled"); ok {
result = append(result, patch)
}
return result
}