From ed0936385258164c40af48ea96418221641ab6f5 Mon Sep 17 00:00:00 2001 From: DrFaust92 Date: Fri, 3 Apr 2020 11:43:46 +0300 Subject: [PATCH 1/3] fix zero ttl add plan time validations to `logging level` and `unauthorized_cache_control_header_strategy` --- ...esource_aws_api_gateway_method_settings.go | 50 +++++++++++-------- ...ce_aws_api_gateway_method_settings_test.go | 8 +++ 2 files changed, 38 insertions(+), 20 deletions(-) diff --git a/aws/resource_aws_api_gateway_method_settings.go b/aws/resource_aws_api_gateway_method_settings.go index eeba69d1b3e..b5d27e38bf7 100644 --- a/aws/resource_aws_api_gateway_method_settings.go +++ b/aws/resource_aws_api_gateway_method_settings.go @@ -5,9 +5,9 @@ import ( "log" "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" + "github.com/hashicorp/terraform-plugin-sdk/helper/validation" ) func resourceAwsApiGatewayMethodSettings() *schema.Resource { @@ -46,6 +46,11 @@ func resourceAwsApiGatewayMethodSettings() *schema.Resource { "logging_level": { Type: schema.TypeString, Optional: true, + ValidateFunc: validation.StringInSlice([]string{ + "OFF", + "ERROR", + "INFO", + }, false), }, "data_trace_enabled": { Type: schema.TypeBool, @@ -78,6 +83,11 @@ func resourceAwsApiGatewayMethodSettings() *schema.Resource { "unauthorized_cache_control_header_strategy": { Type: schema.TypeString, Optional: true, + ValidateFunc: validation.StringInSlice([]string{ + apigateway.UnauthorizedCacheControlHeaderStrategyFailWith403, + apigateway.UnauthorizedCacheControlHeaderStrategySucceedWithResponseHeader, + apigateway.UnauthorizedCacheControlHeaderStrategySucceedWithoutResponseHeader, + }, false), }, }, }, @@ -96,7 +106,7 @@ func resourceAwsApiGatewayMethodSettingsRead(d *schema.ResourceData, meta interf } stage, err := conn.GetStage(&input) if err != nil { - if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NotFoundException" { + if isAWSErr(err, apigateway.ErrCodeNotFoundException, "") { log.Printf("[WARN] API Gateway Stage (%s) not found, removing method settings", d.Id()) d.SetId("") return nil @@ -119,7 +129,7 @@ func resourceAwsApiGatewayMethodSettingsRead(d *schema.ResourceData, meta interf d.Set("settings.0.throttling_burst_limit", settings.ThrottlingBurstLimit) d.Set("settings.0.throttling_rate_limit", settings.ThrottlingRateLimit) d.Set("settings.0.caching_enabled", settings.CachingEnabled) - d.Set("settings.0.cache_ttl_in_seconds", settings.CacheTtlInSeconds) + d.Set("settings.0.cache_ttl_in_seconds", aws.Int64Value(settings.CacheTtlInSeconds)) d.Set("settings.0.cache_data_encrypted", settings.CacheDataEncrypted) d.Set("settings.0.require_authorization_for_cache_control", settings.RequireAuthorizationForCacheControl) d.Set("settings.0.unauthorized_cache_control_header_strategy", settings.UnauthorizedCacheControlHeaderStrategy) @@ -136,21 +146,21 @@ func resourceAwsApiGatewayMethodSettingsUpdate(d *schema.ResourceData, meta inte ops := make([]*apigateway.PatchOperation, 0) if d.HasChange("settings.0.metrics_enabled") { ops = append(ops, &apigateway.PatchOperation{ - Op: aws.String("replace"), + Op: aws.String(apigateway.OpReplace), Path: aws.String(prefix + "metrics/enabled"), Value: aws.String(fmt.Sprintf("%t", d.Get("settings.0.metrics_enabled").(bool))), }) } if d.HasChange("settings.0.logging_level") { ops = append(ops, &apigateway.PatchOperation{ - Op: aws.String("replace"), + Op: aws.String(apigateway.OpReplace), Path: aws.String(prefix + "logging/loglevel"), Value: aws.String(d.Get("settings.0.logging_level").(string)), }) } if d.HasChange("settings.0.data_trace_enabled") { ops = append(ops, &apigateway.PatchOperation{ - Op: aws.String("replace"), + Op: aws.String(apigateway.OpReplace), Path: aws.String(prefix + "logging/dataTrace"), Value: aws.String(fmt.Sprintf("%t", d.Get("settings.0.data_trace_enabled").(bool))), }) @@ -158,49 +168,49 @@ func resourceAwsApiGatewayMethodSettingsUpdate(d *schema.ResourceData, meta inte if d.HasChange("settings.0.throttling_burst_limit") { ops = append(ops, &apigateway.PatchOperation{ - Op: aws.String("replace"), + Op: aws.String(apigateway.OpReplace), Path: aws.String(prefix + "throttling/burstLimit"), Value: aws.String(fmt.Sprintf("%d", d.Get("settings.0.throttling_burst_limit").(int))), }) } if d.HasChange("settings.0.throttling_rate_limit") { ops = append(ops, &apigateway.PatchOperation{ - Op: aws.String("replace"), + Op: aws.String(apigateway.OpReplace), Path: aws.String(prefix + "throttling/rateLimit"), Value: aws.String(fmt.Sprintf("%f", d.Get("settings.0.throttling_rate_limit").(float64))), }) } if d.HasChange("settings.0.caching_enabled") { ops = append(ops, &apigateway.PatchOperation{ - Op: aws.String("replace"), + Op: aws.String(apigateway.OpReplace), Path: aws.String(prefix + "caching/enabled"), Value: aws.String(fmt.Sprintf("%t", d.Get("settings.0.caching_enabled").(bool))), }) } - if d.HasChange("settings.0.cache_ttl_in_seconds") { - ops = append(ops, &apigateway.PatchOperation{ - Op: aws.String("replace"), - Path: aws.String(prefix + "caching/ttlInSeconds"), - Value: aws.String(fmt.Sprintf("%d", d.Get("settings.0.cache_ttl_in_seconds").(int))), - }) - } + + ops = append(ops, &apigateway.PatchOperation{ + Op: aws.String(apigateway.OpReplace), + Path: aws.String(prefix + "caching/ttlInSeconds"), + Value: aws.String(fmt.Sprintf("%d", d.Get("settings.0.cache_ttl_in_seconds").(int))), + }) + if d.HasChange("settings.0.cache_data_encrypted") { ops = append(ops, &apigateway.PatchOperation{ - Op: aws.String("replace"), + Op: aws.String(apigateway.OpReplace), Path: aws.String(prefix + "caching/dataEncrypted"), Value: aws.String(fmt.Sprintf("%t", d.Get("settings.0.cache_data_encrypted").(bool))), }) } if d.HasChange("settings.0.require_authorization_for_cache_control") { ops = append(ops, &apigateway.PatchOperation{ - Op: aws.String("replace"), + Op: aws.String(apigateway.OpReplace), Path: aws.String(prefix + "caching/requireAuthorizationForCacheControl"), Value: aws.String(fmt.Sprintf("%t", d.Get("settings.0.require_authorization_for_cache_control").(bool))), }) } if d.HasChange("settings.0.unauthorized_cache_control_header_strategy") { ops = append(ops, &apigateway.PatchOperation{ - Op: aws.String("replace"), + Op: aws.String(apigateway.OpReplace), Path: aws.String(prefix + "caching/unauthorizedCacheControlHeaderStrategy"), Value: aws.String(d.Get("settings.0.unauthorized_cache_control_header_strategy").(string)), }) @@ -233,7 +243,7 @@ func resourceAwsApiGatewayMethodSettingsDelete(d *schema.ResourceData, meta inte StageName: aws.String(d.Get("stage_name").(string)), PatchOperations: []*apigateway.PatchOperation{ { - Op: aws.String("remove"), + Op: aws.String(apigateway.OpRemove), Path: aws.String(fmt.Sprintf("/%s", d.Get("method_path").(string))), }, }, diff --git a/aws/resource_aws_api_gateway_method_settings_test.go b/aws/resource_aws_api_gateway_method_settings_test.go index a0040e0acd5..98b1d6bfe73 100644 --- a/aws/resource_aws_api_gateway_method_settings_test.go +++ b/aws/resource_aws_api_gateway_method_settings_test.go @@ -74,6 +74,14 @@ func TestAccAWSAPIGatewayMethodSettings_Settings_CacheTtlInSeconds(t *testing.T) Providers: testAccProviders, CheckDestroy: testAccCheckAWSAPIGatewayMethodSettingsDestroy, Steps: []resource.TestStep{ + { + Config: testAccAWSAPIGatewayMethodSettingsConfigSettingsCacheTtlInSeconds(rName, 0), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSAPIGatewayMethodSettingsExists(resourceName, &stage1), + resource.TestCheckResourceAttr(resourceName, "settings.#", "1"), + resource.TestCheckResourceAttr(resourceName, "settings.0.cache_ttl_in_seconds", "0"), + ), + }, { Config: testAccAWSAPIGatewayMethodSettingsConfigSettingsCacheTtlInSeconds(rName, 1), Check: resource.ComposeTestCheckFunc( From 4210d46e49c865b35a3261f93eca7bc79777ecea Mon Sep 17 00:00:00 2001 From: DrFaust92 Date: Fri, 3 Apr 2020 11:54:40 +0300 Subject: [PATCH 2/3] fix zero ttl --- aws/resource_aws_api_gateway_method_settings.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/aws/resource_aws_api_gateway_method_settings.go b/aws/resource_aws_api_gateway_method_settings.go index b5d27e38bf7..23b8e60f6c0 100644 --- a/aws/resource_aws_api_gateway_method_settings.go +++ b/aws/resource_aws_api_gateway_method_settings.go @@ -188,11 +188,13 @@ func resourceAwsApiGatewayMethodSettingsUpdate(d *schema.ResourceData, meta inte }) } - ops = append(ops, &apigateway.PatchOperation{ - Op: aws.String(apigateway.OpReplace), - Path: aws.String(prefix + "caching/ttlInSeconds"), - Value: aws.String(fmt.Sprintf("%d", d.Get("settings.0.cache_ttl_in_seconds").(int))), - }) + if v, ok := d.GetOkExists("settings.0.cache_ttl_in_seconds"); ok { + ops = append(ops, &apigateway.PatchOperation{ + Op: aws.String(apigateway.OpReplace), + Path: aws.String(prefix + "caching/ttlInSeconds"), + Value: aws.String(fmt.Sprintf("%d", v.(int))), + }) + } if d.HasChange("settings.0.cache_data_encrypted") { ops = append(ops, &apigateway.PatchOperation{ From c7597e67288b070be5102db3967e027cea38314b Mon Sep 17 00:00:00 2001 From: DrFaust92 Date: Sat, 25 Jul 2020 00:53:45 +0300 Subject: [PATCH 3/3] fix lint --- aws/resource_aws_api_gateway_method_settings.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/aws/resource_aws_api_gateway_method_settings.go b/aws/resource_aws_api_gateway_method_settings.go index f2910159f91..b8fbbed4eac 100644 --- a/aws/resource_aws_api_gateway_method_settings.go +++ b/aws/resource_aws_api_gateway_method_settings.go @@ -57,7 +57,7 @@ func resourceAwsApiGatewayMethodSettings() *schema.Resource { "OFF", "ERROR", "INFO", - }, false), + }, false), }, "data_trace_enabled": { Type: schema.TypeBool, @@ -156,7 +156,7 @@ func resourceAwsApiGatewayMethodSettingsRead(d *schema.ResourceData, meta interf } if err := d.Set("settings", flattenAwsApiGatewayMethodSettings(settings)); err != nil { - return fmt.Errorf("error setting settings: %s", err) + return fmt.Errorf("error setting settings: %w", err) } return nil @@ -253,7 +253,7 @@ func resourceAwsApiGatewayMethodSettingsUpdate(d *schema.ResourceData, meta inte log.Printf("[DEBUG] Updating API Gateway Stage: %s", input) _, err := conn.UpdateStage(&input) if err != nil { - return fmt.Errorf("Updating API Gateway Stage failed: %s", err) + return fmt.Errorf("updating API Gateway Stage failed: %w", err) } d.SetId(restApiId + "-" + stageName + "-" + methodPath) @@ -278,7 +278,7 @@ func resourceAwsApiGatewayMethodSettingsDelete(d *schema.ResourceData, meta inte log.Printf("[DEBUG] Updating API Gateway Stage: %s", input) _, err := conn.UpdateStage(&input) if err != nil { - return fmt.Errorf("Updating API Gateway Stage failed: %s", err) + return fmt.Errorf("updating API Gateway Stage failed: %w", err) } return nil