Skip to content

Commit

Permalink
Merge pull request #3351 from terraform-providers/t-aws_dynamodb_tabl…
Browse files Browse the repository at this point in the history
…e-add-sweeper

test/resource/aws_dynamodb_table: Add sweeper
  • Loading branch information
bflad authored Feb 13, 2018
2 parents dfe0d6a + 049894c commit 49ea590
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions aws/resource_aws_dynamodb_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"fmt"
"log"
"regexp"
"strings"
"testing"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
Expand All @@ -14,6 +16,64 @@ import (
"github.com/hashicorp/terraform/terraform"
)

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

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

prefixes := []string{
"TerraformTest",
"tf-acc-test-",
"tf-autoscaled-table-",
}

err = conn.ListTablesPages(&dynamodb.ListTablesInput{}, func(out *dynamodb.ListTablesOutput, lastPage bool) bool {
for _, tableName := range out.TableNames {
for _, prefix := range prefixes {
if !strings.HasPrefix(*tableName, prefix) {
log.Printf("[INFO] Skipping DynamoDB Table: %s", *tableName)
continue
}
}
log.Printf("[INFO] Deleting DynamoDB Table: %s", *tableName)

input := &dynamodb.DeleteTableInput{
TableName: tableName,
}
err := resource.Retry(1*time.Minute, func() *resource.RetryError {
_, err := conn.DeleteTable(input)
if err != nil {
// Subscriber limit exceeded: Only 10 tables can be created, updated, or deleted simultaneously
if isAWSErr(err, dynamodb.ErrCodeLimitExceededException, "simultaneously") {
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
})
if err != nil {
log.Printf("[ERROR] Failed to delete DynamoDB Table %s: %s", *tableName, err)
continue
}
}
return !lastPage
})
if err != nil {
return fmt.Errorf("Error retrieving DynamoDB Tables: %s", err)
}

return nil
}

func TestDiffDynamoDbGSI(t *testing.T) {
testCases := []struct {
Old []interface{}
Expand Down

0 comments on commit 49ea590

Please sign in to comment.