Skip to content

Commit 982f4d1

Browse files
authored
Fix storage object detect md5 hash for dynamic content (#848)
1 parent bea8642 commit 982f4d1

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

google/resource_storage_bucket_object.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@ func resourceStorageBucketObject() *schema.Resource {
104104
Optional: true,
105105
ForceNew: true,
106106
// Makes the diff message nicer:
107-
// md5hash: "1XcnP/iFw/hNrbhXi7QTmQ==" => "different hash" (forces new resource)
107+
// detect_md5hash: "1XcnP/iFw/hNrbhXi7QTmQ==" => "different hash" (forces new resource)
108108
// Instead of the more confusing:
109-
// md5hash: "1XcnP/iFw/hNrbhXi7QTmQ==" => "" (forces new resource)
109+
// detect_md5hash: "1XcnP/iFw/hNrbhXi7QTmQ==" => "" (forces new resource)
110110
Default: "different hash",
111111
// 1. Compute the md5 hash of the local file
112112
// 2. Compare the computed md5 hash with the hash stored in Cloud Storage
@@ -121,6 +121,13 @@ func resourceStorageBucketObject() *schema.Resource {
121121
localMd5Hash = getContentMd5Hash([]byte(content.(string)))
122122
}
123123

124+
// If `source` or `content` is dynamically set, both field will be empty.
125+
// We should not suppress the diff to avoid the following error:
126+
// 'Mismatch reason: extra attributes: detect_md5hash'
127+
if localMd5Hash == "" {
128+
return false
129+
}
130+
124131
// `old` is the md5 hash we retrieved from the server in the ReadFunc
125132
if old != localMd5Hash {
126133
return false

google/resource_storage_bucket_object_test.go

+35
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,27 @@ func TestAccGoogleStorageObject_withContentCharacteristics(t *testing.T) {
150150
})
151151
}
152152

153+
func TestAccGoogleStorageObject_dynamicContent(t *testing.T) {
154+
t.Parallel()
155+
156+
resource.Test(t, resource.TestCase{
157+
PreCheck: func() { testAccPreCheck(t) },
158+
Providers: testAccProviders,
159+
CheckDestroy: testAccGoogleStorageObjectDestroy,
160+
Steps: []resource.TestStep{
161+
resource.TestStep{
162+
Config: testGoogleStorageBucketsObjectDynamicContent(testBucketName()),
163+
Check: resource.ComposeTestCheckFunc(
164+
resource.TestCheckResourceAttr(
165+
"google_storage_bucket_object.object", "content_type", "text/plain; charset=utf-8"),
166+
resource.TestCheckResourceAttr(
167+
"google_storage_bucket_object.object", "storage_class", "STANDARD"),
168+
),
169+
},
170+
},
171+
})
172+
}
173+
153174
func TestAccGoogleStorageObject_cacheControl(t *testing.T) {
154175
t.Parallel()
155176

@@ -267,6 +288,20 @@ resource "google_storage_bucket_object" "object" {
267288
`, bucketName, objectName, content)
268289
}
269290

291+
func testGoogleStorageBucketsObjectDynamicContent(bucketName string) string {
292+
return fmt.Sprintf(`
293+
resource "google_storage_bucket" "bucket" {
294+
name = "%s"
295+
}
296+
297+
resource "google_storage_bucket_object" "object" {
298+
name = "%s"
299+
bucket = "${google_storage_bucket.bucket.name}"
300+
content = "${google_storage_bucket.bucket.project}"
301+
}
302+
`, bucketName, objectName)
303+
}
304+
270305
func testGoogleStorageBucketsObjectBasic(bucketName, sourceFilename string) string {
271306
return fmt.Sprintf(`
272307
resource "google_storage_bucket" "bucket" {

0 commit comments

Comments
 (0)