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

     1  package aws
     2  
     3  import (
     4  	"log"
     5  
     6  	"github.com/aws/aws-sdk-go/aws"
     7  	elasticsearch "github.com/aws/aws-sdk-go/service/elasticsearchservice"
     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 setTagsElasticsearchService(conn *elasticsearch.ElasticsearchService, d *schema.ResourceData, arn 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 := diffTagsElasticsearchService(tagsFromMapElasticsearchService(o), tagsFromMapElasticsearchService(n))
    19  
    20  		// Set tags
    21  		if len(remove) > 0 {
    22  			log.Printf("[DEBUG] Removing tags: %#v", remove)
    23  			k := make([]*string, 0, len(remove))
    24  			for _, t := range remove {
    25  				k = append(k, t.Key)
    26  			}
    27  			_, err := conn.RemoveTags(&elasticsearch.RemoveTagsInput{
    28  				ARN:     aws.String(arn),
    29  				TagKeys: k,
    30  			})
    31  			if err != nil {
    32  				return err
    33  			}
    34  		}
    35  		if len(create) > 0 {
    36  			log.Printf("[DEBUG] Creating tags: %#v", create)
    37  			_, err := conn.AddTags(&elasticsearch.AddTagsInput{
    38  				ARN:     aws.String(arn),
    39  				TagList: create,
    40  			})
    41  			if err != nil {
    42  				return err
    43  			}
    44  		}
    45  	}
    46  
    47  	return nil
    48  }
    49  
    50  // diffTags takes our tags locally and the ones remotely and returns
    51  // the set of tags that must be created, and the set of tags that must
    52  // be destroyed.
    53  func diffTagsElasticsearchService(oldTags, newTags []*elasticsearch.Tag) ([]*elasticsearch.Tag, []*elasticsearch.Tag) {
    54  	// First, we're creating everything we have
    55  	create := make(map[string]interface{})
    56  	for _, t := range newTags {
    57  		create[*t.Key] = *t.Value
    58  	}
    59  
    60  	// Build the list of what to remove
    61  	var remove []*elasticsearch.Tag
    62  	for _, t := range oldTags {
    63  		old, ok := create[*t.Key]
    64  		if !ok || old != *t.Value {
    65  			// Delete it!
    66  			remove = append(remove, t)
    67  		}
    68  	}
    69  
    70  	return tagsFromMapElasticsearchService(create), remove
    71  }
    72  
    73  // tagsFromMap returns the tags for the given map of data.
    74  func tagsFromMapElasticsearchService(m map[string]interface{}) []*elasticsearch.Tag {
    75  	var result []*elasticsearch.Tag
    76  	for k, v := range m {
    77  		result = append(result, &elasticsearch.Tag{
    78  			Key:   aws.String(k),
    79  			Value: aws.String(v.(string)),
    80  		})
    81  	}
    82  
    83  	return result
    84  }
    85  
    86  // tagsToMap turns the list of tags into a map.
    87  func tagsToMapElasticsearchService(ts []*elasticsearch.Tag) map[string]string {
    88  	result := make(map[string]string)
    89  	for _, t := range ts {
    90  		result[*t.Key] = *t.Value
    91  	}
    92  
    93  	return result
    94  }