This repository has been archived by the owner on Mar 13, 2021. It is now read-only.
/
implementation_create_http.go
152 lines (137 loc) · 4.74 KB
/
implementation_create_http.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
package function
import (
"log"
"time"
"github.com/alessandromr/go-serverless-client/manager/rollback"
"github.com/alessandromr/go-serverless-client/resource/apigateway/integration"
"github.com/alessandromr/go-serverless-client/resource/apigateway/method"
"github.com/alessandromr/go-serverless-client/resource/apigateway/resource"
"github.com/alessandromr/go-serverless-client/resource/apigateway/rest"
"github.com/alessandromr/go-serverless-client/utils"
"github.com/alessandromr/go-serverless-client/utils/auth"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/apigateway"
"github.com/aws/aws-sdk-go/service/lambda"
)
//CreateDependencies create all the dependencies for the HTTPEvent
func (input HTTPCreateFunctionInput) CreateDependencies(lambdaResult *lambda.FunctionConfiguration) (map[string]interface{}, error) {
svc := apigateway.New(auth.Sess)
var err error
//apigateway.CreateRestApi
if !input.HTTPCreateEvent.Existing {
apiInput := &apigateway.CreateRestApiInput{
Name: input.HTTPCreateEvent.ApiName,
}
response, err := svc.CreateRestApi(apiInput)
if err != nil {
return nil, err
}
rollback.ResourcesList = append(
rollback.ResourcesList,
rest.ApiGatewayRestApi{
RestApiId: *response.Id,
ApiName: *input.HTTPCreateEvent.ApiName,
},
)
input.HTTPCreateEvent.ApiId = response.Id
}
time.Sleep(utils.ShortSleep * time.Millisecond)
//Get Root Resource
//apigateway.GetResources
getResourceInput := &apigateway.GetResourcesInput{
RestApiId: input.HTTPCreateEvent.ApiId,
}
getResourceOutput, err := svc.GetResources(getResourceInput)
if err != nil {
return nil, err
}
var rootParent string
for _, v := range getResourceOutput.Items {
if *v.Path == "/" {
rootParent = *v.Id
}
}
time.Sleep(utils.ShortSleep * time.Millisecond)
//apigateway.CreateResource
resourceInput := &apigateway.CreateResourceInput{
PathPart: input.HTTPCreateEvent.Path,
RestApiId: input.HTTPCreateEvent.ApiId,
ParentId: aws.String(rootParent),
}
createResourceOutput, err := svc.CreateResource(resourceInput)
if err != nil {
log.Println(err)
rollback.ExecuteRollback()
return nil, err
}
rollback.ResourcesList = append(
rollback.ResourcesList,
resource.ApiGatewayResource{
ResourceId: *createResourceOutput.Id,
RestApiId: *input.HTTPCreateEvent.ApiId,
Path: *input.HTTPCreateEvent.Path,
ParentId: rootParent,
},
)
time.Sleep(utils.ShortSleep * time.Millisecond)
//apigateway.PutMethod
methodInput := &apigateway.PutMethodInput{
HttpMethod: input.HTTPCreateEvent.Method,
RestApiId: input.HTTPCreateEvent.ApiId,
ResourceId: createResourceOutput.Id,
AuthorizationType: aws.String("NONE"),
}
_, err = svc.PutMethod(methodInput)
if err != nil {
log.Println(err)
rollback.ExecuteRollback()
return nil, err
}
rollback.ResourcesList = append(
rollback.ResourcesList,
method.ApiGatewayMethod{
HttpMethod: *input.HTTPCreateEvent.Method,
ResourceId: *createResourceOutput.Id,
RestApiId: *input.HTTPCreateEvent.ApiId,
},
)
time.Sleep(utils.ShortSleep * time.Millisecond)
//Put integration between lambda and api gateway method
//apigateway.PutIntegration
integrationInput := &apigateway.PutIntegrationInput{
Type: aws.String("AWS_PROXY"),
Credentials: input.HTTPCreateEvent.ExecutionRole,
HttpMethod: input.HTTPCreateEvent.Method,
RestApiId: input.HTTPCreateEvent.ApiId,
ResourceId: createResourceOutput.Id,
IntegrationHttpMethod: aws.String("POST"),
Uri: aws.String("arn:aws:apigateway:" + auth.Region + ":lambda:path/2015-03-31/functions/" + *lambdaResult.FunctionArn + "/invocations"),
}
_, err = svc.PutIntegration(integrationInput)
if err != nil {
log.Println(err)
rollback.ExecuteRollback()
return nil, err
}
rollback.ResourcesList = append(
rollback.ResourcesList,
integration.ApiGatewayIntegration{
HttpMethod: *input.HTTPCreateEvent.Method,
IntegrationHTTPMethod: "POST",
ResourceId: *createResourceOutput.Id,
RestApiId: *input.HTTPCreateEvent.ApiId,
Uri: "arn:aws:apigateway:" + auth.Region + ":lambda:path/2015-03-31/functions/" + *lambdaResult.FunctionArn + "/invocations",
Credentials: *input.HTTPCreateEvent.ExecutionRole,
Type: "AWS_PROXY",
},
)
out := make(map[string]interface{})
out["RestApiId"] = *input.HTTPCreateEvent.ApiId
out["Method"] = *input.HTTPCreateEvent.Method
out["ResourceId"] = *createResourceOutput.Id
return out, nil
}
//GetFunctionInput return the CreateFunctionInput from the custom input
func (input HTTPCreateFunctionInput) GetFunctionInput() *lambda.CreateFunctionInput {
return input.FunctionInput
}