2014-10-09 02:54:00 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
2015-04-16 22:31:40 +02:00
|
|
|
"github.com/awslabs/aws-sdk-go/aws"
|
|
|
|
"github.com/awslabs/aws-sdk-go/service/ec2"
|
2014-10-09 02:54:00 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
// tagsSchema returns the schema to use for tags.
|
2015-03-12 22:43:08 +01:00
|
|
|
//
|
2014-10-09 02:54:00 +02:00
|
|
|
func tagsSchema() *schema.Schema {
|
|
|
|
return &schema.Schema{
|
|
|
|
Type: schema.TypeMap,
|
|
|
|
Optional: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// setTags is a helper to set the tags for a resource. It expects the
|
|
|
|
// tags field to be named "tags"
|
2015-05-12 21:58:10 +02:00
|
|
|
func setTags(conn *ec2.EC2, d *schema.ResourceData) error {
|
2014-10-09 02:54:00 +02:00
|
|
|
if d.HasChange("tags") {
|
|
|
|
oraw, nraw := d.GetChange("tags")
|
|
|
|
o := oraw.(map[string]interface{})
|
|
|
|
n := nraw.(map[string]interface{})
|
2015-05-12 21:58:10 +02:00
|
|
|
create, remove := diffTags(tagsFromMap(o), tagsFromMap(n))
|
2014-10-09 02:54:00 +02:00
|
|
|
|
|
|
|
// Set tags
|
|
|
|
if len(remove) > 0 {
|
2015-05-29 09:05:21 +02:00
|
|
|
log.Printf("[DEBUG] Removing tags: %#v from %s", remove, d.Id())
|
2015-04-16 22:31:40 +02:00
|
|
|
_, err := conn.DeleteTags(&ec2.DeleteTagsInput{
|
|
|
|
Resources: []*string{aws.String(d.Id())},
|
2015-03-12 22:43:08 +01:00
|
|
|
Tags: remove,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2014-10-09 02:54:00 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(create) > 0 {
|
2015-05-29 09:05:21 +02:00
|
|
|
log.Printf("[DEBUG] Creating tags: %#v for %s", create, d.Id())
|
2015-04-16 22:31:40 +02:00
|
|
|
_, err := conn.CreateTags(&ec2.CreateTagsInput{
|
|
|
|
Resources: []*string{aws.String(d.Id())},
|
2015-03-12 22:43:08 +01:00
|
|
|
Tags: create,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2014-10-09 02:54:00 +02:00
|
|
|
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-05-12 21:58:10 +02:00
|
|
|
func diffTags(oldTags, newTags []*ec2.Tag) ([]*ec2.Tag, []*ec2.Tag) {
|
2014-10-09 02:54:00 +02:00
|
|
|
// First, we're creating everything we have
|
|
|
|
create := make(map[string]interface{})
|
|
|
|
for _, t := range newTags {
|
2015-03-12 22:43:08 +01:00
|
|
|
create[*t.Key] = *t.Value
|
2014-10-09 02:54:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build the list of what to remove
|
2015-04-16 22:31:40 +02:00
|
|
|
var remove []*ec2.Tag
|
2014-10-09 02:54:00 +02:00
|
|
|
for _, t := range oldTags {
|
2015-03-12 22:43:08 +01:00
|
|
|
old, ok := create[*t.Key]
|
|
|
|
if !ok || old != *t.Value {
|
2014-10-09 02:54:00 +02:00
|
|
|
// Delete it!
|
|
|
|
remove = append(remove, t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-12 21:58:10 +02:00
|
|
|
return tagsFromMap(create), remove
|
2014-10-09 02:54:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// tagsFromMap returns the tags for the given map of data.
|
2015-05-12 21:58:10 +02:00
|
|
|
func tagsFromMap(m map[string]interface{}) []*ec2.Tag {
|
2015-04-16 22:31:40 +02:00
|
|
|
result := make([]*ec2.Tag, 0, len(m))
|
2014-10-09 02:54:00 +02:00
|
|
|
for k, v := range m {
|
2015-04-16 22:31:40 +02:00
|
|
|
result = append(result, &ec2.Tag{
|
2015-03-12 22:43:08 +01:00
|
|
|
Key: aws.String(k),
|
|
|
|
Value: aws.String(v.(string)),
|
2014-10-09 02:54:00 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// tagsToMap turns the list of tags into a map.
|
2015-05-12 21:58:10 +02:00
|
|
|
func tagsToMap(ts []*ec2.Tag) map[string]string {
|
2014-10-09 02:54:00 +02:00
|
|
|
result := make(map[string]string)
|
|
|
|
for _, t := range ts {
|
2015-03-12 22:43:08 +01:00
|
|
|
result[*t.Key] = *t.Value
|
2014-10-09 02:54:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|