diff --git a/.changelog/3120.txt b/.changelog/3120.txt new file mode 100644 index 0000000000..4e1b02c3f7 --- /dev/null +++ b/.changelog/3120.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +storage: added `metadata` to `google_storage_bucket_object`. +``` diff --git a/google-beta/resource_storage_bucket_object.go b/google-beta/resource_storage_bucket_object.go index 96c0a6b7bf..f3e7c02a35 100644 --- a/google-beta/resource_storage_bucket_object.go +++ b/google-beta/resource_storage_bucket_object.go @@ -139,6 +139,13 @@ func resourceStorageBucketObject() *schema.Resource { Computed: true, }, + "metadata": { + Type: schema.TypeMap, + Optional: true, + ForceNew: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + "self_link": { Type: schema.TypeString, Computed: true, diff --git a/google-beta/resource_storage_bucket_object_test.go b/google-beta/resource_storage_bucket_object_test.go index 714002f3c2..9d20c0385c 100644 --- a/google-beta/resource_storage_bucket_object_test.go +++ b/google-beta/resource_storage_bucket_object_test.go @@ -254,6 +254,38 @@ func TestAccStorageObject_storageClass(t *testing.T) { }) } +func TestAccStorageObject_metadata(t *testing.T) { + t.Parallel() + + bucketName := testBucketName() + data := []byte(content) + h := md5.New() + if _, err := h.Write(data); err != nil { + t.Errorf("error calculating md5: %v", err) + } + data_md5 := base64.StdEncoding.EncodeToString(h.Sum(nil)) + testFile := getNewTmpTestFile(t, "tf-test") + if err := ioutil.WriteFile(testFile.Name(), data, 0644); err != nil { + t.Errorf("error writing file: %v", err) + } + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccStorageObjectDestroy, + Steps: []resource.TestStep{ + { + Config: testGoogleStorageBucketsObject_metadata(bucketName), + Check: resource.ComposeTestCheckFunc( + testAccCheckGoogleStorageObject(bucketName, objectName, data_md5), + resource.TestCheckResourceAttr( + "google_storage_bucket_object.object", "metadata.customKey", "custom_value"), + ), + }, + }, + }) +} + func testAccCheckGoogleStorageObject(bucket, object, md5 string) resource.TestCheckFunc { return func(s *terraform.State) error { config := testAccProvider.Meta().(*Config) @@ -390,6 +422,24 @@ resource "google_storage_bucket_object" "object" { `, bucketName, objectName, content, storageClass) } +func testGoogleStorageBucketsObject_metadata(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 = "%s" + + metadata = { + "customKey" = "custom_value" + } +} +`, bucketName, objectName, content) +} + // Creates a new tmp test file. Fails the current test if we cannot create // new tmp file in the filesystem. func getNewTmpTestFile(t *testing.T, prefix string) *os.File { diff --git a/website/docs/r/storage_bucket_object.html.markdown b/website/docs/r/storage_bucket_object.html.markdown index 679d440911..fef95aeec0 100644 --- a/website/docs/r/storage_bucket_object.html.markdown +++ b/website/docs/r/storage_bucket_object.html.markdown @@ -37,6 +37,8 @@ The following arguments are supported: * `name` - (Required) The name of the object. If you're interpolating the name of this object, see `output_name` instead. +* `metadata` - (Optional) User-provided metadata, in key/value pairs. + One of the following is required: * `content` - (Optional, Sensitive) Data as `string` to be uploaded. Must be defined if `source` is not. **Note**: The `content` field is marked as sensitive. To view the raw contents of the object, please define an [output](/docs/configuration/outputs.html).