provider/aws: Add tags to Route53 hosted zones

This commit is contained in:
Clint Shryock 2015-03-26 16:45:23 -05:00
parent 340863b6cc
commit b64a919d83
3 changed files with 205 additions and 1 deletions

View File

@ -16,6 +16,7 @@ func resourceAwsRoute53Zone() *schema.Resource {
return &schema.Resource{
Create: resourceAwsRoute53ZoneCreate,
Read: resourceAwsRoute53ZoneRead,
Update: resourceAwsRoute53ZoneUpdate,
Delete: resourceAwsRoute53ZoneDelete,
Schema: map[string]*schema.Schema{
@ -29,6 +30,8 @@ func resourceAwsRoute53Zone() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"tags": tagsSchema(),
},
}
}
@ -72,7 +75,7 @@ func resourceAwsRoute53ZoneCreate(d *schema.ResourceData, meta interface{}) erro
if err != nil {
return err
}
return nil
return resourceAwsRoute53ZoneUpdate(d, meta)
}
func resourceAwsRoute53ZoneRead(d *schema.ResourceData, meta interface{}) error {
@ -87,9 +90,38 @@ func resourceAwsRoute53ZoneRead(d *schema.ResourceData, meta interface{}) error
return err
}
// get tags
req := &route53.ListTagsForResourceRequest{
ResourceID: aws.String(d.Id()),
ResourceType: aws.String("hostedzone"),
}
resp, err := r53.ListTagsForResource(req)
if err != nil {
return err
}
var tags []route53.Tag
if resp.ResourceTagSet != nil {
tags = resp.ResourceTagSet.Tags
}
d.Set("tags", tagsToMapR53(tags))
return nil
}
func resourceAwsRoute53ZoneUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).r53conn
if err := setTagsR53(conn, d); err != nil {
return err
} else {
d.SetPartial("tags")
}
return resourceAwsRoute53ZoneRead(d, meta)
}
func resourceAwsRoute53ZoneDelete(d *schema.ResourceData, meta interface{}) error {
r53 := meta.(*AWSClient).r53conn

View File

@ -0,0 +1,87 @@
package aws
import (
"log"
"github.com/hashicorp/aws-sdk-go/aws"
"github.com/hashicorp/aws-sdk-go/gen/route53"
"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 setTagsR53(conn *route53.Route53, 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 := diffTagsR53(tagsFromMapR53(o), tagsFromMapR53(n))
// Set tags
r := make([]string, len(remove))
for i, t := range remove {
r[i] = *t.Key
}
log.Printf("[DEBUG] Changing tags: \n\tadding: %#v\n\tremoving:", create, remove)
req := &route53.ChangeTagsForResourceRequest{
AddTags: create,
RemoveTagKeys: r,
ResourceID: aws.String(d.Id()),
ResourceType: aws.String("hostedzone"),
}
resp, err := conn.ChangeTagsForResource(req)
log.Printf("\n\t****\nresp:\n%#v", resp)
if err != nil {
log.Printf("\n\t****\nerror:\n%#v", err)
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.
func diffTagsR53(oldTags, newTags []route53.Tag) ([]route53.Tag, []route53.Tag) {
// 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
var remove []route53.Tag
for _, t := range oldTags {
old, ok := create[*t.Key]
if !ok || old != *t.Value {
// Delete it!
remove = append(remove, t)
}
}
return tagsFromMapR53(create), remove
}
// tagsFromMap returns the tags for the given map of data.
func tagsFromMapR53(m map[string]interface{}) []route53.Tag {
result := make([]route53.Tag, 0, len(m))
for k, v := range m {
result = append(result, route53.Tag{
Key: aws.String(k),
Value: aws.String(v.(string)),
})
}
return result
}
// tagsToMap turns the list of tags into a map.
func tagsToMapR53(ts []route53.Tag) map[string]string {
result := make(map[string]string)
for _, t := range ts {
result[*t.Key] = *t.Value
}
return result
}

View File

@ -0,0 +1,85 @@
package aws
import (
"fmt"
"reflect"
"testing"
"github.com/hashicorp/aws-sdk-go/gen/ec2"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
func TestDiffTagsR53(t *testing.T) {
cases := []struct {
Old, New map[string]interface{}
Create, Remove map[string]string
}{
// Basic add/remove
{
Old: map[string]interface{}{
"foo": "bar",
},
New: map[string]interface{}{
"bar": "baz",
},
Create: map[string]string{
"bar": "baz",
},
Remove: map[string]string{
"foo": "bar",
},
},
// Modify
{
Old: map[string]interface{}{
"foo": "bar",
},
New: map[string]interface{}{
"foo": "baz",
},
Create: map[string]string{
"foo": "baz",
},
Remove: map[string]string{
"foo": "bar",
},
},
}
for i, tc := range cases {
c, r := diffTags(tagsFromMap(tc.Old), tagsFromMap(tc.New))
cm := tagsToMap(c)
rm := tagsToMap(r)
if !reflect.DeepEqual(cm, tc.Create) {
t.Fatalf("%d: bad create: %#v", i, cm)
}
if !reflect.DeepEqual(rm, tc.Remove) {
t.Fatalf("%d: bad remove: %#v", i, rm)
}
}
}
// testAccCheckTags can be used to check the tags on a resource.
func testAccCheckTagsR53(
ts *[]ec2.Tag, key string, value string) resource.TestCheckFunc {
return func(s *terraform.State) error {
m := tagsToMap(*ts)
v, ok := m[key]
if value != "" && !ok {
return fmt.Errorf("Missing tag: %s", key)
} else if value == "" && ok {
return fmt.Errorf("Extra tag: %s", key)
}
if value == "" {
return nil
}
if v != value {
return fmt.Errorf("%s: bad value: %s", key, v)
}
return nil
}
}