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

storage_bucket_object: added holds #9403

Closed
wants to merge 1 commit into from
Closed
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
27 changes: 26 additions & 1 deletion google/resource_storage_bucket_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,18 @@ func resourceStorageBucketObject() *schema.Resource {
DiffSuppressFunc: compareCryptoKeyVersions,
Description: `Resource name of the Cloud KMS key that will be used to encrypt the object. Overrides the object metadata's kmsKeyName value, if any.`,
},

"event_based_hold": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Description: `Whether an object is under event-based hold. Event-based hold is a way to retain objects until an event occurs, which is signified by the hold's release (i.e. this value is set to false). After being released (set to false), such objects will be subject to bucket-level retention (if any).`,
},
"temporary_hold": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Description: `Whether an object is under temporary hold. While this flag is set to true, the object is protected against deletion and overwrites.`,
},
"metadata": {
Type: schema.TypeMap,
Optional: true,
Expand Down Expand Up @@ -266,6 +277,14 @@ func resourceStorageBucketObjectCreate(d *schema.ResourceData, meta interface{})
object.KmsKeyName = v.(string)
}

if v, ok := d.GetOk("event_based_hold"); ok {
object.EventBasedHold = v.(bool)
}

if v, ok := d.GetOk("temporary_hold"); ok {
object.TemporaryHold = v.(bool)
}

insertCall := objectsService.Insert(bucket, object)
insertCall.Name(name)
insertCall.Media(media)
Expand Down Expand Up @@ -340,6 +359,12 @@ 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("event_based_hold", res.EventBasedHold); err != nil {
return fmt.Errorf("Error setting event_based_hold: %s", err)
}
if err := d.Set("temporary_hold", res.TemporaryHold); err != nil {
return fmt.Errorf("Error setting temporary_hold: %s", err)
}

d.SetId(objectGetID(res))

Expand Down
50 changes: 50 additions & 0 deletions google/resource_storage_bucket_object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,40 @@ func TestAccStorageObjectKms(t *testing.T) {
})
}

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

bucketName := 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)
}

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccStorageObjectDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testGoogleStorageBucketsObjectHolds(bucketName),
Check: resource.ComposeTestCheckFunc(
testAccCheckGoogleStorageObject(t, bucketName, objectName, dataMd5),
resource.TestCheckResourceAttr(
"google_storage_bucket_object.object", "event_based_hold", "true"),
resource.TestCheckResourceAttr(
"google_storage_bucket_object.object", "temporary_hold", "true"),
),
},
},
})
}

func testAccCheckGoogleStorageObject(t *testing.T, bucket, object, md5 string) resource.TestCheckFunc {
return func(s *terraform.State) error {
config := googleProviderConfig(t)
Expand Down Expand Up @@ -472,6 +506,22 @@ resource "google_storage_bucket_object" "object" {
`, bucketName, objectName, content)
}

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

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

func testGoogleStorageBucketsObjectKms(bucketName, sourceFilename, kmsKey string) string {
return fmt.Sprintf(`

Expand Down