Skip to content

Commit

Permalink
add metadata to storage_bucket_object (#3120) (#1779)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored Feb 19, 2020
1 parent 5985482 commit f8036f6
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/3120.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
storage: added `metadata` to `google_storage_bucket_object`.
```
7 changes: 7 additions & 0 deletions google-beta/resource_storage_bucket_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
50 changes: 50 additions & 0 deletions google-beta/resource_storage_bucket_object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/storage_bucket_object.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down

0 comments on commit f8036f6

Please sign in to comment.