forked from hashicorp/terraform-provider-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_aws_api_gateway_method.go
297 lines (254 loc) · 8.64 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
package aws
import (
"encoding/json"
"fmt"
"log"
"strconv"
"time"
"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/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsApiGatewayMethod() *schema.Resource {
return &schema.Resource{
Create: resourceAwsApiGatewayMethodCreate,
Read: resourceAwsApiGatewayMethodRead,
Update: resourceAwsApiGatewayMethodUpdate,
Delete: resourceAwsApiGatewayMethodDelete,
Schema: map[string]*schema.Schema{
"rest_api_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"resource_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"http_method": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateHTTPMethod(),
},
"authorization": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"authorizer_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"api_key_required": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"request_models": &schema.Schema{
Type: schema.TypeMap,
Optional: true,
Elem: schema.TypeString,
},
"request_parameters": &schema.Schema{
Type: schema.TypeMap,
Elem: schema.TypeBool,
Optional: true,
ConflictsWith: []string{"request_parameters_in_json"},
},
"request_parameters_in_json": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"request_parameters"},
Deprecated: "Use field request_parameters instead",
},
"request_validator_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
},
}
}
func resourceAwsApiGatewayMethodCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigateway
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("request_parameters_in_json"); ok {
if err := json.Unmarshal([]byte(v.(string)), ¶meters); err != nil {
return fmt.Errorf("Error unmarshaling request_parameters_in_json: %s", err)
}
input.RequestParameters = aws.BoolMap(parameters)
}
if v, ok := d.GetOk("authorizer_id"); ok {
input.AuthorizerId = aws.String(v.(string))
}
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).apigateway
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.SetId(fmt.Sprintf("agm-%s-%s-%s", d.Get("rest_api_id").(string), d.Get("resource_id").(string), d.Get("http_method").(string)))
d.Set("request_parameters", aws.BoolValueMap(out.RequestParameters))
d.Set("request_parameters_in_json", aws.BoolValueMap(out.RequestParameters))
d.Set("api_key_required", out.ApiKeyRequired)
d.Set("authorization", out.AuthorizationType)
d.Set("authorizer_id", out.AuthorizerId)
d.Set("request_models", aws.StringValueMap(out.RequestModels))
d.Set("request_validator_id", out.RequestValidatorId)
return nil
}
func resourceAwsApiGatewayMethodUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigateway
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("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).apigateway
log.Printf("[DEBUG] Deleting API Gateway Method: %s", d.Id())
return resource.Retry(5*time.Minute, func() *resource.RetryError {
_, 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 err == nil {
return nil
}
apigatewayErr, ok := err.(awserr.Error)
if apigatewayErr.Code() == "NotFoundException" {
return nil
}
if !ok {
return resource.NonRetryableError(err)
}
return resource.NonRetryableError(err)
})
}