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

service/s3: Add sweepers #7398

Merged
merged 1 commit into from
Jan 31, 2019
Merged
Show file tree
Hide file tree
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
134 changes: 131 additions & 3 deletions aws/resource_aws_s3_bucket_object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,147 @@ import (
"encoding/base64"
"fmt"
"io/ioutil"
"log"
"os"
"reflect"
"sort"
"strings"
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
)

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

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

conn := client.(*AWSClient).s3conn
input := &s3.ListBucketsInput{}

output, err := conn.ListBuckets(input)

if testSweepSkipSweepError(err) {
log.Printf("[WARN] Skipping S3 Bucket Objects sweep for %s: %s", region, err)
return nil
}

if err != nil {
return fmt.Errorf("error listing S3 Bucket Objects: %s", err)
}

if len(output.Buckets) == 0 {
log.Print("[DEBUG] No S3 Bucket Objects to sweep")
return nil
}

for _, bucket := range output.Buckets {
bucketName := aws.StringValue(bucket.Name)

if !strings.HasPrefix(bucketName, "tf-acc") && !strings.HasPrefix(bucketName, "tf-object-test") && !strings.HasPrefix(bucketName, "tf-test") {
log.Printf("[INFO] Skipping S3 Bucket: %s", bucketName)
continue
}

input := &s3.ListObjectVersionsInput{
Bucket: bucket.Name,
}

err := conn.ListObjectVersionsPages(input, func(page *s3.ListObjectVersionsOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

for _, objectVersion := range page.Versions {
input := &s3.DeleteObjectInput{
Bucket: bucket.Name,
Key: objectVersion.Key,
VersionId: objectVersion.VersionId,
}
objectKey := aws.StringValue(objectVersion.Key)
objectVersionID := aws.StringValue(objectVersion.VersionId)

log.Printf("[INFO] Deleting S3 Bucket (%s) Object (%s) Version: %s", bucketName, objectKey, objectVersionID)
_, err := conn.DeleteObject(input)

if isAWSErr(err, s3.ErrCodeNoSuchBucket, "") || isAWSErr(err, s3.ErrCodeNoSuchKey, "") {
continue
}

if err != nil {
log.Printf("[ERROR] Error deleting S3 Bucket (%s) Object (%s) Version (%s): %s", bucketName, objectKey, objectVersionID, err)
}
}

return !lastPage
})

if isAWSErr(err, s3.ErrCodeNoSuchBucket, "") {
continue
}

if isAWSErr(err, "BucketRegionError", "") {
log.Printf("[INFO] Skipping S3 Bucket (%s) Objects: %s", bucketName, err)
continue
}

if err != nil {
return fmt.Errorf("error listing S3 Bucket (%s) Objects: %s", bucketName, err)
}

err = conn.ListObjectVersionsPages(input, func(page *s3.ListObjectVersionsOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

for _, deleteMarker := range page.DeleteMarkers {
input := &s3.DeleteObjectInput{
Bucket: bucket.Name,
Key: deleteMarker.Key,
VersionId: deleteMarker.VersionId,
}
deleteMarkerKey := aws.StringValue(deleteMarker.Key)
deleteMarkerVersionID := aws.StringValue(deleteMarker.VersionId)

log.Printf("[INFO] Deleting S3 Bucket (%s) Object (%s) Delete Marker: %s", bucketName, deleteMarkerKey, deleteMarkerVersionID)
_, err := conn.DeleteObject(input)

if isAWSErr(err, s3.ErrCodeNoSuchBucket, "") || isAWSErr(err, s3.ErrCodeNoSuchKey, "") {
continue
}

if err != nil {
log.Printf("[ERROR] Error deleting S3 Bucket (%s) Object (%s) Version (%s): %s", bucketName, deleteMarkerKey, deleteMarkerVersionID, err)
}
}

return !lastPage
})

if isAWSErr(err, s3.ErrCodeNoSuchBucket, "") {
continue
}

if err != nil {
return fmt.Errorf("error listing S3 Bucket (%s) Objects: %s", bucketName, err)
}
}

return nil
}

func TestAccAWSS3BucketObject_source(t *testing.T) {
var obj s3.GetObjectOutput
resourceName := "aws_s3_bucket_object.object"
Expand Down
91 changes: 85 additions & 6 deletions aws/resource_aws_s3_bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,104 @@ import (
"bytes"
"encoding/json"
"fmt"
"log"
"reflect"
"regexp"
"sort"
"strconv"
"strings"
"testing"
"text/template"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"

"strings"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
)

func init() {
resource.AddTestSweepers("aws_s3_bucket", &resource.Sweeper{
Name: "aws_s3_bucket",
F: testSweepS3Buckets,
Dependencies: []string{
"aws_s3_bucket_object",
},
})
}

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

conn := client.(*AWSClient).s3conn
input := &s3.ListBucketsInput{}

output, err := conn.ListBuckets(input)

if testSweepSkipSweepError(err) {
log.Printf("[WARN] Skipping S3 Buckets sweep for %s: %s", region, err)
return nil
}

if err != nil {
return fmt.Errorf("error listing S3 Buckets: %s", err)
}

if len(output.Buckets) == 0 {
log.Print("[DEBUG] No S3 Buckets to sweep")
return nil
}

for _, bucket := range output.Buckets {
name := aws.StringValue(bucket.Name)

if !strings.HasPrefix(name, "tf-acc") && !strings.HasPrefix(name, "tf-object-test") && !strings.HasPrefix(name, "tf-test") {
log.Printf("[INFO] Skipping S3 Bucket: %s", name)
continue
}

input := &s3.DeleteBucketInput{
Bucket: bucket.Name,
}

log.Printf("[INFO] Deleting S3 Bucket: %s", name)
err := resource.Retry(1*time.Minute, func() *resource.RetryError {
_, err := conn.DeleteBucket(input)

if isAWSErr(err, s3.ErrCodeNoSuchBucket, "") {
return nil
}

if isAWSErr(err, "BucketRegionError", "") {
log.Printf("[INFO] Skipping S3 Bucket (%s): %s", name, err)
return nil
}

if isAWSErr(err, "BucketNotEmpty", "") {
return resource.RetryableError(err)
}

if err != nil {
return resource.NonRetryableError(err)
}

return nil
})

if err != nil {
return fmt.Errorf("error deleting S3 Bucket (%s): %s", name, err)
}
}

return nil
}

func TestAccAWSS3Bucket_importBasic(t *testing.T) {
resourceName := "aws_s3_bucket.bucket"
rInt := acctest.RandInt()
Expand Down