Skip to content

Commit

Permalink
Merge pull request #1965 from hashicorp/f-export-cache-cluster-endpoints
Browse files Browse the repository at this point in the history
provider/aws: export elasticache nodes
  • Loading branch information
catsby committed May 14, 2015
2 parents 533933f + d81e63c commit 0b548a4
Show file tree
Hide file tree
Showing 4 changed files with 305 additions and 5 deletions.
123 changes: 119 additions & 4 deletions builtin/providers/aws/resource_aws_elasticache_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ package aws
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 @@ -16,6 +19,7 @@ func resourceAwsElasticacheCluster() *schema.Resource {
return &schema.Resource{
Create: resourceAwsElasticacheClusterCreate,
Read: resourceAwsElasticacheClusterRead,
Update: resourceAwsElasticacheClusterUpdate,
Delete: resourceAwsElasticacheClusterDelete,

Schema: map[string]*schema.Schema{
Expand All @@ -42,6 +46,7 @@ func resourceAwsElasticacheCluster() *schema.Resource {
"parameter_group_name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"port": &schema.Schema{
Expand Down Expand Up @@ -82,6 +87,29 @@ func resourceAwsElasticacheCluster() *schema.Resource {
return hashcode.String(v.(string))
},
},
// Exported Attributes
"cache_nodes": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"address": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"port": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
},
},
},
},

"tags": tagsSchema(),
},
}
}
Expand All @@ -98,11 +126,11 @@ func resourceAwsElasticacheClusterCreate(d *schema.ResourceData, meta interface{
subnetGroupName := d.Get("subnet_group_name").(string)
securityNameSet := d.Get("security_group_names").(*schema.Set)
securityIdSet := d.Get("security_group_ids").(*schema.Set)
paramGroupName := d.Get("parameter_group_name").(string) // default.memcached1.4

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 @@ -113,7 +141,12 @@ func resourceAwsElasticacheClusterCreate(d *schema.ResourceData, meta interface{
CacheSubnetGroupName: aws.String(subnetGroupName),
CacheSecurityGroupNames: securityNames,
SecurityGroupIDs: securityIds,
CacheParameterGroupName: aws.String(paramGroupName),
Tags: tags,
}

// parameter groups are optional and can be defaulted by AWS
if v, ok := d.GetOk("parameter_group_name"); ok {
req.CacheParameterGroupName = aws.String(v.(string))
}

_, err := conn.CreateCacheCluster(req)
Expand All @@ -139,13 +172,14 @@ func resourceAwsElasticacheClusterCreate(d *schema.ResourceData, meta interface{

d.SetId(clusterId)

return nil
return resourceAwsElasticacheClusterRead(d, meta)
}

func resourceAwsElasticacheClusterRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).elasticacheconn
req := &elasticache.DescribeCacheClustersInput{
CacheClusterID: aws.String(d.Id()),
CacheClusterID: aws.String(d.Id()),
ShowCacheNodeInfo: aws.Boolean(true),
}

res, err := conn.DescribeCacheClusters(req)
Expand All @@ -167,11 +201,78 @@ func resourceAwsElasticacheClusterRead(d *schema.ResourceData, meta interface{})
d.Set("security_group_names", c.CacheSecurityGroups)
d.Set("security_group_ids", c.SecurityGroups)
d.Set("parameter_group_name", c.CacheParameterGroup)

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", d.Id())
} 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)
sort.Sort(byCacheNodeId(sortedCacheNodes))

cacheNodeData := make([]map[string]interface{}, 0, len(sortedCacheNodes))

for _, node := range sortedCacheNodes {
if node.CacheNodeID == nil || node.Endpoint == nil || node.Endpoint.Address == nil || node.Endpoint.Port == nil {
return fmt.Errorf("Unexpected nil pointer in: %#v", node)
}
cacheNodeData = append(cacheNodeData, map[string]interface{}{
"id": *node.CacheNodeID,
"address": *node.Endpoint.Address,
"port": int(*node.Endpoint.Port),
})
}

return d.Set("cache_nodes", cacheNodeData)
}

type byCacheNodeId []*elasticache.CacheNode

