Skip to content

Commit

Permalink
Make resource_google_storage_bucket_object generate diff for md5hash,…
Browse files Browse the repository at this point in the history
… generation, crc32c if content changes (#12541)

[upstream:3b3cb68702ce1f619ff98998560c381d570527c9]

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician committed Dec 12, 2024
1 parent 3af98a8 commit bfa6766
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .changelog/12541.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
storage: Make `resource_google_storage_bucket_object` generate diff for `md5hash`, `generation`, `crc32c` if content changes
```
42 changes: 38 additions & 4 deletions google-beta/services/storage/resource_storage_bucket_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package storage

import (
"bytes"
"context"
"fmt"
"io"
"log"
Expand All @@ -26,10 +27,11 @@ import (

func ResourceStorageBucketObject() *schema.Resource {
return &schema.Resource{
Create: resourceStorageBucketObjectCreate,
Read: resourceStorageBucketObjectRead,
Update: resourceStorageBucketObjectUpdate,
Delete: resourceStorageBucketObjectDelete,
Create: resourceStorageBucketObjectCreate,
Read: resourceStorageBucketObjectRead,
Update: resourceStorageBucketObjectUpdate,
Delete: resourceStorageBucketObjectDelete,
CustomizeDiff: resourceStorageBucketObjectCustomizeDiff,

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(4 * time.Minute),
Expand Down Expand Up @@ -605,3 +607,35 @@ func flattenObjectRetention(objectRetention *storage.ObjectRetention) []map[stri
retentions = append(retentions, retention)
return retentions
}

func resourceStorageBucketObjectCustomizeDiff(ctx context.Context, d *schema.ResourceDiff, meta interface{}) error {
localMd5Hash := ""
if source, ok := d.GetOkExists("source"); ok {
localMd5Hash = tpgresource.GetFileMd5Hash(source.(string))
}
if content, ok := d.GetOkExists("content"); ok {
localMd5Hash = tpgresource.GetContentMd5Hash([]byte(content.(string)))
}
if localMd5Hash == "" {
return nil
}

oldMd5Hash, ok := d.GetOkExists("md5hash")
if ok && oldMd5Hash == localMd5Hash {
return nil
}

err := d.SetNewComputed("md5hash")
if err != nil {
return fmt.Errorf("Error re-setting md5hash: %s", err)
}
err = d.SetNewComputed("crc32c")
if err != nil {
return fmt.Errorf("Error re-setting crc32c: %s", err)
}
err = d.SetNewComputed("generation")
if err != nil {
return fmt.Errorf("Error re-setting generation: %s", err)
}
return nil
}

0 comments on commit bfa6766

Please sign in to comment.