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

r/aws_apigatewayv2_stage: Correctly handle deletion of route_settings #16133

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
20 changes: 20 additions & 0 deletions aws/resource_aws_api_gateway_account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"regexp"
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/apigateway"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
Expand Down Expand Up @@ -114,6 +115,25 @@ func testAccCheckAWSAPIGatewayAccountDestroy(s *terraform.State) error {
return nil
}

// testAccPreCheckAWSAPIGatewayAccountCloudWatchRoleArn checks whether a CloudWatch role ARN has been configured in the current AWS region.
func testAccPreCheckAWSAPIGatewayAccountCloudWatchRoleArn(t *testing.T) {
conn := testAccProvider.Meta().(*AWSClient).apigatewayconn

output, err := conn.GetAccount(&apigateway.GetAccountInput{})

if testAccPreCheckSkipError(err) {
t.Skipf("skipping tests: %s", err)
}

if err != nil {
t.Fatalf("error reading API Gateway Account: %s", err)
}

if output == nil || aws.StringValue(output.CloudwatchRoleArn) == "" {
t.Skip("skipping tests; no API Gateway CloudWatch role ARN has been configured in this region")
}
}

const testAccAWSAPIGatewayAccountConfig_empty = `
resource "aws_api_gateway_account" "test" {
}
Expand Down
63 changes: 27 additions & 36 deletions aws/resource_aws_apigatewayv2_stage.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package aws

import (
"bytes"
"fmt"
"log"
"strings"
Expand All @@ -11,7 +10,6 @@ import (
"github.com/aws/aws-sdk-go/service/apigatewayv2"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/hashcode"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)

Expand Down Expand Up @@ -170,7 +168,6 @@ func resourceAwsApiGatewayV2Stage() *schema.Resource {
},
},
},
Set: apiGatewayV2RouteSettingsHash,
},
"stage_variables": {
Type: schema.TypeMap,
Expand Down Expand Up @@ -218,7 +215,7 @@ func resourceAwsApiGatewayV2StageCreate(d *schema.ResourceData, meta interface{}
req.Description = aws.String(v.(string))
}
if v, ok := d.GetOk("route_settings"); ok {
req.RouteSettings = expandApiGatewayV2RouteSettings(v.(*schema.Set), protocolType)
req.RouteSettings = expandApiGatewayV2RouteSettings(v.(*schema.Set).List(), protocolType)
}
if v, ok := d.GetOk("stage_variables"); ok {
req.StageVariables = stringMapToPointers(v.(map[string]interface{}))
Expand Down Expand Up @@ -356,7 +353,28 @@ func resourceAwsApiGatewayV2StageUpdate(d *schema.ResourceData, meta interface{}
req.Description = aws.String(d.Get("description").(string))
}
if d.HasChange("route_settings") {
req.RouteSettings = expandApiGatewayV2RouteSettings(d.Get("route_settings").(*schema.Set), protocolType)
o, n := d.GetChange("route_settings")
os := o.(*schema.Set)
ns := n.(*schema.Set)

for _, vRouteSetting := range os.Difference(ns).List() {
routeKey := vRouteSetting.(map[string]interface{})["route_key"].(string)

log.Printf("[DEBUG] Deleting API Gateway v2 stage (%s) route settings (%s)", d.Id(), routeKey)
_, err := conn.DeleteRouteSettings(&apigatewayv2.DeleteRouteSettingsInput{
ApiId: aws.String(d.Get("api_id").(string)),
RouteKey: aws.String(routeKey),
StageName: aws.String(d.Id()),
})
if isAWSErr(err, apigatewayv2.ErrCodeNotFoundException, "") {
continue
}
if err != nil {
return fmt.Errorf("error deleting API Gateway v2 stage (%s) route settings (%s): %w", d.Id(), routeKey, err)
}
}

req.RouteSettings = expandApiGatewayV2RouteSettings(ns.List(), protocolType)
}
if d.HasChange("stage_variables") {
o, n := d.GetChange("stage_variables")
Expand Down Expand Up @@ -506,10 +524,10 @@ func flattenApiGatewayV2DefaultRouteSettings(routeSettings *apigatewayv2.RouteSe
}}
}

func expandApiGatewayV2RouteSettings(vSettings *schema.Set, protocolType string) map[string]*apigatewayv2.RouteSettings {
func expandApiGatewayV2RouteSettings(vSettings []interface{}, protocolType string) map[string]*apigatewayv2.RouteSettings {
settings := map[string]*apigatewayv2.RouteSettings{}

for _, v := range vSettings.List() {
for _, v := range vSettings {
routeSettings := &apigatewayv2.RouteSettings{}

mSettings := v.(map[string]interface{})
Expand All @@ -536,7 +554,7 @@ func expandApiGatewayV2RouteSettings(vSettings *schema.Set, protocolType string)
return settings
}

func flattenApiGatewayV2RouteSettings(settings map[string]*apigatewayv2.RouteSettings) *schema.Set {
func flattenApiGatewayV2RouteSettings(settings map[string]*apigatewayv2.RouteSettings) []interface{} {
vSettings := []interface{}{}

for k, routeSetting := range settings {
Expand All @@ -550,32 +568,5 @@ func flattenApiGatewayV2RouteSettings(settings map[string]*apigatewayv2.RouteSet
})
}

return schema.NewSet(apiGatewayV2RouteSettingsHash, vSettings)
}

func apiGatewayV2RouteSettingsHash(vSettings interface{}) int {
var buf bytes.Buffer

mSettings := vSettings.(map[string]interface{})

if v, ok := mSettings["route_key"].(string); ok {
buf.WriteString(fmt.Sprintf("%s-", v))
}
if v, ok := mSettings["data_trace_enabled"].(bool); ok {
buf.WriteString(fmt.Sprintf("%t-", v))
}
if v, ok := mSettings["detailed_metrics_enabled"].(bool); ok {
buf.WriteString(fmt.Sprintf("%t-", v))
}
if v, ok := mSettings["logging_level"].(string); ok {
buf.WriteString(fmt.Sprintf("%s-", v))
}
if v, ok := mSettings["throttling_burst_limit"].(int); ok {
buf.WriteString(fmt.Sprintf("%d-", v))
}
if v, ok := mSettings["throttling_rate_limit"].(float64); ok {
buf.WriteString(fmt.Sprintf("%g-", v))
}

return hashcode.String(buf.String())
return vSettings
}
Loading