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

Fix storage object detect md5 hash for dynamic content #848

Merged
merged 1 commit into from
Dec 14, 2017
Merged
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
11 changes: 9 additions & 2 deletions google/resource_storage_bucket_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ func resourceStorageBucketObject() *schema.Resource {
Optional: true,
ForceNew: true,
// Makes the diff message nicer:
// md5hash: "1XcnP/iFw/hNrbhXi7QTmQ==" => "different hash" (forces new resource)
// detect_md5hash: "1XcnP/iFw/hNrbhXi7QTmQ==" => "different hash" (forces new resource)
// Instead of the more confusing:
// md5hash: "1XcnP/iFw/hNrbhXi7QTmQ==" => "" (forces new resource)
// detect_md5hash: "1XcnP/iFw/hNrbhXi7QTmQ==" => "" (forces new resource)
Default: "different hash",
// 1. Compute the md5 hash of the local file
// 2. Compare the computed md5 hash with the hash stored in Cloud Storage
Expand All @@ -121,6 +121,13 @@ func resourceStorageBucketObject() *schema.Resource {
localMd5Hash = getContentMd5Hash([]byte(content.(string)))
}

// If `source` or `content` is dynamically set, both field will be empty.
// We should not suppress the diff to avoid the following error:
// 'Mismatch reason: extra attributes: detect_md5hash'
if localMd5Hash == "" {
return false
}

// `old` is the md5 hash we retrieved from the server in the ReadFunc
if old != localMd5Hash {
return false
Expand Down
35 changes: 35 additions & 0 deletions google/resource_storage_bucket_object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,27 @@ func TestAccGoogleStorageObject_withContentCharacteristics(t *testing.T) {
})
}

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

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccGoogleStorageObjectDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testGoogleStorageBucketsObjectDynamicContent(testBucketName()),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"google_storage_bucket_object.object", "content_type", "text/plain; charset=utf-8"),
resource.TestCheckResourceAttr(
"google_storage_bucket_object.object", "storage_class", "STANDARD"),
),
},
},
})
}

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

Expand Down Expand Up @@ -267,6 +288,20 @@ resource "google_storage_bucket_object" "object" {
`, bucketName, objectName, content)
}

func testGoogleStorageBucketsObjectDynamicContent(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 = "${google_storage_bucket.bucket.project}"
}
`, bucketName, objectName)
}

func testGoogleStorageBucketsObjectBasic(bucketName, sourceFilename string) string {
return fmt.Sprintf(`
resource "google_storage_bucket" "bucket" {
Expand Down