Add S3 bucket object tag support. (#11344)

This commit is contained in:
Kit Ewbank 2017-02-13 12:27:13 -05:00 committed by Paul Stack
parent 911717c9d5
commit 03af9fa42d
6 changed files with 81 additions and 0 deletions

View File

@ -99,6 +99,8 @@ func dataSourceAwsS3BucketObject() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"tags": tagsSchemaComputed(),
},
}
}
@ -201,6 +203,16 @@ func dataSourceAwsS3BucketObjectRead(d *schema.ResourceData, meta interface{}) e
uniqueId, contentType)
}
tagResp, err := conn.GetObjectTagging(
&s3.GetObjectTaggingInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
})
if err != nil {
return err
}
d.Set("tags", tagsToMapS3(tagResp.TagSet))
return nil
}

View File

@ -1,3 +1,4 @@
// make testacc TEST=./builtin/providers/aws/ TESTARGS='-run=TestAccDataSourceAWSS3BucketObject_'
package aws
import (
@ -160,6 +161,7 @@ func TestAccDataSourceAWSS3BucketObject_allParams(t *testing.T) {
resource.TestCheckResourceAttr("data.aws_s3_bucket_object.obj", "expires", ""),
resource.TestCheckResourceAttr("data.aws_s3_bucket_object.obj", "website_redirect_location", ""),
resource.TestCheckResourceAttr("data.aws_s3_bucket_object.obj", "metadata.%", "0"),
resource.TestCheckResourceAttr("data.aws_s3_bucket_object.obj", "tags.%", "1"),
),
},
},
@ -284,6 +286,9 @@ CONTENT
content_disposition = "attachment"
content_encoding = "gzip"
content_language = "en-GB"
tags {
Key1 = "Value 1"
}
}
`, randInt, randInt)

View File

@ -5,6 +5,7 @@ import (
"fmt"
"io"
"log"
"net/url"
"os"
"sort"
"strings"
@ -117,6 +118,8 @@ func resourceAwsS3BucketObject() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"tags": tagsSchema(),
},
}
}
@ -188,6 +191,15 @@ func resourceAwsS3BucketObjectPut(d *schema.ResourceData, meta interface{}) erro
putInput.ServerSideEncryption = aws.String(s3.ServerSideEncryptionAwsKms)
}
if v, ok := d.GetOk("tags"); ok {
// The tag-set must be encoded as URL Query parameters.
values := url.Values{}
for k, v := range v.(map[string]interface{}) {
values.Add(k, v.(string))
}
putInput.Tagging = aws.String(values.Encode())
}
resp, err := s3conn.PutObject(putInput)
if err != nil {
return fmt.Errorf("Error putting object in S3 bucket (%s): %s", bucket, err)
@ -257,6 +269,16 @@ func resourceAwsS3BucketObjectRead(d *schema.ResourceData, meta interface{}) err
d.Set("storage_class", resp.StorageClass)
}
tagResp, err := s3conn.GetObjectTagging(
&s3.GetObjectTaggingInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
})
if err != nil {
return err
}
d.Set("tags", tagsToMapS3(tagResp.TagSet))
return nil
}

View File

@ -1,3 +1,4 @@
// make testacc TEST=./builtin/providers/aws/ TESTARGS='-run=TestAccAWSS3BucketObject_'
package aws
import (
@ -532,6 +533,27 @@ func testAccCheckAWSS3BucketObjectSSE(n, expectedSSE string) resource.TestCheckF
}
}
func TestAccAWSS3BucketObject_tags(t *testing.T) {
rInt := acctest.RandInt()
var obj s3.GetObjectOutput
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSS3BucketObjectDestroy,
Steps: []resource.TestStep{
resource.TestStep{
PreConfig: func() {},
Config: testAccAWSS3BucketObjectConfig_withTags(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSS3BucketObjectExists("aws_s3_bucket_object.object", &obj),
resource.TestCheckResourceAttr("aws_s3_bucket_object.object", "tags.%", "2"),
),
},
},
})
}
func testAccAWSS3BucketObjectConfigSource(randInt int, source string) string {
return fmt.Sprintf(`
resource "aws_s3_bucket" "object_bucket" {
@ -668,3 +690,21 @@ resource "aws_s3_bucket_object" "object" {
}
`, randInt, storage_class)
}
func testAccAWSS3BucketObjectConfig_withTags(randInt int) string {
return fmt.Sprintf(`
resource "aws_s3_bucket" "object_bucket_2" {
bucket = "tf-object-test-bucket-%d"
}
resource "aws_s3_bucket_object" "object" {
bucket = "${aws_s3_bucket.object_bucket_2.bucket}"
key = "test-key"
content = "stuff"
tags {
Key1 = "Value One"
Description = "Very interesting"
}
}
`, randInt)
}

View File

@ -79,3 +79,4 @@ The following attributes are exported:
* `storage_class` - [Storage class](http://docs.aws.amazon.com/AmazonS3/latest/dev/storage-class-intro.html) information of the object. Available for all objects except for `Standard` storage class objects.
* `version_id` - The latest version ID of the object returned.
* `website_redirect_location` - If the bucket is configured as a website, redirects requests for this object to another object in the same bucket or to an external URL. Amazon S3 stores the value of this header in the object metadata.
* `tags` - A mapping of tags assigned to the object.

View File

@ -83,6 +83,7 @@ This attribute is not compatible with `kms_key_id`.
This value is a fully qualified **ARN** of the KMS Key. If using `aws_kms_key`,
use the exported `arn` attribute:
`kms_key_id = "${aws_kms_key.foo.arn}"`
* `tags` - (Optional) A mapping of tags to assign to the object.
Either `source` or `content` must be provided to specify the bucket content.
These two arguments are mutually-exclusive.