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

fix apigw cache waiting logic when cache size provided but not cache enabled #23091

Merged
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
3 changes: 3 additions & 0 deletions .changelog/23091.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_api_gateway_stage: Fixed issue with providing `cache_cluster_size` without `cache_cluster_enabled` resulted in waiter error
```
26 changes: 15 additions & 11 deletions internal/service/apigateway/stage.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func resourceStageCreate(d *schema.ResourceData, meta interface{}) error {

respApiId := d.Get("rest_api_id").(string)
stageName := d.Get("stage_name").(string)
input := apigateway.CreateStageInput{
input := &apigateway.CreateStageInput{
RestApiId: aws.String(respApiId),
StageName: aws.String(stageName),
DeploymentId: aws.String(d.Get("deployment_id").(string)),
Expand All @@ -151,27 +151,29 @@ func resourceStageCreate(d *schema.ResourceData, meta interface{}) error {
if v, ok := d.GetOk("description"); ok {
input.Description = aws.String(v.(string))
}
if v, ok := d.GetOk("xray_tracing_enabled"); ok {
input.TracingEnabled = aws.Bool(v.(bool))
}
if v, ok := d.GetOk("documentation_version"); ok {
input.DocumentationVersion = aws.String(v.(string))
}
if vars, ok := d.GetOk("variables"); ok {
input.Variables = flex.ExpandStringMap(vars.(map[string]interface{}))
}
if v, ok := d.GetOk("xray_tracing_enabled"); ok {
input.TracingEnabled = aws.Bool(v.(bool))
}

if len(tags) > 0 {
input.Tags = Tags(tags.IgnoreAWS())
}

_, err := conn.CreateStage(&input)
output, err := conn.CreateStage(input)

if err != nil {
return fmt.Errorf("Error creating API Gateway Stage: %s", err)
return fmt.Errorf("error creating API Gateway Stage (%s): %w", stageName, err)
}

d.SetId(fmt.Sprintf("ags-%s-%s", respApiId, stageName))

if waitForCache {
if waitForCache && aws.StringValue(output.CacheClusterStatus) != apigateway.CacheClusterStatusNotAvailable {
_, err := waitStageCacheAvailable(conn, respApiId, stageName)
if err != nil {
return fmt.Errorf("error waiting for API Gateway Stage (%s) to be available: %w", d.Id(), err)
Expand Down Expand Up @@ -367,18 +369,20 @@ func resourceStageUpdate(d *schema.ResourceData, meta interface{}) error {
}
}

input := apigateway.UpdateStageInput{
input := &apigateway.UpdateStageInput{
RestApiId: aws.String(respApiId),
StageName: aws.String(stageName),
PatchOperations: operations,
}

log.Printf("[DEBUG] Updating API Gateway Stage: %s", input)
_, err := conn.UpdateStage(&input)
output, err := conn.UpdateStage(input)

if err != nil {
return fmt.Errorf("Updating API Gateway Stage failed: %w", err)
return fmt.Errorf("error updating API Gateway Stage (%s): %w", d.Id(), err)
}

if waitForCache {
if waitForCache && aws.StringValue(output.CacheClusterStatus) != apigateway.CacheClusterStatusNotAvailable {
_, err := waitStageCacheUpdated(conn, respApiId, stageName)
if err != nil {
return fmt.Errorf("error waiting for API Gateway Stage (%s) to be updated: %w", d.Id(), err)
Expand Down
56 changes: 56 additions & 0 deletions internal/service/apigateway/stage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,51 @@ func TestAccAPIGatewayStage_cache(t *testing.T) {
})
}

// Reference: https://github.com/hashicorp/terraform-provider-aws/issues/22866
func TestAccAPIGatewayStage_cache_size_cache_disabled(t *testing.T) {
var conf apigateway.Stage
rName := sdkacctest.RandString(5)
resourceName := "aws_api_gateway_stage.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t); acctest.PreCheckAPIGatewayTypeEDGE(t) },
ErrorCheck: acctest.ErrorCheck(t, apigateway.EndpointsID),
Providers: acctest.Providers,
CheckDestroy: testAccCheckStageDestroy,
Steps: []resource.TestStep{
{
Config: testAccStageConfigBasic(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckStageExists(resourceName, &conf),
resource.TestCheckResourceAttr(resourceName, "cache_cluster_enabled", "false"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateIdFunc: testAccStageImportStateIdFunc(resourceName),
ImportStateVerify: true,
},
{
Config: testAccStageConfigCacheSizeCacheDisabled(rName, "0.5"),
Check: resource.ComposeTestCheckFunc(
testAccCheckStageExists(resourceName, &conf),
resource.TestCheckResourceAttr(resourceName, "cache_cluster_size", "0.5"),
resource.TestCheckResourceAttr(resourceName, "cache_cluster_enabled", "false"),
),
},
{
Config: testAccStageConfigCacheConfig(rName, "0.5"),
Check: resource.ComposeTestCheckFunc(
testAccCheckStageExists(resourceName, &conf),
resource.TestCheckResourceAttr(resourceName, "cache_cluster_size", "0.5"),
resource.TestCheckResourceAttr(resourceName, "cache_cluster_enabled", "true"),
),
},
},
})
}

// Reference: https://github.com/hashicorp/terraform-provider-aws/issues/12756
func TestAccAPIGatewayStage_Disappears_referencingDeployment(t *testing.T) {
var stage apigateway.Stage
Expand Down Expand Up @@ -644,6 +689,17 @@ resource "aws_api_gateway_stage" "test" {
`
}

func testAccStageConfigCacheSizeCacheDisabled(rName, 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_size = %[1]q
}
`, size)
}

func testAccStageConfigCacheConfig(rName, size string) string {
return testAccStageConfig_base(rName) + fmt.Sprintf(`
resource "aws_api_gateway_stage" "test" {
Expand Down