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

Adds validation to API Gateway Stage cache configuration #22861

Closed
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
16 changes: 14 additions & 2 deletions internal/service/apigateway/stage.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package apigateway

import (
"context"
"fmt"
"log"
"strings"
Expand All @@ -9,6 +10,7 @@ import (
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/apigateway"
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
Expand Down Expand Up @@ -122,10 +124,9 @@ func ResourceStage() *schema.Resource {
},
},

CustomizeDiff: verify.SetTagsDiff,
CustomizeDiff: customdiff.All(verify.SetTagsDiff, validateCacheClusterConfiguration),
}
}

func resourceStageCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).APIGatewayConn
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
Expand Down Expand Up @@ -451,3 +452,14 @@ func flattenApiGatewayStageAccessLogSettings(accessLogSettings *apigateway.Acces
}
return result
}

func validateCacheClusterConfiguration(ctx context.Context, diff *schema.ResourceDiff, meta interface{}) error {
cacheSize, cacheSizeSet := diff.GetOk("cache_cluster_size")
_, cacheEnabledSet := diff.GetOk("cache_cluster_enabled")

if cacheSizeSet && !cacheEnabledSet {
return fmt.Errorf("error with API Gateway Stage: cache_cluster_size is set to %s but cache cluster is not enabled for this stage. Unset cache_cluster_size or set cache_cluster_enabled to true.", cacheSize.(string))
}

return nil
}
17 changes: 11 additions & 6 deletions internal/service/apigateway/stage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func TestAccAPIGatewayStage_cache(t *testing.T) {
ImportStateVerify: true,
},
{
Config: testAccStageConfigCacheConfig(rName, "0.5"),
Config: testAccStageConfigCacheConfig(rName, true, "0.5"),
Check: resource.ComposeTestCheckFunc(
testAccCheckStageExists(resourceName, &conf),
resource.TestCheckResourceAttr(resourceName, "cache_cluster_size", "0.5"),
Expand All @@ -112,13 +112,17 @@ func TestAccAPIGatewayStage_cache(t *testing.T) {
},

{
Config: testAccStageConfigCacheConfig(rName, "1.6"),
Config: testAccStageConfigCacheConfig(rName, true, "1.6"),
Check: resource.ComposeTestCheckFunc(
testAccCheckStageExists(resourceName, &conf),
resource.TestCheckResourceAttr(resourceName, "cache_cluster_size", "1.6"),
resource.TestCheckResourceAttr(resourceName, "cache_cluster_enabled", "true"),
),
},
{
Config: testAccStageConfigCacheConfig(rName, false, "13.5"),
ExpectError: regexp.MustCompile("^error with API Gateway Stage: cache_cluster_size is set to 13.5 but cache cluster is not enabled for this stage. Unset cache_cluster_size or set cache_cluster_enabled to true.$"),
},
{
Config: testAccStageConfigBasic(rName),
Check: resource.ComposeTestCheckFunc(
Expand Down Expand Up @@ -644,16 +648,17 @@ resource "aws_api_gateway_stage" "test" {
`
}

func testAccStageConfigCacheConfig(rName, size string) string {
func testAccStageConfigCacheConfig(rName string, enabled bool, size string) string {
return testAccStageConfig_base(rName) + fmt.Sprintf(`
resource "aws_api_gateway_stage" "test" {
rest_api_id = aws_api_gateway_rest_api.test.id
stage_name = "prod"
deployment_id = aws_api_gateway_deployment.dev.id
cache_cluster_enabled = true
cache_cluster_size = %[1]q
cache_cluster_enabled = %[1]t
cache_cluster_size = %[2]q
}
`, size)
`, enabled, size)

}

func testAccStageConfig_accessLogSettings(rName string, format string) string {
Expand Down