Skip to content

Commit

Permalink
Merge pull request #17261 from shuheiktgw/add_sweepers_to_elasticache
Browse files Browse the repository at this point in the history
  • Loading branch information
gdavison committed Feb 19, 2021
2 parents b4f2c19 + d66a405 commit bb0ac71
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 10 deletions.
65 changes: 58 additions & 7 deletions aws/resource_aws_elasticache_parameter_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,72 @@ package aws

import (
"fmt"
"log"
"reflect"
"strings"
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/elasticache"
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
"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/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func init() {
resource.AddTestSweepers("aws_elasticache_parameter_group", &resource.Sweeper{
Name: "aws_elasticache_parameter_group",
F: testSweepElasticacheParameterGroups,
Dependencies: []string{
"aws_elasticache_cluster",
"aws_elasticache_replication_group",
},
})
}

func testSweepElasticacheParameterGroups(region string) error {
client, err := sharedClientForRegion(region)
if err != nil {
return fmt.Errorf("error getting client: %w", err)
}
conn := client.(*AWSClient).elasticacheconn

err = conn.DescribeCacheParameterGroupsPages(&elasticache.DescribeCacheParameterGroupsInput{}, func(page *elasticache.DescribeCacheParameterGroupsOutput, isLast bool) bool {
if len(page.CacheParameterGroups) == 0 {
log.Print("[DEBUG] No Elasticache Parameter Groups to sweep")
return false
}

for _, parameterGroup := range page.CacheParameterGroups {
name := aws.StringValue(parameterGroup.CacheParameterGroupName)

if strings.HasPrefix(name, "default.") {
log.Printf("[INFO] Skipping Elasticache Cache Parameter Group: %s", name)
continue
}

log.Printf("[INFO] Deleting Elasticache Parameter Group: %s", name)
_, err := conn.DeleteCacheParameterGroup(&elasticache.DeleteCacheParameterGroupInput{
CacheParameterGroupName: aws.String(name),
})
if err != nil {
log.Printf("[ERROR] Failed to delete Elasticache Parameter Group (%s): %s", name, err)
}
}
return !isLast
})
if err != nil {
if testSweepSkipSweepError(err) {
log.Printf("[WARN] Skipping Elasticache Parameter Group sweep for %s: %s", region, err)
return nil
}
return fmt.Errorf("Error retrieving Elasticache Parameter Group: %w", err)
}
return nil
}

func TestAccAWSElasticacheParameterGroup_basic(t *testing.T) {
var v elasticache.CacheParameterGroup
resourceName := "aws_elasticache_parameter_group.test"
Expand Down Expand Up @@ -358,7 +412,6 @@ func testAccCheckAWSElasticacheParameterGroupDestroy(s *terraform.State) error {
continue
}

// Try to find the Group
resp, err := conn.DescribeCacheParameterGroups(
&elasticache.DescribeCacheParameterGroupsInput{
CacheParameterGroupName: aws.String(rs.Primary.ID),
Expand All @@ -371,12 +424,10 @@ func testAccCheckAWSElasticacheParameterGroupDestroy(s *terraform.State) error {
}
}

// Verify the error
newerr, ok := err.(awserr.Error)
if !ok {
return err
if tfawserr.ErrCodeEquals(err, elasticache.ErrCodeCacheParameterGroupNotFoundFault) {
return nil
}
if newerr.Code() != "CacheParameterGroupNotFound" {
if err != nil {
return err
}
}
Expand Down
54 changes: 51 additions & 3 deletions aws/resource_aws_elasticache_subnet_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,64 @@ package aws

import (
"fmt"
"log"
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/elasticache"
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
"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"
)

func init() {
resource.AddTestSweepers("aws_elasticache_subnet_group", &resource.Sweeper{
Name: "aws_elasticache_subnet_group",
F: testSweepElasticacheSubnetGroups,
Dependencies: []string{
"aws_elasticache_cluster",
"aws_elasticache_replication_group",
},
})
}

func testSweepElasticacheSubnetGroups(region string) error {
client, err := sharedClientForRegion(region)
if err != nil {
return fmt.Errorf("error getting client: %w", err)
}
conn := client.(*AWSClient).elasticacheconn

err = conn.DescribeCacheSubnetGroupsPages(&elasticache.DescribeCacheSubnetGroupsInput{}, func(page *elasticache.DescribeCacheSubnetGroupsOutput, isLast bool) bool {
if len(page.CacheSubnetGroups) == 0 {
log.Print("[DEBUG] No Elasticache Subnet Groups to sweep")
return false
}

for _, subnetGroup := range page.CacheSubnetGroups {
name := aws.StringValue(subnetGroup.CacheSubnetGroupName)

log.Printf("[INFO] Deleting Elasticache Subnet Group: %s", name)
_, err := conn.DeleteCacheSubnetGroup(&elasticache.DeleteCacheSubnetGroupInput{
CacheSubnetGroupName: aws.String(name),
})
if err != nil {
log.Printf("[ERROR] Failed to delete Elasticache Subnet Group (%s): %s", name, err)
}
}
return !isLast
})
if err != nil {
if testSweepSkipSweepError(err) {
log.Printf("[WARN] Skipping Elasticache Subnet Group sweep for %s: %s", region, err)
return nil
}
return fmt.Errorf("Error retrieving Elasticache Subnet Groups: %w", err)
}
return nil
}

func TestAccAWSElasticacheSubnetGroup_basic(t *testing.T) {
var csg elasticache.CacheSubnetGroup
resourceName := "aws_elasticache_subnet_group.test"
Expand Down Expand Up @@ -87,7 +135,7 @@ func testAccCheckAWSElasticacheSubnetGroupDestroy(s *terraform.State) error {
})
if err != nil {
// Verify the error is what we want
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "CacheSubnetGroupNotFoundFault" {
if tfawserr.ErrCodeEquals(err, elasticache.ErrCodeCacheSubnetGroupNotFoundFault) {
continue
}
return err
Expand Down Expand Up @@ -115,7 +163,7 @@ func testAccCheckAWSElasticacheSubnetGroupExists(n string, csg *elasticache.Cach
CacheSubnetGroupName: aws.String(rs.Primary.ID),
})
if err != nil {
return fmt.Errorf("CacheSubnetGroup error: %v", err)
return fmt.Errorf("CacheSubnetGroup error: %w", err)
}

for _, c := range resp.CacheSubnetGroups {
Expand Down

0 comments on commit bb0ac71

Please sign in to comment.