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

Object lock feature implementation for Storage #9363

Merged
merged 16 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
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
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,13 @@ func ResourceStorageBucket() *schema.Resource {
Description: `The bucket's Lifecycle Rules configuration.`,
},

"per_object_retention": {
thomasmaclean marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Description: `Enables each object in the bucket to have its own retention policy, which prevents deletion until stored for a specific length of time.`,
},

"versioning": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -594,7 +601,11 @@ func resourceStorageBucketCreate(d *schema.ResourceData, meta interface{}) error

err = transport_tpg.Retry(transport_tpg.RetryOptions{
RetryFunc: func() error {
res, err = config.NewStorageClient(userAgent).Buckets.Insert(project, sb).Do()
insertCall := config.NewStorageClient(userAgent).Buckets.Insert(project, sb)
if d.Get("per_object_retention").(bool) {
thomasmaclean marked this conversation as resolved.
Show resolved Hide resolved
insertCall.EnableObjectRetention(true)
}
res, err = insertCall.Do()
return err
},
})
Expand Down Expand Up @@ -1121,6 +1132,16 @@ func flattenBucketRetentionPolicy(bucketRetentionPolicy *storage.BucketRetention
return bucketRetentionPolicies
}

func flattenBucketObjectRetention(bucketObjectRetention *storage.BucketObjectRetention) bool {
if bucketObjectRetention == nil {
return false
}
if bucketObjectRetention.Mode == "Enabled" {
return true
}
return false
}

