mirror of
https://github.com/letic/terraform-provider-google.git
synced 2024-07-05 17:52:38 +00:00
Added support for updating a cloud storage object using source field (#362)
This commit is contained in:
parent
e15e7742bf
commit
5ba87f3d9e
|
@ -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)
|
||||
|
||||
|
|
|
@ -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()
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user