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

resource/aws_appautoscaling_target: Remove DeregisterScalableTarget retries on all errors and add disappears test #14259

Merged
merged 1 commit into from
Jul 27, 2020
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
51 changes: 16 additions & 35 deletions aws/resource_aws_appautoscaling_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ package aws
import (
"fmt"
"log"
"strings"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/applicationautoscaling"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func resourceAwsAppautoscalingTarget() *schema.Resource {
Expand Down Expand Up @@ -148,46 +146,29 @@ func resourceAwsAppautoscalingTargetRead(d *schema.ResourceData, meta interface{
func resourceAwsAppautoscalingTargetDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).appautoscalingconn

namespace := d.Get("service_namespace").(string)
dimension := d.Get("scalable_dimension").(string)

t, err := getAwsAppautoscalingTarget(d.Id(), namespace, dimension, conn)
if err != nil {
return err
}
if t == nil {
log.Printf("[INFO] Application AutoScaling Target %q not found", d.Id())
return nil
}

log.Printf("[DEBUG] Application AutoScaling Target destroy: %#v", d.Id())
deleteOpts := applicationautoscaling.DeregisterScalableTargetInput{
input := &applicationautoscaling.DeregisterScalableTargetInput{
ResourceId: aws.String(d.Get("resource_id").(string)),
ServiceNamespace: aws.String(d.Get("service_namespace").(string)),
ScalableDimension: aws.String(d.Get("scalable_dimension").(string)),
}

err = resource.Retry(5*time.Minute, func() *resource.RetryError {
if _, err := conn.DeregisterScalableTarget(&deleteOpts); err != nil {
if awserr, ok := err.(awserr.Error); ok {
// @TODO: We should do stuff here depending on the actual error returned
return resource.RetryableError(awserr)
}
// Non recognized error, no retry.
return resource.NonRetryableError(err)
}
// Successful delete
return nil
})
_, err := conn.DeregisterScalableTarget(input)

if err != nil {
return err
return fmt.Errorf("error deleting Application AutoScaling Target (%s): %w", d.Id(), err)
}

return resource.Retry(5*time.Minute, func() *resource.RetryError {
if t, _ = getAwsAppautoscalingTarget(d.Id(), namespace, dimension, conn); t != nil {
return resource.RetryableError(
fmt.Errorf("Application AutoScaling Target still exists"))
t, err := getAwsAppautoscalingTarget(d.Get("resource_id").(string), d.Get("service_namespace").(string), d.Get("scalable_dimension").(string), conn)

if err != nil {
return resource.NonRetryableError(err)
}

if t != nil {
return resource.RetryableError(fmt.Errorf("Application AutoScaling Target (%s) still exists", d.Id()))
}

return nil
})
}
Expand Down
22 changes: 22 additions & 0 deletions aws/resource_aws_appautoscaling_target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,28 @@ func TestAccAWSAppautoScalingTarget_basic(t *testing.T) {
})
}

func TestAccAWSAppautoScalingTarget_disappears(t *testing.T) {
var target applicationautoscaling.ScalableTarget
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_appautoscaling_target.bar"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSAppautoscalingTargetDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSAppautoscalingTargetConfig(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAppautoscalingTargetExists(resourceName, &target),
testAccCheckResourceDisappears(testAccProvider, resourceAwsAppautoscalingTarget(), resourceName),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func TestAccAWSAppautoScalingTarget_spotFleetRequest(t *testing.T) {
var target applicationautoscaling.ScalableTarget
validUntil := time.Now().UTC().Add(24 * time.Hour).Format(time.RFC3339)
Expand Down