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

provider/google: Content field for bucket objects #3893

Merged
merged 1 commit into from
Jan 13, 2016
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
42 changes: 32 additions & 10 deletions builtin/providers/google/resource_storage_bucket_object.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package google

import (
"bytes"
"fmt"
"io"
"log"
"os"

Expand All @@ -23,26 +25,39 @@ func resourceStorageBucketObject() *schema.Resource {
Required: true,
ForceNew: true,
},

"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"source": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"content"},
},

"content": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"source"},
},

"predefined_acl": &schema.Schema{
Type: schema.TypeString,
Deprecated: "Please use resource \"storage_object_acl.predefined_acl\" instead.",
Optional: true,
ForceNew: true,
},

"md5hash": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},

"crc32c": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Expand All @@ -60,27 +75,34 @@ func resourceStorageBucketObjectCreate(d *schema.ResourceData, meta interface{})

bucket := d.Get("bucket").(string)
name := d.Get("name").(string)
source := d.Get("source").(string)
var media io.Reader

file, err := os.Open(source)
if err != nil {
return fmt.Errorf("Error opening %s: %s", source, err)
if v, ok := d.GetOk("source"); ok {
err := error(nil)
media, err = os.Open(v.(string))
if err != nil {
return err
}
} else if v, ok := d.GetOk("content"); ok {
media = bytes.NewReader([]byte(v.(string)))
} else {
return fmt.Errorf("Error, either \"content\" or \"string\" must be specified")
}

objectsService := storage.NewObjectsService(config.clientStorage)
object := &storage.Object{Bucket: bucket}

insertCall := objectsService.Insert(bucket, object)
insertCall.Name(name)
insertCall.Media(file)
insertCall.Media(media)
if v, ok := d.GetOk("predefined_acl"); ok {
insertCall.PredefinedAcl(v.(string))
}

_, err = insertCall.Do()
_, err := insertCall.Do()

if err != nil {
return fmt.Errorf("Error uploading contents of object %s from %s: %s", name, source, err)
return fmt.Errorf("Error uploading object %s: %s", name, err)
}

return resourceStorageBucketObjectRead(d, meta)
Expand Down
39 changes: 39 additions & 0 deletions builtin/providers/google/resource_storage_bucket_object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
var tf, err = ioutil.TempFile("", "tf-gce-test")
var bucketName = "tf-gce-bucket-test"
var objectName = "tf-gce-test"
var content = "now this is content!"

func TestAccGoogleStorageObject_basic(t *testing.T) {
data := []byte("data data data")
Expand All @@ -42,6 +43,31 @@ func TestAccGoogleStorageObject_basic(t *testing.T) {
})
}

func TestAccGoogleStorageObject_content(t *testing.T) {
data := []byte(content)
h := md5.New()
h.Write(data)
data_md5 := base64.StdEncoding.EncodeToString(h.Sum(nil))

ioutil.WriteFile(tf.Name(), data, 0644)
resource.Test(t, resource.TestCase{
PreCheck: func() {
if err != nil {
panic(err)
}
testAccPreCheck(t)
},
Providers: testAccProviders,
CheckDestroy: testAccGoogleStorageObjectDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testGoogleStorageBucketsObjectContent,
Check: testAccCheckGoogleStorageObject(bucketName, objectName, data_md5),
},
},
})
}

func testAccCheckGoogleStorageObject(bucket, object, md5 string) resource.TestCheckFunc {
return func(s *terraform.State) error {
config := testAccProvider.Meta().(*Config)
Expand Down Expand Up @@ -87,6 +113,19 @@ func testAccGoogleStorageObjectDestroy(s *terraform.State) error {
return nil
}

var testGoogleStorageBucketsObjectContent = fmt.Sprintf(`
resource "google_storage_bucket" "bucket" {
name = "%s"
}

resource "google_storage_bucket_object" "object" {
name = "%s"
bucket = "${google_storage_bucket.bucket.name}"
content = "%s"
predefined_acl = "projectPrivate"
}
`, bucketName, objectName, content)

var testGoogleStorageBucketsObjectBasic = fmt.Sprintf(`
resource "google_storage_bucket" "bucket" {
name = "%s"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,15 @@ resource "google_storage_bucket_object" "picture" {
The following arguments are supported:

* `name` - (Required) The name of the object.

* `bucket` - (Required) The name of the containing bucket.
* `source` - (Required) A path to the data you want to upload.

* `source` - (Optional) A path to the data you want to upload. Must be defined
if `content` is not.

* `content` - (Optional) Data as `string` to be uploaded. Must be defined if
`source` is not.

* `predefined_acl` - (Optional, Deprecated) The [canned GCS ACL](https://cloud.google.com/storage/docs/access-control#predefined-acl) apply. Please switch
to `google_storage_object_acl.predefined_acl`.

Expand All @@ -39,4 +46,5 @@ to `google_storage_object_acl.predefined_acl`.
The following attributes are exported:

* `md5hash` - (Computed) Base 64 MD5 hash of the uploaded data.

* `crc32c` - (Computed) Base 64 CRC32 hash of the uploaded data.