github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/aws/tags_route53.go (about)

     1  package aws
     2  
     3  import (
     4  	"log"
     5  
     6  	"github.com/aws/aws-sdk-go/aws"
     7  	"github.com/aws/aws-sdk-go/service/route53"
     8  	"github.com/hashicorp/terraform/helper/schema"
     9  )
    10  
    11  // setTags is a helper to set the tags for a resource. It expects the
    12  // tags field to be named "tags"
    13  func setTagsR53(conn *route53.Route53, d *schema.ResourceData, resourceType string) error {
    14  	if d.HasChange("tags") {
    15  		oraw, nraw := d.GetChange("tags")
    16  		o := oraw.(map[string]interface{})
    17  		n := nraw.(map[string]interface{})
    18  		create, remove := diffTagsR53(tagsFromMapR53(o), tagsFromMapR53(n))
    19  
    20  		// Set tags
    21  		r := make([]*string, len(remove))
    22  		for i, t := range remove {
    23  			r[i] = t.Key
    24  		}
    25  		log.Printf("[DEBUG] Changing tags: \n\tadding: %#v\n\tremoving:%#v", create, remove)
    26  		req := &route53.ChangeTagsForResourceInput{
    27  			ResourceId:   aws.String(d.Id()),
    28  			ResourceType: aws.String(resourceType),
    29  		}
    30  
    31  		if len(create) > 0 {
    32  			req.AddTags = create
    33  		}
    34  		if len(r) > 0 {
    35  			req.RemoveTagKeys = r
    36  		}
    37  
    38  		_, err := conn.ChangeTagsForResource(req)
    39  		if err != nil {
    40  			return err
    41  		}
    42  	}
    43  
    44  	return nil
    45  }
    46  
    47  // diffTags takes our tags locally and the ones remotely and returns
    48  // the set of tags that must be created, and the set of tags that must
    49  // be destroyed.
    50  func diffTagsR53(oldTags, newTags []*route53.Tag) ([]*route53.Tag, []*route53.Tag) {
    51  	// First, we're creating everything we have
    52  	create := make(map[string]interface{})
    53  	for _, t := range newTags {
    54  		create[*t.Key] = *t.Value
    55  	}
    56  
    57  	// Build the list of what to remove
    58  	var remove []*route53.Tag
    59  	for _, t := range oldTags {
    60  		old, ok := create[*t.Key]
    61  		if !ok || old != *t.Value {
    62  			// Delete it!
    63  			remove = append(remove, t)
    64  		}
    65  	}
    66  
    67  	return tagsFromMapR53(create), remove
    68  }
    69  
    70  // tagsFromMap returns the tags for the given map of data.
    71  func tagsFromMapR53(m map[string]interface{}) []*route53.Tag {
    72  	result := make([]*route53.Tag, 0, len(m))
    73  	for k, v := range m {
    74  		result = append(result, &route53.Tag{
    75  			Key:   aws.String(k),
    76  			Value: aws.String(v.(string)),
    77  		})
    78  	}
    79  
    80  	return result
    81  }
    82  
    83  // tagsToMap turns the list of tags into a map.
    84  func tagsToMapR53(ts []*route53.Tag) map[string]string {
    85  	result := make(map[string]string)
    86  	for _, t := range ts {
    87  		result[*t.Key] = *t.Value
    88  	}
    89  
    90  	return result
    91  }