Skip to content

Commit

Permalink
Merge pull request #12651 from DrFaust92/r/api_gateway_method_setting…
Browse files Browse the repository at this point in the history
…s_ttl

r/api_gateway_method_settings: allow zero ttl
  • Loading branch information
anGie44 authored Jul 31, 2020
2 parents ab52636 + c7597e6 commit d5d267b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
23 changes: 18 additions & 5 deletions aws/resource_aws_api_gateway_method_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/aws/aws-sdk-go/aws"
"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 {
Expand Down Expand Up @@ -52,6 +53,11 @@ func resourceAwsApiGatewayMethodSettings() *schema.Resource {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringInSlice([]string{
"OFF",
"ERROR",
"INFO",
}, false),
},
"data_trace_enabled": {
Type: schema.TypeBool,
Expand Down Expand Up @@ -91,6 +97,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),
Computed: true,
},
},
Expand Down Expand Up @@ -145,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
Expand Down Expand Up @@ -201,13 +212,15 @@ func resourceAwsApiGatewayMethodSettingsUpdate(d *schema.ResourceData, meta inte
Value: aws.String(fmt.Sprintf("%t", d.Get("settings.0.caching_enabled").(bool))),
})
}
if d.HasChange("settings.0.cache_ttl_in_seconds") {

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", d.Get("settings.0.cache_ttl_in_seconds").(int))),
Value: aws.String(fmt.Sprintf("%d", v.(int))),
})
}

if d.HasChange("settings.0.cache_data_encrypted") {
ops = append(ops, &apigateway.PatchOperation{
Op: aws.String(apigateway.OpReplace),
Expand Down Expand Up @@ -240,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)
Expand All @@ -265,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
Expand Down
8 changes: 8 additions & 0 deletions aws/resource_aws_api_gateway_method_settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,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(
Expand Down

0 comments on commit d5d267b

Please sign in to comment.