github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/aws/tagsCloudtrail.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/cloudtrail"
     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 setTagsCloudtrail(conn *cloudtrail.CloudTrail, d *schema.ResourceData) 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 := diffTagsCloudtrail(tagsFromMapCloudtrail(o), tagsFromMapCloudtrail(n))
    19  
    20  		// Set tags
    21  		if len(remove) > 0 {
    22  			input := cloudtrail.RemoveTagsInput{
    23  				ResourceId: aws.String(d.Get("arn").(string)),
    24  				TagsList:   remove,
    25  			}
    26  			log.Printf("[DEBUG] Removing CloudTrail tags: %s", input)
    27  			_, err := conn.RemoveTags(&input)
    28  			if err != nil {
    29  				return err
    30  			}
    31  		}
    32  		if len(create) > 0 {
    33  			input := cloudtrail.AddTagsInput{
    34  				ResourceId: aws.String(d.Get("arn").(string)),
    35  				TagsList:   create,
    36  			}
    37  			log.Printf("[DEBUG] Adding CloudTrail tags: %s", input)
    38  			_, err := conn.AddTags(&input)
    39  			if err != nil {
    40  				return err
    41  			}
    42  		}
    43  	}
    44  
    45  	return nil
    46  }
    47  
    48  // diffTags takes our tags locally and the ones remotely and returns
    49  // the set of tags that must be created, and the set of tags that must
    50  // be destroyed.
    51  func diffTagsCloudtrail(oldTags, newTags []*cloudtrail.Tag) ([]*cloudtrail.Tag, []*cloudtrail.Tag) {
    52  	// First, we're creating everything we have
    53  	create := make(map[string]interface{})
    54  	for _, t := range newTags {
    55  		create[*t.Key] = *t.Value
    56  	}
    57  
    58  	// Build the list of what to remove
    59  	var remove []*cloudtrail.Tag
    60  	for _, t := range oldTags {
    61  		old, ok := create[*t.Key]
    62  		if !ok || old != *t.Value {
    63  			// Delete it!
    64  			remove = append(remove, t)
    65  		}
    66  	}
    67  
    68  	return tagsFromMapCloudtrail(create), remove
    69  }
    70  
    71  // tagsFromMap returns the tags for the given map of data.
    72  func tagsFromMapCloudtrail(m map[string]interface{}) []*cloudtrail.Tag {
    73  	var result []*cloudtrail.Tag
    74  	for k, v := range m {
    75  		result = append(result, &cloudtrail.Tag{
    76  			Key:   aws.String(k),
    77  			Value: aws.String(v.(string)),
    78  		})
    79  	}
    80  
    81  	return result
    82  }
    83  
    84  // tagsToMap turns the list of tags into a map.
    85  func tagsToMapCloudtrail(ts []*cloudtrail.Tag) map[string]string {
    86  	result := make(map[string]string)
    87  	for _, t := range ts {
    88  		result[*t.Key] = *t.Value
    89  	}
    90  
    91  	return result
    92  }