Skip to content

Commit

Permalink
Storage bucket: Surface non-retryable object delete error (GoogleClou…
Browse files Browse the repository at this point in the history
…dPlatform#3336)

* Storage bucket: Surface non-retryable object delete error

* storage: test deleting bucket with non-deletable objects error
  • Loading branch information
stepanstipl authored and Nathan Klish committed May 18, 2020
1 parent f42b214 commit fe0c2b5
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 6 deletions.
12 changes: 6 additions & 6 deletions third_party/terraform/resources/resource_storage_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -616,8 +616,8 @@ func resourceStorageBucketDelete(d *schema.ResourceData, meta interface{}) error
// Get the bucket
bucket := d.Get("name").(string)

var listError error
for {
var listError, deleteObjectError error
for deleteObjectError == nil {
res, err := config.clientStorage.Objects.List(bucket).Versions(true).Do()
if err != nil {
log.Printf("Error listing contents of bucket %s: %v", bucket, err)
Expand Down Expand Up @@ -672,10 +672,7 @@ func resourceStorageBucketDelete(d *schema.ResourceData, meta interface{}) error
wp.Submit(func() {
log.Printf("[TRACE] Attempting to delete %s", object.Name)
if err := config.clientStorage.Objects.Delete(bucket, object.Name).Generation(object.Generation).Do(); err != nil {
// We should really return an error here, but it doesn't really
// matter since the following step (bucket deletion) will fail
// with an error indicating objects are still present, and this
// log line will point to that object.
deleteObjectError = err
log.Printf("[ERR] Failed to delete storage object %s: %s", object.Name, err)
} else {
log.Printf("[TRACE] Successfully deleted %s", object.Name)
Expand All @@ -701,6 +698,9 @@ func resourceStorageBucketDelete(d *schema.ResourceData, meta interface{}) error
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 409 && strings.Contains(gerr.Message, "not empty") && listError != nil {
return fmt.Errorf("could not delete non-empty bucket due to error when listing contents: %v", listError)
}
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 409 && strings.Contains(gerr.Message, "not empty") && deleteObjectError != nil {
return fmt.Errorf("could not delete non-empty bucket due to error when deleting contents: %v", deleteObjectError)
}
if err != nil {
log.Printf("Error deleting bucket %s: %v", bucket, err)
return err
Expand Down
51 changes: 51 additions & 0 deletions third_party/terraform/tests/resource_storage_bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,33 @@ func TestAccStorageBucket_forceDestroyWithVersioning(t *testing.T) {
})
}

func TestAccStorageBucket_forceDestroyObjectDeleteError(t *testing.T) {
t.Parallel()

bucketName := fmt.Sprintf("tf-test-acl-bucket-%d", acctest.RandInt())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccStorageBucketDestroy,
Steps: []resource.TestStep{
{
Config: testAccStorageBucket_forceDestroyWithRetentionPolicy(bucketName),
Check: resource.ComposeTestCheckFunc(
testAccCheckStorageBucketPutItem(bucketName),
),
},
{
Config: testAccStorageBucket_forceDestroyWithRetentionPolicy(bucketName),
Destroy: true,
ExpectError: regexp.MustCompile("could not delete non-empty bucket due to error when deleting contents"),
},
{
Config: testAccStorageBucket_forceDestroy(bucketName),
},
},
})
}
func TestAccStorageBucket_versioning(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -1492,3 +1519,27 @@ resource "google_storage_bucket" "website" {
}
`, bucketName)
}

func testAccStorageBucket_forceDestroy(bucketName string) string {
return fmt.Sprintf(`
resource "google_storage_bucket" "bucket" {
name = "%s"
force_destroy = true
}
`, bucketName)
}

func testAccStorageBucket_forceDestroyWithRetentionPolicy(bucketName string) string {
return fmt.Sprintf(`
resource "google_storage_bucket" "bucket" {
name = "%s"
force_destroy = true
retention_policy {
retention_period = 3600
}
}
`, bucketName)
}

0 comments on commit fe0c2b5

Please sign in to comment.