Skip to content

Commit 18e9f33

Browse files
feat: (storage) added deletion_policy field in storage bucket object (#14594) (#10445)
[upstream:00de1d49713446dbe1a381af748ecc2717380633] Signed-off-by: Modular Magician <[email protected]>
1 parent 94b2725 commit 18e9f33

File tree

4 files changed

+81
-0
lines changed

4 files changed

+81
-0
lines changed

.changelog/14594.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
storage: added `deletion_policy` field to `google_storage_bucket_object` resource
3+
```

google-beta/services/storage/resource_storage_bucket_object.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport"
3232

3333
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
34+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
3435

3536
"crypto/sha256"
3637
"encoding/base64"
@@ -323,6 +324,13 @@ func ResourceStorageBucketObject() *schema.Resource {
323324
Computed: true,
324325
Description: `A url reference to download this object.`,
325326
},
327+
328+
"deletion_policy": {
329+
Type: schema.TypeString,
330+
Optional: true,
331+
Description: `The deletion policy for the object. Setting ABANDON allows the resource to be abandoned rather than deleted when removed from your Terraform configuration.`,
332+
ValidateFunc: validation.StringInSlice([]string{"ABANDON"}, false),
333+
},
326334
},
327335
UseJSONNumber: true,
328336
}
@@ -587,6 +595,12 @@ func resourceStorageBucketObjectDelete(d *schema.ResourceData, meta interface{})
587595
return err
588596
}
589597

598+
if deletionPolicy := d.Get("deletion_policy"); deletionPolicy == "ABANDON" {
599+
log.Printf("[WARN] Object %q deletion_policy is set to 'ABANDON', object deletion has been abandoned", d.Id())
600+
d.SetId("")
601+
return nil
602+
}
603+
590604
bucket := d.Get("bucket").(string)
591605
name := d.Get("name").(string)
592606

google-beta/services/storage/resource_storage_bucket_object_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,29 @@ func TestAccStorageObject_knownAfterApply(t *testing.T) {
632632
})
633633
}
634634

635+
func TestAccStorageObject_objectDeletionPolicy(t *testing.T) {
636+
t.Parallel()
637+
638+
bucketName := acctest.TestBucketName(t)
639+
640+
acctest.VcrTest(t, resource.TestCase{
641+
PreCheck: func() { acctest.AccTestPreCheck(t) },
642+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
643+
CheckDestroy: testAccStorageObjectDestroyProducer(t),
644+
Steps: []resource.TestStep{
645+
{
646+
Config: testGoogleStorageBucketsObjectDeletionPolicy(bucketName, "samplecontent"),
647+
},
648+
{
649+
Config: testGoogleStorageBucketsObjectAbandon(bucketName),
650+
Check: resource.ComposeTestCheckFunc(
651+
testAccCheckStorageObjectExists(t, bucketName),
652+
),
653+
},
654+
},
655+
})
656+
}
657+
635658
func testAccCheckGoogleStorageObject(t *testing.T, bucket, object, md5 string) resource.TestCheckFunc {
636659
return testAccCheckGoogleStorageObjectWithEncryption(t, bucket, object, md5, "")
637660
}
@@ -1096,3 +1119,42 @@ output "valid" {
10961119
}
10971120
`, bucketName, content, filename)
10981121
}
1122+
1123+
func testGoogleStorageBucketsObjectDeletionPolicy(bucketName string, customContent string) string {
1124+
return fmt.Sprintf(`
1125+
resource "google_storage_bucket" "bucket" {
1126+
name = "%s"
1127+
location = "US"
1128+
}
1129+
1130+
resource "google_storage_bucket_object" "object" {
1131+
name = "%s"
1132+
bucket = google_storage_bucket.bucket.name
1133+
content = "%s"
1134+
deletion_policy = "ABANDON"
1135+
}
1136+
`, bucketName, objectName, customContent)
1137+
}
1138+
1139+
func testGoogleStorageBucketsObjectAbandon(bucketName string) string {
1140+
return fmt.Sprintf(`
1141+
resource "google_storage_bucket" "bucket" {
1142+
name = "%s"
1143+
location = "US"
1144+
force_destroy = true
1145+
}
1146+
`, bucketName)
1147+
}
1148+
1149+
func testAccCheckStorageObjectExists(t *testing.T, bucketName string) resource.TestCheckFunc {
1150+
return func(s *terraform.State) error {
1151+
1152+
config := acctest.GoogleProviderConfig(t)
1153+
1154+
_, err := config.NewStorageClient(config.UserAgent).Objects.Get(bucketName, objectName).Do()
1155+
if err != nil {
1156+
return err
1157+
}
1158+
return nil
1159+
}
1160+
}

website/docs/r/storage_bucket_object.html.markdown

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ One of the following is required:
105105

106106
* `force_empty_content_type` - (Optional) When set to true, it ensure the object's Content-Type is empty.
107107

108+
* `deletion_policy` - (Optional) When set to ABANDON, the object won't be deleted from storage bucket. Instead, it will only be removed from terraform's state file.
109+
108110
---
109111

110112
<a name="nested_customer_encryption"></a>The `customer_encryption` block supports:

0 commit comments

Comments
 (0)