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

r/aws_db_cluster_snapshot: Add test sweeper #13301

Merged
merged 2 commits into from
May 20, 2020
Merged
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
62 changes: 62 additions & 0 deletions aws/resource_aws_db_cluster_snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,78 @@ package aws

import (
"fmt"
"log"
"regexp"
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/rds"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
)

func init() {
resource.AddTestSweepers("aws_db_cluster_snapshot", &resource.Sweeper{
Name: "aws_db_cluster_snapshot",
F: testSweepDbClusterSnapshots,
})
}

func testSweepDbClusterSnapshots(region string) error {
client, err := sharedClientForRegion(region)
if err != nil {
return fmt.Errorf("error getting client: %w", err)
}
conn := client.(*AWSClient).rdsconn
input := &rds.DescribeDBClusterSnapshotsInput{
// "InvalidDBClusterSnapshotStateFault: Only manual snapshots may be deleted."
Filters: []*rds.Filter{{
Name: aws.String("snapshot-type"),
Values: aws.StringSlice([]string{"manual"}),
}},
}
var sweeperErrs *multierror.Error

for {
output, err := conn.DescribeDBClusterSnapshots(input)
if testSweepSkipSweepError(err) {
log.Printf("[WARN] Skipping RDS DB Cluster Snapshots sweep for %s: %s", region, err)
return sweeperErrs.ErrorOrNil() // In case we have completed some pages, but had errors
}
if err != nil {
sweeperErrs = multierror.Append(sweeperErrs, fmt.Errorf("error retrieving RDS DB Cluster Snapshots: %w", err))
return sweeperErrs
}

for _, dbClusterSnapshot := range output.DBClusterSnapshots {
id := aws.StringValue(dbClusterSnapshot.DBClusterSnapshotIdentifier)

log.Printf("[INFO] Deleting RDS DB Cluster Snapshot: %s", id)
_, err := conn.DeleteDBClusterSnapshot(&rds.DeleteDBClusterSnapshotInput{
DBClusterSnapshotIdentifier: aws.String(id),
})
if isAWSErr(err, rds.ErrCodeDBClusterSnapshotNotFoundFault, "") {
continue
}
if err != nil {
sweeperErr := fmt.Errorf("error deleting RDS DB Cluster Snapshot (%s): %w", id, err)
log.Printf("[ERROR] %s", sweeperErr)
sweeperErrs = multierror.Append(sweeperErrs, sweeperErr)
continue
}
}

if aws.StringValue(output.Marker) == "" {
break
}
input.Marker = output.Marker
}

return sweeperErrs.ErrorOrNil()
}

func TestAccAWSDBClusterSnapshot_basic(t *testing.T) {
var dbClusterSnapshot rds.DBClusterSnapshot
rName := acctest.RandomWithPrefix("tf-acc-test")
Expand Down