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

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	"github.com/aws/aws-sdk-go/service/rds"
     9  	"github.com/hashicorp/terraform/helper/schema"
    10  )
    11  
    12  // setTags is a helper to set the tags for a resource. It expects the
    13  // tags field to be named "tags"
    14  func setTagsRDS(conn *rds.RDS, d *schema.ResourceData, arn string) error {
    15  	if d.HasChange("tags") {
    16  		oraw, nraw := d.GetChange("tags")
    17  		o := oraw.(map[string]interface{})
    18  		n := nraw.(map[string]interface{})
    19  		create, remove := diffTagsRDS(tagsFromMapRDS(o), tagsFromMapRDS(n))
    20  
    21  		// Set tags
    22  		if len(remove) > 0 {
    23  			log.Printf("[DEBUG] Removing tags: %s", remove)
    24  			k := make([]*string, len(remove), len(remove))
    25  			for i, t := range remove {
    26  				k[i] = t.Key
    27  			}
    28  
    29  			_, err := conn.RemoveTagsFromResource(&rds.RemoveTagsFromResourceInput{
    30  				ResourceName: aws.String(arn),
    31  				TagKeys:      k,
    32  			})
    33  			if err != nil {
    34  				return err
    35  			}
    36  		}
    37  		if len(create) > 0 {
    38  			log.Printf("[DEBUG] Creating tags: %s", create)
    39  			_, err := conn.AddTagsToResource(&rds.AddTagsToResourceInput{
    40  				ResourceName: aws.String(arn),
    41  				Tags:         create,
    42  			})
    43  			if err != nil {
    44  				return err
    45  			}
    46  		}
    47  	}
    48  
    49  	return nil
    50  }
    51  
    52  // diffTags takes our tags locally and the ones remotely and returns
    53  // the set of tags that must be created, and the set of tags that must
    54  // be destroyed.
    55  func diffTagsRDS(oldTags, newTags []*rds.Tag) ([]*rds.Tag, []*rds.Tag) {
    56  	// First, we're creating everything we have
    57  	create := make(map[string]interface{})
    58  	for _, t := range newTags {
    59  		create[*t.Key] = *t.Value
    60  	}
    61  
    62  	// Build the list of what to remove
    63  	var remove []*rds.Tag
    64  	for _, t := range oldTags {
    65  		old, ok := create[*t.Key]
    66  		if !ok || old != *t.Value {
    67  			// Delete it!
    68  			remove = append(remove, t)
    69  		}
    70  	}
    71  
    72  	return tagsFromMapRDS(create), remove
    73  }
    74  
    75  // tagsFromMap returns the tags for the given map of data.
    76  func tagsFromMapRDS(m map[string]interface{}) []*rds.Tag {
    77  	result := make([]*rds.Tag, 0, len(m))
    78  	for k, v := range m {
    79  		result = append(result, &rds.Tag{
    80  			Key:   aws.String(k),
    81  			Value: aws.String(v.(string)),
    82  		})
    83  	}
    84  
    85  	return result
    86  }
    87  
    88  // tagsToMap turns the list of tags into a map.
    89  func tagsToMapRDS(ts []*rds.Tag) map[string]string {
    90  	result := make(map[string]string)
    91  	for _, t := range ts {
    92  		result[*t.Key] = *t.Value
    93  	}
    94  
    95  	return result
    96  }
    97  
    98  func saveTagsRDS(conn *rds.RDS, d *schema.ResourceData, arn string) error {
    99  	resp, err := conn.ListTagsForResource(&rds.ListTagsForResourceInput{
   100  		ResourceName: aws.String(arn),
   101  	})
   102  
   103  	if err != nil {
   104  		return fmt.Errorf("[DEBUG] Error retreiving tags for ARN: %s", arn)
   105  	}
   106  
   107  	var dt []*rds.Tag
   108  	if len(resp.TagList) > 0 {
   109  		dt = resp.TagList
   110  	}
   111  
   112  	return d.Set("tags", tagsToMapRDS(dt))
   113  }