-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
Copy pathresource_aws_api_gateway_method.go
345 lines (290 loc) · 9.76 KB
/
resource_aws_api_gateway_method.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
package aws
import (
"fmt"
"log"
"strconv"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/apigateway"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
func resourceAwsApiGatewayMethod() *schema.Resource {
return &schema.Resource{
Create: resourceAwsApiGatewayMethodCreate,
Read: resourceAwsApiGatewayMethodRead,
Update: resourceAwsApiGatewayMethodUpdate,
Delete: resourceAwsApiGatewayMethodDelete,
Importer: &schema.ResourceImporter{
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
idParts := strings.Split(d.Id(), "/")
if len(idParts) != 3 || idParts[0] == "" || idParts[1] == "" || idParts[2] == "" {
return nil, fmt.Errorf("Unexpected format of ID (%q), expected REST-API-ID/RESOURCE-ID/HTTP-METHOD", d.Id())
}
restApiID := idParts[0]
resourceID := idParts[1]
httpMethod := idParts[2]
d.Set("http_method", httpMethod)
d.Set("resource_id", resourceID)
d.Set("rest_api_id", restApiID)
d.SetId(fmt.Sprintf("agm-%s-%s-%s", restApiID, resourceID, httpMethod))
return []*schema.ResourceData{d}, nil
},
},
Schema: map[string]*schema.Schema{
"rest_api_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"resource_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"http_method": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateHTTPMethod(),
},
"authorization": {
Type: schema.TypeString,
Required: true,
},
"authorizer_id": {
Type: schema.TypeString,
Optional: true,
},
"authorization_scopes": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
Optional: true,
},
"api_key_required": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"request_models": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"request_parameters": {
Type: schema.TypeMap,
Elem: &schema.Schema{Type: schema.TypeBool},
Optional: true,
},
"request_parameters_in_json": {
Type: schema.TypeString,
Optional: true,
Removed: "Use `request_parameters` argument instead",
},
"request_validator_id": {
Type: schema.TypeString,
Optional: true,
},
},
}
}
func resourceAwsApiGatewayMethodCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigatewayconn
input := apigateway.PutMethodInput{
AuthorizationType: aws.String(d.Get("authorization").(string)),
HttpMethod: aws.String(d.Get("http_method").(string)),
ResourceId: aws.String(d.Get("resource_id").(string)),
RestApiId: aws.String(d.Get("rest_api_id").(string)),
ApiKeyRequired: aws.Bool(d.Get("api_key_required").(bool)),
}
models := make(map[string]string)
for k, v := range d.Get("request_models").(map[string]interface{}) {
models[k] = v.(string)
}
if len(models) > 0 {
input.RequestModels = aws.StringMap(models)
}
parameters := make(map[string]bool)
if kv, ok := d.GetOk("request_parameters"); ok {
for k, v := range kv.(map[string]interface{}) {
parameters[k], ok = v.(bool)
if !ok {
value, _ := strconv.ParseBool(v.(string))
parameters[k] = value
}
}
input.RequestParameters = aws.BoolMap(parameters)
}
if v, ok := d.GetOk("authorizer_id"); ok {
input.AuthorizerId = aws.String(v.(string))
}
if v, ok := d.GetOk("authorization_scopes"); ok {
input.AuthorizationScopes = expandStringList(v.(*schema.Set).List())
}
if v, ok := d.GetOk("request_validator_id"); ok {
input.RequestValidatorId = aws.String(v.(string))
}
_, err := conn.PutMethod(&input)
if err != nil {
return fmt.Errorf("Error creating API Gateway Method: %s", err)
}
d.SetId(fmt.Sprintf("agm-%s-%s-%s", d.Get("rest_api_id").(string), d.Get("resource_id").(string), d.Get("http_method").(string)))
log.Printf("[DEBUG] API Gateway Method ID: %s", d.Id())
return nil
}
func resourceAwsApiGatewayMethodRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigatewayconn
log.Printf("[DEBUG] Reading API Gateway Method %s", d.Id())
out, err := conn.GetMethod(&apigateway.GetMethodInput{
HttpMethod: aws.String(d.Get("http_method").(string)),
ResourceId: aws.String(d.Get("resource_id").(string)),
RestApiId: aws.String(d.Get("rest_api_id").(string)),
})
if err != nil {
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NotFoundException" {
log.Printf("[WARN] API Gateway Method (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
return err
}
log.Printf("[DEBUG] Received API Gateway Method: %s", out)
d.Set("api_key_required", out.ApiKeyRequired)
if err := d.Set("authorization_scopes", flattenStringList(out.AuthorizationScopes)); err != nil {
return fmt.Errorf("error setting authorization_scopes: %s", err)
}
d.Set("authorization", out.AuthorizationType)
d.Set("authorizer_id", out.AuthorizerId)
if err := d.Set("request_models", aws.StringValueMap(out.RequestModels)); err != nil {
return fmt.Errorf("error setting request_models: %s", err)
}
if err := d.Set("request_parameters", aws.BoolValueMap(out.RequestParameters)); err != nil {
return fmt.Errorf("error setting request_parameters: %s", err)
}
d.Set("request_validator_id", out.RequestValidatorId)
return nil
}
func resourceAwsApiGatewayMethodUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigatewayconn
log.Printf("[DEBUG] Reading API Gateway Method %s", d.Id())
operations := make([]*apigateway.PatchOperation, 0)
if d.HasChange("resource_id") {
operations = append(operations, &apigateway.PatchOperation{
Op: aws.String("replace"),
Path: aws.String("/resourceId"),
Value: aws.String(d.Get("resource_id").(string)),
})
}
if d.HasChange("request_models") {
operations = append(operations, expandApiGatewayRequestResponseModelOperations(d, "request_models", "requestModels")...)
}
if d.HasChange("request_parameters_in_json") {
ops, err := deprecatedExpandApiGatewayMethodParametersJSONOperations(d, "request_parameters_in_json", "requestParameters")
if err != nil {
return err
}
operations = append(operations, ops...)
}
if d.HasChange("request_parameters") {
parameters := make(map[string]bool)
var ok bool
for k, v := range d.Get("request_parameters").(map[string]interface{}) {
parameters[k], ok = v.(bool)
if !ok {
value, _ := strconv.ParseBool(v.(string))
parameters[k] = value
}
}
ops, err := expandApiGatewayMethodParametersOperations(d, "request_parameters", "requestParameters")
if err != nil {
return err
}
operations = append(operations, ops...)
}
if d.HasChange("authorization") {
operations = append(operations, &apigateway.PatchOperation{
Op: aws.String("replace"),
Path: aws.String("/authorizationType"),
Value: aws.String(d.Get("authorization").(string)),
})
}
if d.HasChange("authorizer_id") {
operations = append(operations, &apigateway.PatchOperation{
Op: aws.String("replace"),
Path: aws.String("/authorizerId"),
Value: aws.String(d.Get("authorizer_id").(string)),
})
}
if d.HasChange("authorization_scopes") {
old, new := d.GetChange("authorization_scopes")
path := "/authorizationScopes"
os := old.(*schema.Set)
ns := new.(*schema.Set)
additionList := ns.Difference(os)
for _, v := range additionList.List() {
operations = append(operations, &apigateway.PatchOperation{
Op: aws.String("add"),
Path: aws.String(path),
Value: aws.String(v.(string)),
})
}
removalList := os.Difference(ns)
for _, v := range removalList.List() {
operations = append(operations, &apigateway.PatchOperation{
Op: aws.String("remove"),
Path: aws.String(path),
Value: aws.String(v.(string)),
})
}
}
if d.HasChange("api_key_required") {
operations = append(operations, &apigateway.PatchOperation{
Op: aws.String("replace"),
Path: aws.String("/apiKeyRequired"),
Value: aws.String(fmt.Sprintf("%t", d.Get("api_key_required").(bool))),
})
}
if d.HasChange("request_validator_id") {
var request_validator_id *string
if v, ok := d.GetOk("request_validator_id"); ok {
// requestValidatorId cannot be an empty string; it must either be nil
// or it must have some value. Otherwise, updating fails.
if s := v.(string); len(s) > 0 {
request_validator_id = &s
}
}
operations = append(operations, &apigateway.PatchOperation{
Op: aws.String("replace"),
Path: aws.String("/requestValidatorId"),
Value: request_validator_id,
})
}
method, err := conn.UpdateMethod(&apigateway.UpdateMethodInput{
HttpMethod: aws.String(d.Get("http_method").(string)),
ResourceId: aws.String(d.Get("resource_id").(string)),
RestApiId: aws.String(d.Get("rest_api_id").(string)),
PatchOperations: operations,
})
if err != nil {
return err
}
log.Printf("[DEBUG] Received API Gateway Method: %s", method)
return resourceAwsApiGatewayMethodRead(d, meta)
}
func resourceAwsApiGatewayMethodDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigatewayconn
log.Printf("[DEBUG] Deleting API Gateway Method: %s", d.Id())
_, err := conn.DeleteMethod(&apigateway.DeleteMethodInput{
HttpMethod: aws.String(d.Get("http_method").(string)),
ResourceId: aws.String(d.Get("resource_id").(string)),
RestApiId: aws.String(d.Get("rest_api_id").(string)),
})
if isAWSErr(err, apigateway.ErrCodeNotFoundException, "") {
return nil
}
if err != nil {
return fmt.Errorf("error deleting API Gateway Method (%s): %s", d.Id(), err)
}
return nil
}