func (b byCacheNodeId) Len() int { return len(b) }
func (b byCacheNodeId) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
func (b byCacheNodeId) Less(i, j int) bool {
return b[i].CacheNodeID != nil && b[j].CacheNodeID != nil &&
*b[i].CacheNodeID < *b[j].CacheNodeID
}

func resourceAwsElasticacheClusterDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).elasticacheconn

Expand Down Expand Up @@ -240,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
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/hashicorp/terraform/terraform"
)

func TestAccAWSElasticacheCluster(t *testing.T) {
func TestAccAWSElasticacheCluster_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Expand All @@ -23,6 +23,8 @@ func TestAccAWSElasticacheCluster(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSElasticacheSecurityGroupExists("aws_elasticache_security_group.bar"),
testAccCheckAWSElasticacheClusterExists("aws_elasticache_cluster.bar"),
resource.TestCheckResourceAttr(
"aws_elasticache_cluster.bar", "cache_nodes.0.id", "0001"),
),
},
},
Expand Down Expand Up @@ -93,6 +95,9 @@ func genRandInt() int {
}

var testAccAWSElasticacheClusterConfig = fmt.Sprintf(`
provider "aws" {
region = "us-east-1"
}
resource "aws_security_group" "bar" {
name = "tf-test-security-group-%03d"
description = "tf-test-security-group-descr"
Expand Down
95 changes: 95 additions & 0 deletions builtin/providers/aws/tagsEC.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package aws

import (
"log"

"github.com/awslabs/aws-sdk-go/aws"
"github.com/awslabs/aws-sdk-go/service/elasticache"
"github.com/hashicorp/terraform/helper/schema"
)

// setTags is a helper to set the tags for a resource. It expects the
// tags field to be named "tags"
func setTagsEC(conn *elasticache.ElastiCache, d *schema.ResourceData, arn string) error {
if d.HasChange("tags") {
oraw, nraw := d.GetChange("tags")
o := oraw.(map[string]interface{})
n := nraw.(map[string]interface{})
create, remove := diffTagsEC(tagsFromMapEC(o), tagsFromMapEC(n))

// Set tags
if len(remove) > 0 {
log.Printf("[DEBUG] Removing tags: %#v", remove)
k := make([]*string, len(remove), len(remove))
for i, t := range remove {
k[i] = t.Key
}

_, err := conn.RemoveTagsFromResource(&elasticache.RemoveTagsFromResourceInput{
ResourceName: aws.String(arn),
TagKeys: k,
})
if err != nil {
return err
}
}
if len(create) > 0 {
log.Printf("[DEBUG] Creating tags: %#v", create)
_, err := conn.AddTagsToResource(&elasticache.AddTagsToResourceInput{
ResourceName: aws.String(arn),
Tags: create,
})
if err != nil {
return err
}
}
}

return nil
}

// diffTags takes our tags locally and the ones remotely and returns
// the set of tags that must be created, and the set of tags that must
// be destroyed.
func diffTagsEC(oldTags, newTags []*elasticache.Tag) ([]*elasticache.Tag, []*elasticache.Tag) {
// First, we're creating everything we have
create := make(map[string]interface{})
for _, t := range newTags {
create[*t.Key] = *t.Value
}

// Build the list of what to remove
var remove []*elasticache.Tag
for _, t := range oldTags {
old, ok := create[*t.Key]
if !ok || old != *t.Value {
// Delete it!
remove = append(remove, t)
}
}

return tagsFromMapEC(create), remove
}

// tagsFromMap returns the tags for the given map of data.
func tagsFromMapEC(m map[string]interface{}) []*elasticache.Tag {
result := make([]*elasticache.Tag, 0, len(m))
for k, v := range m {
result = append(result, &elasticache.Tag{
Key: aws.String(k),
Value: aws.String(v.(string)),
})
}

return result
}

// tagsToMap turns the list of tags into a map.
func tagsToMapEC(ts []*elasticache.Tag) map[string]string {
result := make(map[string]string)
for _, t := range ts {
result[*t.Key] = *t.Value
}

return result
}
Loading

0 comments on commit 0b548a4

Please sign in to comment.