From bfa67665f594b814d6d3e4b42c6d213e83d4d2f1 Mon Sep 17 00:00:00 2001 From: Modular Magician Date: Thu, 12 Dec 2024 21:34:46 +0000 Subject: [PATCH] Make resource_google_storage_bucket_object generate diff for md5hash, generation, crc32c if content changes (#12541) [upstream:3b3cb68702ce1f619ff98998560c381d570527c9] Signed-off-by: Modular Magician --- .changelog/12541.txt | 3 ++ .../storage/resource_storage_bucket_object.go | 42 +++++++++++++++++-- 2 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 .changelog/12541.txt diff --git a/.changelog/12541.txt b/.changelog/12541.txt new file mode 100644 index 0000000000..09d9b32653 --- /dev/null +++ b/.changelog/12541.txt @@ -0,0 +1,3 @@ +```release-note:bug +storage: Make `resource_google_storage_bucket_object` generate diff for `md5hash`, `generation`, `crc32c` if content changes +``` \ No newline at end of file diff --git a/google-beta/services/storage/resource_storage_bucket_object.go b/google-beta/services/storage/resource_storage_bucket_object.go index 53932c51a0..77ae68a85a 100644 --- a/google-beta/services/storage/resource_storage_bucket_object.go +++ b/google-beta/services/storage/resource_storage_bucket_object.go @@ -5,6 +5,7 @@ package storage import ( "bytes" + "context" "fmt" "io" "log" @@ -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), @@ -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 +}