Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resource/apigatewayv2: Add support for body parameter #12567

Merged
merged 2 commits into from
Aug 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion aws/data_source_aws_cloudformation_stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func dataSourceAwsCloudFormationStackRead(d *schema.ResourceData, meta interface
return err
}

template, err := normalizeCloudFormationTemplate(*tOut.TemplateBody)
template, err := normalizeJsonOrYamlString(*tOut.TemplateBody)
if err != nil {
return fmt.Errorf("template body contains an invalid JSON or YAML: %s", err)
}
Expand Down
6 changes: 3 additions & 3 deletions aws/diff_suppress_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,15 @@ func suppressOpenIdURL(k, old, new string, d *schema.ResourceData) bool {
return oldUrl.String() == newUrl.String()
}

func suppressCloudFormationTemplateBodyDiffs(k, old, new string, d *schema.ResourceData) bool {
normalizedOld, err := normalizeCloudFormationTemplate(old)
func suppressEquivalentJsonOrYamlDiffs(k, old, new string, d *schema.ResourceData) bool {
normalizedOld, err := normalizeJsonOrYamlString(old)

if err != nil {
log.Printf("[WARN] Unable to normalize Terraform state CloudFormation template body: %s", err)
return false
}

normalizedNew, err := normalizeCloudFormationTemplate(new)
normalizedNew, err := normalizeJsonOrYamlString(new)

if err != nil {
log.Printf("[WARN] Unable to normalize Terraform configuration CloudFormation template body: %s", err)
Expand Down
4 changes: 2 additions & 2 deletions aws/diff_suppress_funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func TestSuppressEquivalentTypeStringBoolean(t *testing.T) {
}
}

func TestSuppressCloudFormationTemplateBodyDiffs(t *testing.T) {
func TestSuppressEquivalentJsonOrYamlDiffs(t *testing.T) {
testCases := []struct {
description string
equivalent bool
Expand Down Expand Up @@ -253,7 +253,7 @@ Outputs:
}

for _, tc := range testCases {
value := suppressCloudFormationTemplateBodyDiffs("test_property", tc.old, tc.new, nil)
value := suppressEquivalentJsonOrYamlDiffs("test_property", tc.old, tc.new, nil)

if tc.equivalent && !value {
t.Fatalf("expected test case (%s) to be equivalent", tc.description)
Expand Down
77 changes: 77 additions & 0 deletions aws/resource_aws_apigatewayv2_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package aws
import (
"fmt"
"log"
"reflect"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
Expand Down Expand Up @@ -101,6 +102,12 @@ func resourceAwsApiGatewayV2Api() *schema.Resource {
Required: true,
ValidateFunc: validation.StringLenBetween(1, 128),
},
"body": {
Type: schema.TypeString,
Optional: true,
DiffSuppressFunc: suppressEquivalentJsonOrYamlDiffs,
ValidateFunc: validateStringIsJsonOrYaml,
},
"protocol_type": {
Type: schema.TypeString,
Required: true,
Expand Down Expand Up @@ -135,6 +142,64 @@ func resourceAwsApiGatewayV2Api() *schema.Resource {
}
}

func resourceAwsAPIGatewayV2ImportOpenAPI(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigatewayv2conn

if body, ok := d.GetOk("body"); ok {
revertReq := &apigatewayv2.UpdateApiInput{
vincenting marked this conversation as resolved.
Show resolved Hide resolved
ApiId: aws.String(d.Id()),
Name: aws.String(d.Get("name").(string)),
Description: aws.String(d.Get("description").(string)),
Version: aws.String(d.Get("version").(string)),
}

log.Printf("[DEBUG] Updating API Gateway from OpenAPI spec %s", d.Id())
importReq := &apigatewayv2.ReimportApiInput{
ApiId: aws.String(d.Id()),
Body: aws.String(body.(string)),
}

_, err := conn.ReimportApi(importReq)

if err != nil {
return fmt.Errorf("error importing API Gateway v2 API (%s) OpenAPI specification: %s", d.Id(), err)
}

tags := d.Get("tags")
corsConfiguration := d.Get("cors_configuration")

if err := resourceAwsApiGatewayV2ApiRead(d, meta); err != nil {
return err
}

if !reflect.DeepEqual(corsConfiguration, d.Get("cors_configuration")) {
if len(corsConfiguration.([]interface{})) == 0 {
log.Printf("[DEBUG] Deleting CORS configuration for API Gateway v2 API (%s)", d.Id())
_, err := conn.DeleteCorsConfiguration(&apigatewayv2.DeleteCorsConfigurationInput{
ApiId: aws.String(d.Id()),
})
if err != nil {
return fmt.Errorf("error deleting CORS configuration for API Gateway v2 API (%s): %s", d.Id(), err)
}
} else {
revertReq.CorsConfiguration = expandApiGateway2CorsConfiguration(corsConfiguration.([]interface{}))
}
}

if err := keyvaluetags.Apigatewayv2UpdateTags(conn, d.Get("arn").(string), d.Get("tags"), tags); err != nil {
return fmt.Errorf("error updating API Gateway v2 API (%s) tags: %s", d.Id(), err)
}

log.Printf("[DEBUG] Reverting API Gateway v2 API: %s", revertReq)
_, err = conn.UpdateApi(revertReq)
if err != nil {
return fmt.Errorf("error updating API Gateway v2 API (%s): %s", d.Id(), err)
}
}

return nil
}

func resourceAwsApiGatewayV2ApiCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigatewayv2conn

Expand Down Expand Up @@ -177,6 +242,11 @@ func resourceAwsApiGatewayV2ApiCreate(d *schema.ResourceData, meta interface{})

d.SetId(aws.StringValue(resp.ApiId))

err = resourceAwsAPIGatewayV2ImportOpenAPI(d, meta)
if err != nil {
return err
}

return resourceAwsApiGatewayV2ApiRead(d, meta)
}

Expand Down Expand Up @@ -286,6 +356,13 @@ func resourceAwsApiGatewayV2ApiUpdate(d *schema.ResourceData, meta interface{})
}
}

if d.HasChange("body") {
err := resourceAwsAPIGatewayV2ImportOpenAPI(d, meta)
if err != nil {
return err
}
}

return resourceAwsApiGatewayV2ApiRead(d, meta)
}

Expand Down
Loading