2015-03-26 15:17:27 +01:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
2015-04-14 18:07:05 +02:00
|
|
|
"github.com/awslabs/aws-sdk-go/aws"
|
2015-05-20 13:21:23 +02:00
|
|
|
"github.com/awslabs/aws-sdk-go/aws/awserr"
|
2015-04-14 18:07:05 +02:00
|
|
|
"github.com/awslabs/aws-sdk-go/service/s3"
|
2015-03-26 15:17:27 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
// setTags is a helper to set the tags for a resource. It expects the
|
|
|
|
// tags field to be named "tags"
|
|
|
|
func setTagsS3(conn *s3.S3, d *schema.ResourceData) error {
|
|
|
|
if d.HasChange("tags") {
|
|
|
|
oraw, nraw := d.GetChange("tags")
|
|
|
|
o := oraw.(map[string]interface{})
|
|
|
|
n := nraw.(map[string]interface{})
|
|
|
|
create, remove := diffTagsS3(tagsFromMapS3(o), tagsFromMapS3(n))
|
|
|
|
|
|
|
|
// Set tags
|
|
|
|
if len(remove) > 0 {
|
|
|
|
log.Printf("[DEBUG] Removing tags: %#v", remove)
|
2015-04-14 18:07:05 +02:00
|
|
|
_, err := conn.DeleteBucketTagging(&s3.DeleteBucketTaggingInput{
|
2015-03-26 15:17:27 +01:00
|
|
|
Bucket: aws.String(d.Get("bucket").(string)),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(create) > 0 {
|
|
|
|
log.Printf("[DEBUG] Creating tags: %#v", create)
|
2015-04-14 18:07:05 +02:00
|
|
|
req := &s3.PutBucketTaggingInput{
|
|
|
|
Bucket: aws.String(d.Get("bucket").(string)),
|
|
|
|
Tagging: &s3.Tagging{
|
|
|
|
TagSet: create,
|
2015-03-26 15:17:27 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-04-14 18:07:05 +02:00
|
|
|
_, err := conn.PutBucketTagging(req)
|
2015-03-26 15:17:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// diffTags takes our tags locally and the ones remotely and returns
|
|
|
|
// the set of tags that must be created, and the set of tags that must
|
|
|
|
// be destroyed.
|
2015-04-14 18:07:05 +02:00
|
|
|
func diffTagsS3(oldTags, newTags []*s3.Tag) ([]*s3.Tag, []*s3.Tag) {
|
2015-03-26 15:17:27 +01:00
|
|
|
// First, we're creating everything we have
|
|
|
|
create := make(map[string]interface{})
|
|
|
|
for _, t := range newTags {
|
|
|
|
create[*t.Key] = *t.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the list of what to remove
|
2015-04-14 18:07:05 +02:00
|
|
|
var remove []*s3.Tag
|
2015-03-26 15:17:27 +01:00
|
|
|
for _, t := range oldTags {
|
|
|
|
old, ok := create[*t.Key]
|
|
|
|
if !ok || old != *t.Value {
|
|
|
|
// Delete it!
|
|
|
|
remove = append(remove, t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tagsFromMapS3(create), remove
|
|
|
|
}
|
|
|
|
|
|
|
|
// tagsFromMap returns the tags for the given map of data.
|
2015-04-14 18:07:05 +02:00
|
|
|
func tagsFromMapS3(m map[string]interface{}) []*s3.Tag {
|
|
|
|
result := make([]*s3.Tag, 0, len(m))
|
2015-03-26 15:17:27 +01:00
|
|
|
for k, v := range m {
|
2015-04-14 18:07:05 +02:00
|
|
|
result = append(result, &s3.Tag{
|
2015-03-26 15:17:27 +01:00
|
|
|
Key: aws.String(k),
|
|
|
|
Value: aws.String(v.(string)),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// tagsToMap turns the list of tags into a map.
|
2015-04-14 18:07:05 +02:00
|
|
|
func tagsToMapS3(ts []*s3.Tag) map[string]string {
|
2015-03-26 15:17:27 +01:00
|
|
|
result := make(map[string]string)
|
|
|
|
for _, t := range ts {
|
|
|
|
result[*t.Key] = *t.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
2015-04-01 14:57:50 +02:00
|
|
|
|
|
|
|
// return a slice of s3 tags associated with the given s3 bucket. Essentially
|
|
|
|
// s3.GetBucketTagging, except returns an empty slice instead of an error when
|
|
|
|
// there are no tags.
|
2015-04-14 18:07:05 +02:00
|
|
|
func getTagSetS3(s3conn *s3.S3, bucket string) ([]*s3.Tag, error) {
|
|
|
|
request := &s3.GetBucketTaggingInput{
|
2015-04-01 14:57:50 +02:00
|
|
|
Bucket: aws.String(bucket),
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := s3conn.GetBucketTagging(request)
|
2015-05-20 13:21:23 +02:00
|
|
|
if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "NoSuchTagSet" {
|
2015-04-01 14:57:50 +02:00
|
|
|
// There is no tag set associated with the bucket.
|
2015-04-14 18:07:05 +02:00
|
|
|
return []*s3.Tag{}, nil
|
2015-04-01 14:57:50 +02:00
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.TagSet, nil
|
|
|
|
}
|