func expandBucketVersioning(configured interface{}) *storage.BucketVersioning {
versionings := configured.([]interface{})
if len(versionings) == 0 {
Expand Down Expand Up @@ -1620,6 +1641,9 @@ func setStorageBucket(d *schema.ResourceData, config *transport_tpg.Config, res
if err := d.Set("logging", flattenBucketLogging(res.Logging)); err != nil {
return fmt.Errorf("Error setting logging: %s", err)
}
if err := d.Set("per_object_retention", flattenBucketObjectRetention(res.ObjectRetention)); err != nil {
return fmt.Errorf("Error setting object retention: %s", err)
}
if err := d.Set("versioning", flattenBucketVersioning(res.Versioning)); err != nil {
return fmt.Errorf("Error setting versioning: %s", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import (
"io"
"log"
"os"
"strings"
"time"

"github.com/hashicorp/terraform-provider-google/google/tpgresource"
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"

"crypto/sha256"
"encoding/base64"
Expand Down Expand Up @@ -207,6 +209,29 @@ func ResourceStorageBucketObject() *schema.Resource {
},
},

"retention": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
ConflictsWith: []string{"event_based_hold"},
Description: `Object level retention configuration.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"retain_until": {
thomasmaclean marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeString,
Required: true,
Description: `Time in RFC 3339 (e.g. 2030-01-01T02:03:04Z) until which object retention protects this object.`,
},
"mode": {
Type: schema.TypeString,
Required: true,
Description: `The object retention mode. Supported values include: "UNLOCKED", "LOCKED".`,
ValidateFunc: validation.StringInSlice([]string{"UNLOCKED", "LOCKED"}, false),
},
},
},
},

"event_based_hold": {
Type: schema.TypeBool,
Optional: true,
thomasmaclean marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -312,6 +337,10 @@ func resourceStorageBucketObjectCreate(d *schema.ResourceData, meta interface{})
object.KmsKeyName = v.(string)
}

if v, ok := d.GetOk("retention"); ok {
object.Retention = expandObjectRetention(v)
}

if v, ok := d.GetOk("event_based_hold"); ok {
object.EventBasedHold = v.(bool)
}
Expand Down Expand Up @@ -357,6 +386,16 @@ func resourceStorageBucketObjectUpdate(d *schema.ResourceData, meta interface{})
return fmt.Errorf("Error retrieving object during update %s: %s", name, err)
}

hasRetentionChanges := d.HasChange("retention")
if hasRetentionChanges {
if v, ok := d.GetOk("retention"); ok {
res.Retention = expandObjectRetention(v)
} else {
res.Retention = nil
res.NullFields = append(res.NullFields, "Retention")
}
}

if d.HasChange("event_based_hold") {
v := d.Get("event_based_hold")
res.EventBasedHold = v.(bool)
Expand All @@ -368,6 +407,9 @@ func resourceStorageBucketObjectUpdate(d *schema.ResourceData, meta interface{})
}

updateCall := objectsService.Update(bucket, name, res)
if hasRetentionChanges {
updateCall.OverrideUnlockedRetention(true)
}
_, err = updateCall.Do()

if err != nil {
Expand Down Expand Up @@ -443,6 +485,9 @@ func resourceStorageBucketObjectRead(d *schema.ResourceData, meta interface{}) e
if err := d.Set("media_link", res.MediaLink); err != nil {
return fmt.Errorf("Error setting media_link: %s", err)
}
if err := d.Set("retention", flattenObjectRetention(res.Retention)); err != nil {
return fmt.Errorf("Error setting lifecycle_rule: %s", err)
thomasmaclean marked this conversation as resolved.
Show resolved Hide resolved
}
if err := d.Set("event_based_hold", res.EventBasedHold); err != nil {
return fmt.Errorf("Error setting event_based_hold: %s", err)
}
Expand Down Expand Up @@ -513,3 +558,39 @@ func expandCustomerEncryption(input []interface{}) map[string]string {
}
return expanded
}

func expandObjectRetention(configured interface{}) *storage.ObjectRetention {
retentions := configured.([]interface{})
if len(retentions) == 0 {
return nil
}
retention := retentions[0].(map[string]interface{})

mode := "Unlocked"
if retention["mode"].(string) == "LOCKED" {
thomasmaclean marked this conversation as resolved.
Show resolved Hide resolved
mode = "Locked"
}

objectRetention := &storage.ObjectRetention{
RetainUntilTime: retention["retain_until"].(string),
Mode: mode,
thomasmaclean marked this conversation as resolved.
Show resolved Hide resolved
}

return objectRetention
}

func flattenObjectRetention(objectRetention *storage.ObjectRetention) []map[string]interface{} {
retentions := make([]map[string]interface{}, 0, 1)

if objectRetention == nil {
return retentions
}

retention := map[string]interface{}{
"mode": strings.ToUpper(objectRetention.Mode),
"retain_until": objectRetention.RetainUntilTime,
}

retentions = append(retentions, retention)
return retentions
}
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,48 @@ func TestAccStorageObject_holds(t *testing.T) {
})
}

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

bucketName := acctest.TestBucketName(t)
data := []byte(content)
h := md5.New()
if _, err := h.Write(data); err != nil {
t.Errorf("error calculating md5: %v", err)
}
dataMd5 := base64.StdEncoding.EncodeToString(h.Sum(nil))
testFile := getNewTmpTestFile(t, "tf-test")
if err := ioutil.WriteFile(testFile.Name(), data, 0644); err != nil {
t.Errorf("error writing file: %v", err)
}

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccStorageObjectDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testGoogleStorageBucketsObjectRetention(bucketName, "2040-01-01T02:03:04.000Z"),
thomasmaclean marked this conversation as resolved.
Show resolved Hide resolved
Check: resource.ComposeTestCheckFunc(
testAccCheckGoogleStorageObject(t, bucketName, objectName, dataMd5),
),
},
{
Config: testGoogleStorageBucketsObjectRetention(bucketName, "2040-01-02T02:03:04.000Z"),
thomasmaclean marked this conversation as resolved.
Show resolved Hide resolved
Check: resource.ComposeTestCheckFunc(
testAccCheckGoogleStorageObject(t, bucketName, objectName, dataMd5),
),
},
{
Config: testGoogleStorageBucketsObjectRetentionDisabled(bucketName),
Check: resource.ComposeTestCheckFunc(
testAccCheckGoogleStorageObject(t, bucketName, objectName, dataMd5),
),
},
},
})
}

func testAccCheckGoogleStorageObject(t *testing.T, bucket, object, md5 string) resource.TestCheckFunc {
return testAccCheckGoogleStorageObjectWithEncryption(t, bucket, object, md5, "")
}
Expand Down Expand Up @@ -646,6 +688,44 @@ resource "google_storage_bucket_object" "object" {
`, bucketName, objectName, content, customerEncryptionKey)
}

func testGoogleStorageBucketsObjectRetention(bucketName string, retainUntil string) string {
return fmt.Sprintf(`
resource "google_storage_bucket" "bucket" {
name = "%s"
location = "US"
force_destroy = true
per_object_retention = true
}

resource "google_storage_bucket_object" "object" {
name = "%s"
bucket = google_storage_bucket.bucket.name
content = "%s"
retention {
mode = "UNLOCKED"
retain_until = "%s"
}
}
`, bucketName, objectName, content, retainUntil)
}

func testGoogleStorageBucketsObjectRetentionDisabled(bucketName string) string {
return fmt.Sprintf(`
resource "google_storage_bucket" "bucket" {
name = "%s"
location = "US"
force_destroy = true
per_object_retention = true
}

resource "google_storage_bucket_object" "object" {
name = "%s"
bucket = google_storage_bucket.bucket.name
content = "%s"
}
`, bucketName, objectName, content)
}

func testGoogleStorageBucketsObjectHolds(bucketName string, eventBasedHold bool, temporaryHold bool) string {
return fmt.Sprintf(`
resource "google_storage_bucket" "bucket" {
Expand Down
Loading