Skip to content

Commit

Permalink
Merge pull request #17650 from hashicorp/elasticache-cluster-sweeper
Browse files Browse the repository at this point in the history
Sweeper: exclude replication group member clusters from aws_elasticache_cluster sweeper
  • Loading branch information
gdavison authored Feb 17, 2021
2 parents 1aacbc5 + 5f67538 commit 6c23c96
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
9 changes: 6 additions & 3 deletions aws/resource_aws_elasticache_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/elasticache"
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
gversion "github.com/hashicorp/go-version"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
Expand Down Expand Up @@ -695,12 +696,14 @@ func deleteElasticacheCacheCluster(conn *elasticache.ElastiCache, cacheClusterID
err := resource.Retry(5*time.Minute, func() *resource.RetryError {
_, err := conn.DeleteCacheCluster(input)
if err != nil {
// This will not be fixed by retrying
if isAWSErr(err, elasticache.ErrCodeInvalidCacheClusterStateFault, "serving as primary") {
if tfawserr.ErrMessageContains(err, elasticache.ErrCodeInvalidCacheClusterStateFault, "serving as primary") {
return resource.NonRetryableError(err)
}
if tfawserr.ErrMessageContains(err, elasticache.ErrCodeInvalidCacheClusterStateFault, "only member of a replication group") {
return resource.NonRetryableError(err)
}
// The cluster may be just snapshotting, so we retry until it's ready for deletion
if isAWSErr(err, elasticache.ErrCodeInvalidCacheClusterStateFault, "") {
if tfawserr.ErrCodeEquals(err, elasticache.ErrCodeInvalidCacheClusterStateFault) {
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
Expand Down
23 changes: 16 additions & 7 deletions aws/resource_aws_elasticache_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/elasticache"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
Expand All @@ -36,7 +37,12 @@ func testSweepElasticacheClusters(region string) error {
}
conn := client.(*AWSClient).elasticacheconn

err = conn.DescribeCacheClustersPages(&elasticache.DescribeCacheClustersInput{}, func(page *elasticache.DescribeCacheClustersOutput, isLast bool) bool {
var sweeperErrs *multierror.Error

input := &elasticache.DescribeCacheClustersInput{
ShowCacheClustersNotInReplicationGroups: aws.Bool(true),
}
err = conn.DescribeCacheClustersPages(input, func(page *elasticache.DescribeCacheClustersOutput, isLast bool) bool {
if len(page.CacheClusters) == 0 {
log.Print("[DEBUG] No ElastiCache Replicaton Groups to sweep")
return false
Expand All @@ -49,22 +55,25 @@ func testSweepElasticacheClusters(region string) error {
err := deleteElasticacheCacheCluster(conn, id, "")
if err != nil {
log.Printf("[ERROR] Failed to delete ElastiCache Cache Cluster (%s): %s", id, err)
sweeperErrs = multierror.Append(sweeperErrs, fmt.Errorf("error deleting ElastiCache Cache Cluster (%s): %w", id, err))
}
_, err = waiter.CacheClusterDeleted(conn, id, waiter.CacheClusterDeletedTimeout)
if err != nil {
log.Printf("[ERROR] Failed waiting for ElastiCache Cache Cluster (%s) to be deleted: %s", id, err)
sweeperErrs = multierror.Append(sweeperErrs, fmt.Errorf("error deleting ElastiCache Cache Cluster (%s): waiting for completion: %w", id, err))
}
}
return !isLast
})
if testSweepSkipSweepError(err) {
log.Printf("[WARN] Skipping ElastiCache Cluster sweep for %s: %s", region, err)
return sweeperErrs.ErrorOrNil() // In case we have completed some pages, but had errors
}
if err != nil {
if testSweepSkipSweepError(err) {
log.Printf("[WARN] Skipping ElastiCache Cluster sweep for %s: %s", region, err)
return nil
}
return fmt.Errorf("Error retrieving ElastiCache Clusters: %s", err)
sweeperErrs = multierror.Append(sweeperErrs, fmt.Errorf("Error retrieving ElastiCache Clusters: %w", err))
}
return nil

return sweeperErrs.ErrorOrNil()
}

func TestAccAWSElasticacheCluster_Engine_Memcached(t *testing.T) {
Expand Down

0 comments on commit 6c23c96

Please sign in to comment.