Added support for updating a cloud storage object using source field (#362)

This commit is contained in:
Rodrigue Cloutier 2017-11-24 13:25:53 -05:00 committed by Vincent Roseberry
parent e15e7742bf
commit 5ba87f3d9e
2 changed files with 66 additions and 0 deletions

View File

@ -2,8 +2,11 @@ package google
import (
"bytes"
"crypto/md5"
"encoding/base64"
"fmt"
"io"
"io/ioutil"
"log"
"os"
@ -92,6 +95,9 @@ func resourceStorageBucketObject() *schema.Resource {
Optional: true,
ForceNew: true,
ConflictsWith: []string{"content"},
StateFunc: func(src interface{}) string {
return fileNameWithMd5(src.(string))
},
},
"storage_class": &schema.Schema{
@ -108,6 +114,20 @@ func objectGetId(object *storage.Object) string {
return object.Bucket + "-" + object.Name
}
func fileNameWithMd5(filename string) string {
data, err := ioutil.ReadFile(filename)
if err != nil {
log.Printf("[WARN] Failed to read source file %s. Will not compute md5 to store in state", filename)
return filename
}
h := md5.New()
h.Write(data)
md5 := base64.StdEncoding.EncodeToString(h.Sum(nil))
return filename + ":" + md5
}
func resourceStorageBucketObjectCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

View File

@ -5,6 +5,7 @@ import (
"encoding/base64"
"fmt"
"io/ioutil"
"os"
"testing"
"github.com/hashicorp/terraform/helper/resource"
@ -46,6 +47,51 @@ func TestAccGoogleStorageObject_basic(t *testing.T) {
})
}
func TestAccGoogleStorageObject_recreate(t *testing.T) {
t.Parallel()
bucketName := testBucketName()
writeFile := func(name string, data []byte) string {
h := md5.New()
h.Write(data)
data_md5 := base64.StdEncoding.EncodeToString(h.Sum(nil))
ioutil.WriteFile(name, data, 0644)
return data_md5
}
data_md5 := writeFile(tf.Name(), []byte("data data data"))
updated_data_md5 := writeFile(tf.Name()+".update", []byte("datum"))
resource.Test(t, resource.TestCase{
PreCheck: func() {
if err != nil {
panic(err)
}
testAccPreCheck(t)
},
Providers: testAccProviders,
CheckDestroy: testAccGoogleStorageObjectDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testGoogleStorageBucketsObjectBasic(bucketName),
Check: testAccCheckGoogleStorageObject(bucketName, objectName, data_md5),
},
resource.TestStep{
PreConfig: func() {
updateName := tf.Name() + ".update"
err := os.Rename(updateName, tf.Name())
if err != nil {
t.Errorf("Failed to rename %s to %s", updateName, tf.Name())
}
},
Config: testGoogleStorageBucketsObjectBasic(bucketName),
Check: testAccCheckGoogleStorageObject(bucketName, objectName, updated_data_md5),
},
},
})
}
func TestAccGoogleStorageObject_content(t *testing.T) {
t.Parallel()