Skip to content

Commit

Permalink
provider/aws: Add tag support to ElastiCache
Browse files Browse the repository at this point in the history
  • Loading branch information
catsby committed May 14, 2015
1 parent aad0808 commit d8f3783
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions builtin/providers/aws/resource_aws_elasticache_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"fmt"
"log"
"sort"
"strings"
"time"

"github.com/awslabs/aws-sdk-go/aws"
"github.com/awslabs/aws-sdk-go/service/elasticache"
"github.com/awslabs/aws-sdk-go/service/iam"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
Expand All @@ -17,6 +19,7 @@ func resourceAwsElasticacheCluster() *schema.Resource {
return &schema.Resource{
Create: resourceAwsElasticacheClusterCreate,
Read: resourceAwsElasticacheClusterRead,
Update: resourceAwsElasticacheClusterUpdate,
Delete: resourceAwsElasticacheClusterDelete,

Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -105,6 +108,8 @@ func resourceAwsElasticacheCluster() *schema.Resource {
},
},
},

"tags": tagsSchema(),
},
}
}
Expand All @@ -125,6 +130,7 @@ func resourceAwsElasticacheClusterCreate(d *schema.ResourceData, meta interface{
securityNames := expandStringList(securityNameSet.List())
securityIds := expandStringList(securityIdSet.List())

tags := tagsFromMapEC(d.Get("tags").(map[string]interface{}))
req := &elasticache.CreateCacheClusterInput{
CacheClusterID: aws.String(clusterId),
CacheNodeType: aws.String(nodeType),
Expand All @@ -135,6 +141,7 @@ func resourceAwsElasticacheClusterCreate(d *schema.ResourceData, meta interface{
CacheSubnetGroupName: aws.String(subnetGroupName),
CacheSecurityGroupNames: securityNames,
SecurityGroupIDs: securityIds,
Tags: tags,
}

// parameter groups are optional and can be defaulted by AWS
Expand Down Expand Up @@ -198,11 +205,44 @@ func resourceAwsElasticacheClusterRead(d *schema.ResourceData, meta interface{})
if err := setCacheNodeData(d, c); err != nil {
return err
}
// list tags for resource
// set tags
arn, err := buildECARN(d, meta)
if err != nil {
log.Printf("[DEBUG] Error building ARN for ElastiCache Cluster, not setting Tags for cluster %s", *c.CacheClusterID)
} else {
resp, err := conn.ListTagsForResource(&elasticache.ListTagsForResourceInput{
ResourceName: aws.String(arn),
})

if err != nil {
log.Printf("[DEBUG] Error retreiving tags for ARN: %s", arn)
}

var et []*elasticache.Tag
if len(resp.TagList) > 0 {
et = resp.TagList
}
d.Set("tags", tagsToMapEC(et))
}
}

return nil
}

func resourceAwsElasticacheClusterUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).elasticacheconn
arn, err := buildECARN(d, meta)
if err != nil {
log.Printf("[DEBUG] Error building ARN for ElastiCache Cluster, not updating Tags for cluster %s", *c.CacheClusterID)
} else {
if err := setTagsEC(conn, d, arn); err != nil {
return err
}
}
return resourceAwsElasticacheClusterRead(d, meta)
}

func setCacheNodeData(d *schema.ResourceData, c *elasticache.CacheCluster) error {
sortedCacheNodes := make([]*elasticache.CacheNode, len(c.CacheNodes))
copy(sortedCacheNodes, c.CacheNodes)
Expand Down Expand Up @@ -301,3 +341,17 @@ func CacheClusterStateRefreshFunc(conn *elasticache.ElastiCache, clusterID, give
return c, *c.CacheClusterStatus, nil
}
}

func buildECARN(d *schema.ResourceData, meta interface{}) (string, error) {
iamconn := meta.(*AWSClient).iamconn
region := meta.(*AWSClient).region
// An zero value GetUserInput{} defers to the currently logged in user
resp, err := iamconn.GetUser(&iam.GetUserInput{})
if err != nil {
return "", err
}
userARN := *resp.User.ARN
accountID := strings.Split(userARN, ":")[4]
arn := fmt.Sprintf("arn:aws:elasticache:%s:%s:cluster:%s", region, accountID, d.Id())
return arn, nil
}

0 comments on commit d8f3783

Please sign in to comment.