Skip to content

Commit

Permalink
resource/aws_appautoscaling_target: Remove DeregisterScalableTarget r…
Browse files Browse the repository at this point in the history
…etries on all errors and add disappears test

Reference: #13409
Reference: #13826

Output from acceptance testing:

```
--- PASS: TestAccAWSAppautoScalingTarget_multipleTargets (20.68s)
--- PASS: TestAccAWSAppautoScalingTarget_optionalRoleArn (25.17s)
--- PASS: TestAccAWSAppautoScalingTarget_basic (43.13s)
--- PASS: TestAccAWSAppautoScalingTarget_spotFleetRequest (57.42s)
--- PASS: TestAccAWSAppautoScalingTarget_disappears (71.79s)
--- PASS: TestAccAWSAppautoScalingTarget_emrCluster (840.33s)

--- PASS: TestAccAWSAppautoScalingPolicy_multiplePoliciesSameName (24.97s)
--- PASS: TestAccAWSAppautoScalingPolicy_dynamodb_table (26.58s)
--- PASS: TestAccAWSAppautoScalingPolicy_multiplePoliciesSameResource (28.13s)
--- PASS: TestAccAWSAppautoScalingPolicy_dynamodb_index (37.07s)
--- PASS: TestAccAWSAppautoScalingPolicy_spotFleetRequest (71.64s)
--- PASS: TestAccAWSAppautoScalingPolicy_disappears (75.45s)
--- PASS: TestAccAWSAppautoScalingPolicy_basic (77.30s)
--- PASS: TestAccAWSAppautoScalingPolicy_scaleOutAndIn (79.17s)
--- PASS: TestAccAWSAppautoScalingPolicy_ResourceId_ForceNew (83.72s)
```
  • Loading branch information
bflad committed Jul 21, 2020
1 parent 2228798 commit 341af8b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 35 deletions.
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

0 comments on commit 341af8b

Please sign in to comment.