Skip to content

Commit d70147b

Browse files
committed
provider/aws: Added API Gateway integration update
1 parent 5ba7aa8 commit d70147b

File tree

3 files changed

+244
-92
lines changed

3 files changed

+244
-92
lines changed

builtin/providers/aws/resource_aws_api_gateway_integration.go

Lines changed: 136 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,87 +11,94 @@ import (
1111
"github.com/aws/aws-sdk-go/service/apigateway"
1212
"github.com/hashicorp/terraform/helper/resource"
1313
"github.com/hashicorp/terraform/helper/schema"
14+
"strings"
1415
)
1516

1617
func resourceAwsApiGatewayIntegration() *schema.Resource {
1718
return &schema.Resource{
1819
Create: resourceAwsApiGatewayIntegrationCreate,
1920
Read: resourceAwsApiGatewayIntegrationRead,
20-
Update: resourceAwsApiGatewayIntegrationCreate,
21+
Update: resourceAwsApiGatewayIntegrationUpdate,
2122
Delete: resourceAwsApiGatewayIntegrationDelete,
2223

2324
Schema: map[string]*schema.Schema{
24-
"rest_api_id": &schema.Schema{
25+
"rest_api_id": {
2526
Type: schema.TypeString,
2627
Required: true,
2728
ForceNew: true,
2829
},
2930

30-
"resource_id": &schema.Schema{
31+
"resource_id": {
3132
Type: schema.TypeString,
3233
Required: true,
3334
ForceNew: true,
3435
},
3536

36-
"http_method": &schema.Schema{
37+
"http_method": {
3738
Type: schema.TypeString,
3839
Required: true,
3940
ForceNew: true,
4041
ValidateFunc: validateHTTPMethod,
4142
},
4243

43-
"type": &schema.Schema{
44+
"type": {
4445
Type: schema.TypeString,
4546
Required: true,
47+
ForceNew: true,
4648
ValidateFunc: validateApiGatewayIntegrationType,
4749
},
4850

49-
"uri": &schema.Schema{
51+
"uri": {
5052
Type: schema.TypeString,
5153
Optional: true,
54+
ForceNew: true,
5255
},
5356

54-
"credentials": &schema.Schema{
57+
"credentials": {
5558
Type: schema.TypeString,
5659
Optional: true,
60+
ForceNew: true,
5761
},
5862

59-
"integration_http_method": &schema.Schema{
63+
"integration_http_method": {
6064
Type: schema.TypeString,
6165
Optional: true,
66+
ForceNew: true,
6267
ValidateFunc: validateHTTPMethod,
6368
},
6469

65-
"request_templates": &schema.Schema{
70+
"request_templates": {
6671
Type: schema.TypeMap,
6772
Optional: true,
6873
Elem: schema.TypeString,
6974
},
7075

71-
"request_parameters": &schema.Schema{
76+
"request_parameters": {
7277
Type: schema.TypeMap,
7378
Elem: schema.TypeString,
7479
Optional: true,
7580
ConflictsWith: []string{"request_parameters_in_json"},
7681
},
7782

78-
"request_parameters_in_json": &schema.Schema{
83+
"request_parameters_in_json": {
7984
Type: schema.TypeString,
8085
Optional: true,
8186
ConflictsWith: []string{"request_parameters"},
8287
Deprecated: "Use field request_parameters instead",
8388
},
8489

85-
"content_handling": &schema.Schema{
90+
"content_handling": {
8691
Type: schema.TypeString,
8792
Optional: true,
93+
ForceNew: true,
8894
ValidateFunc: validateApiGatewayIntegrationContentHandling,
8995
},
9096

91-
"passthrough_behavior": &schema.Schema{
97+
"passthrough_behavior": {
9298
Type: schema.TypeString,
9399
Optional: true,
94100
Computed: true,
101+
ForceNew: true,
95102
ValidateFunc: validateApiGatewayIntegrationPassthroughBehavior,
96103
},
97104
},
@@ -101,6 +108,7 @@ func resourceAwsApiGatewayIntegration() *schema.Resource {
101108
func resourceAwsApiGatewayIntegrationCreate(d *schema.ResourceData, meta interface{}) error {
102109
conn := meta.(*AWSClient).apigateway
103110

111+
log.Print("[DEBUG] Creating API Gateway Integration")
104112
var integrationHttpMethod *string
105113
if v, ok := d.GetOk("integration_http_method"); ok {
106114
integrationHttpMethod = aws.String(v.(string))
@@ -163,13 +171,13 @@ func resourceAwsApiGatewayIntegrationCreate(d *schema.ResourceData, meta interfa
163171

164172
d.SetId(fmt.Sprintf("agi-%s-%s-%s", d.Get("rest_api_id").(string), d.Get("resource_id").(string), d.Get("http_method").(string)))
165173

166-
return nil
174+
return resourceAwsApiGatewayIntegrationRead(d, meta)
167175
}
168176

169177
func resourceAwsApiGatewayIntegrationRead(d *schema.ResourceData, meta interface{}) error {
170178
conn := meta.(*AWSClient).apigateway
171179

172-
log.Printf("[DEBUG] Reading API Gateway Integration %s", d.Id())
180+
log.Printf("[DEBUG] Reading API Gateway Integration: %s", d.Id())
173181
integration, err := conn.GetIntegration(&apigateway.GetIntegrationInput{
174182
HttpMethod: aws.String(d.Get("http_method").(string)),
175183
ResourceId: aws.String(d.Get("resource_id").(string)),
@@ -191,17 +199,127 @@ func resourceAwsApiGatewayIntegrationRead(d *schema.ResourceData, meta interface
191199
}
192200

193201
d.Set("request_templates", aws.StringValueMap(integration.RequestTemplates))
194-
d.Set("credentials", integration.Credentials)
195202
d.Set("type", integration.Type)
196-
d.Set("uri", integration.Uri)
197203
d.Set("request_parameters", aws.StringValueMap(integration.RequestParameters))
198204
d.Set("request_parameters_in_json", aws.StringValueMap(integration.RequestParameters))
199205
d.Set("passthrough_behavior", integration.PassthroughBehavior)
200-
d.Set("content_handling", integration.ContentHandling)
206+
207+
if integration.Uri != nil {
208+
d.Set("uri", integration.Uri)
209+
}
210+
211+
if integration.Credentials != nil {
212+
d.Set("credentials", integration.Credentials)
213+
}
214+
215+
if integration.ContentHandling != nil {
216+
d.Set("content_handling", integration.ContentHandling)
217+
}
201218

202219
return nil
203220
}
204221

222+
func resourceAwsApiGatewayIntegrationUpdate(d *schema.ResourceData, meta interface{}) error {
223+
conn := meta.(*AWSClient).apigateway
224+
225+
log.Printf("[DEBUG] Updating API Gateway Integration: %s", d.Id())
226+
operations := make([]*apigateway.PatchOperation, 0)
227+
228+
// https://docs.aws.amazon.com/apigateway/api-reference/link-relation/integration-update/#remarks
229+
// According to the above documentation, only a few parts are addable / removable.
230+
if d.HasChange("request_templates") {
231+
o, n := d.GetChange("request_templates")
232+
prefix := "requestTemplates"
233+
234+
os := o.(map[string]interface{})
235+
ns := n.(map[string]interface{})
236+
237+
// Handle Removal
238+
for k := range os {
239+
if _, ok := ns[k]; !ok {
240+
operations = append(operations, &apigateway.PatchOperation{
241+
Op: aws.String("remove"),
242+
Path: aws.String(fmt.Sprintf("/%s/%s", prefix, strings.Replace(k, "/", "~1", -1))),
243+
})
244+
}
245+
}
246+
247+
for k, v := range ns {
248+
// Handle replaces
249+
if _, ok := os[k]; ok {
250+
operations = append(operations, &apigateway.PatchOperation{
251+
Op: aws.String("replace"),
252+
Path: aws.String(fmt.Sprintf("/%s/%s", prefix, strings.Replace(k, "/", "~1", -1))),
253+
Value: aws.String(v.(string)),
254+
})
255+
}
256+
257+
// Handle additions
258+
if _, ok := os[k]; !ok {
259+
operations = append(operations, &apigateway.PatchOperation{
260+
Op: aws.String("add"),
261+
Path: aws.String(fmt.Sprintf("/%s/%s", prefix, strings.Replace(k, "/", "~1", -1))),
262+
Value: aws.String(v.(string)),
263+
})
264+
}
265+
}
266+
}
267+
268+
if d.HasChange("request_parameters") {
269+
o, n := d.GetChange("request_parameters")
270+
prefix := "requestParameters"
271+
272+
os := o.(map[string]interface{})
273+
ns := n.(map[string]interface{})
274+
275+
// Handle Removal
276+
for k := range os {
277+
if _, ok := ns[k]; !ok {
278+
operations = append(operations, &apigateway.PatchOperation{
279+
Op: aws.String("remove"),
280+
Path: aws.String(fmt.Sprintf("/%s/%s", prefix, strings.Replace(k, "/", "~1", -1))),
281+
})
282+
}
283+
}
284+
285+
for k, v := range ns {
286+
// Handle replaces
287+
if _, ok := os[k]; ok {
288+
operations = append(operations, &apigateway.PatchOperation{
289+
Op: aws.String("replace"),
290+
Path: aws.String(fmt.Sprintf("/%s/%s", prefix, strings.Replace(k, "/", "~1", -1))),
291+
Value: aws.String(v.(string)),
292+
})
293+
}
294+
295+
// Handle additions
296+
if _, ok := os[k]; !ok {
297+
operations = append(operations, &apigateway.PatchOperation{
298+
Op: aws.String("add"),
299+
Path: aws.String(fmt.Sprintf("/%s/%s", prefix, strings.Replace(k, "/", "~1", -1))),
300+
Value: aws.String(v.(string)),
301+
})
302+
}
303+
}
304+
}
305+
306+
params := &apigateway.UpdateIntegrationInput{
307+
HttpMethod: aws.String(d.Get("http_method").(string)),
308+
ResourceId: aws.String(d.Get("resource_id").(string)),
309+
RestApiId: aws.String(d.Get("rest_api_id").(string)),
310+
PatchOperations: operations,
311+
}
312+
313+
_, err := conn.UpdateIntegration(params)
314+
if err != nil {
315+
return fmt.Errorf("Error updating API Gateway Integration: %s", err)
316+
}
317+
318+
d.SetId(fmt.Sprintf("agi-%s-%s-%s", d.Get("rest_api_id").(string), d.Get("resource_id").(string), d.Get("http_method").(string)))
319+
320+
return resourceAwsApiGatewayIntegrationRead(d, meta)
321+
}
322+
205323
func resourceAwsApiGatewayIntegrationDelete(d *schema.ResourceData, meta interface{}) error {
206324
conn := meta.(*AWSClient).apigateway
207325
log.Printf("[DEBUG] Deleting API Gateway Integration: %s", d.Id())

0 commit comments

Comments
 (0)