2015-04-26 03:53:21 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2015-05-13 00:05:03 +02:00
|
|
|
"sort"
|
2015-05-14 18:44:24 +02:00
|
|
|
"strings"
|
2015-04-26 03:53:21 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/awslabs/aws-sdk-go/aws"
|
2015-05-20 13:21:23 +02:00
|
|
|
"github.com/awslabs/aws-sdk-go/aws/awserr"
|
2015-04-26 03:53:21 +02:00
|
|
|
"github.com/awslabs/aws-sdk-go/service/elasticache"
|
2015-05-14 18:44:24 +02:00
|
|
|
"github.com/awslabs/aws-sdk-go/service/iam"
|
2015-04-26 03:53:21 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/hashcode"
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
2015-04-28 18:30:15 +02:00
|
|
|
func resourceAwsElasticacheCluster() *schema.Resource {
|
2015-04-26 03:53:21 +02:00
|
|
|
return &schema.Resource{
|
2015-04-28 18:30:15 +02:00
|
|
|
Create: resourceAwsElasticacheClusterCreate,
|
|
|
|
Read: resourceAwsElasticacheClusterRead,
|
2015-05-14 18:44:24 +02:00
|
|
|
Update: resourceAwsElasticacheClusterUpdate,
|
2015-04-28 18:30:15 +02:00
|
|
|
Delete: resourceAwsElasticacheClusterDelete,
|
2015-04-26 03:53:21 +02:00
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"cluster_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
"engine": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
"node_type": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
"num_cache_nodes": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
"parameter_group_name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2015-05-14 18:12:07 +02:00
|
|
|
Computed: true,
|
2015-04-26 03:53:21 +02:00
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
"port": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Default: 11211,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
"engine_version": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2015-05-07 15:42:28 +02:00
|
|
|
Computed: true,
|
2015-04-26 03:53:21 +02:00
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
"subnet_group_name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2015-05-08 18:27:43 +02:00
|
|
|
Computed: true,
|
2015-04-26 03:53:21 +02:00
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
"security_group_names": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
Set: func(v interface{}) int {
|
|
|
|
return hashcode.String(v.(string))
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"security_group_ids": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
Set: func(v interface{}) int {
|
|
|
|
return hashcode.String(v.(string))
|
|
|
|
},
|
|
|
|
},
|
2015-05-13 00:05:03 +02:00
|
|
|
// 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,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-05-14 18:44:24 +02:00
|
|
|
|
|
|
|
"tags": tagsSchema(),
|
2015-04-26 03:53:21 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-28 18:30:15 +02:00
|
|
|
func resourceAwsElasticacheClusterCreate(d *schema.ResourceData, meta interface{}) error {
|
2015-04-26 03:53:21 +02:00
|
|
|
conn := meta.(*AWSClient).elasticacheconn
|
|
|
|
|
|
|
|
clusterId := d.Get("cluster_id").(string)
|
|
|
|
nodeType := d.Get("node_type").(string) // e.g) cache.m1.small
|
|
|
|
numNodes := int64(d.Get("num_cache_nodes").(int)) // 2
|
|
|
|
engine := d.Get("engine").(string) // memcached
|
|
|
|
engineVersion := d.Get("engine_version").(string) // 1.4.14
|
|
|
|
port := int64(d.Get("port").(int)) // 11211
|
|
|
|
subnetGroupName := d.Get("subnet_group_name").(string)
|
|
|
|
securityNameSet := d.Get("security_group_names").(*schema.Set)
|
|
|
|
securityIdSet := d.Get("security_group_ids").(*schema.Set)
|
|
|
|
|
|
|
|
securityNames := expandStringList(securityNameSet.List())
|
|
|
|
securityIds := expandStringList(securityIdSet.List())
|
|
|
|
|
2015-05-14 18:44:24 +02:00
|
|
|
tags := tagsFromMapEC(d.Get("tags").(map[string]interface{}))
|
2015-04-26 03:53:21 +02:00
|
|
|
req := &elasticache.CreateCacheClusterInput{
|
|
|
|
CacheClusterID: aws.String(clusterId),
|
|
|
|
CacheNodeType: aws.String(nodeType),
|
|
|
|
NumCacheNodes: aws.Long(numNodes),
|
|
|
|
Engine: aws.String(engine),
|
|
|
|
EngineVersion: aws.String(engineVersion),
|
|
|
|
Port: aws.Long(port),
|
|
|
|
CacheSubnetGroupName: aws.String(subnetGroupName),
|
|
|
|
CacheSecurityGroupNames: securityNames,
|
|
|
|
SecurityGroupIDs: securityIds,
|
2015-05-14 18:44:24 +02:00
|
|
|
Tags: tags,
|
2015-05-14 18:12:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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))
|
2015-04-26 03:53:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err := conn.CreateCacheCluster(req)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating Elasticache: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
pending := []string{"creating"}
|
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Pending: pending,
|
|
|
|
Target: "available",
|
|
|
|
Refresh: CacheClusterStateRefreshFunc(conn, d.Id(), "available", pending),
|
|
|
|
Timeout: 10 * time.Minute,
|
|
|
|
Delay: 10 * time.Second,
|
|
|
|
MinTimeout: 3 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Waiting for state to become available: %v", d.Id())
|
|
|
|
_, sterr := stateConf.WaitForState()
|
|
|
|
if sterr != nil {
|
|
|
|
return fmt.Errorf("Error waiting for elasticache (%s) to be created: %s", d.Id(), sterr)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId(clusterId)
|
|
|
|
|
2015-05-14 18:12:07 +02:00
|
|
|
return resourceAwsElasticacheClusterRead(d, meta)
|
2015-04-26 03:53:21 +02:00
|
|
|
}
|
|
|
|
|
2015-04-28 18:30:15 +02:00
|
|
|
func resourceAwsElasticacheClusterRead(d *schema.ResourceData, meta interface{}) error {
|
2015-04-26 03:53:21 +02:00
|
|
|
conn := meta.(*AWSClient).elasticacheconn
|
|
|
|
req := &elasticache.DescribeCacheClustersInput{
|
2015-05-14 18:12:07 +02:00
|
|
|
CacheClusterID: aws.String(d.Id()),
|
|
|
|
ShowCacheNodeInfo: aws.Boolean(true),
|
2015-04-26 03:53:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
res, err := conn.DescribeCacheClusters(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(res.CacheClusters) == 1 {
|
|
|
|
c := res.CacheClusters[0]
|
|
|
|
d.Set("cluster_id", c.CacheClusterID)
|
|
|
|
d.Set("node_type", c.CacheNodeType)
|
|
|
|
d.Set("num_cache_nodes", c.NumCacheNodes)
|
|
|
|
d.Set("engine", c.Engine)
|
|
|
|
d.Set("engine_version", c.EngineVersion)
|
|
|
|
if c.ConfigurationEndpoint != nil {
|
|
|
|
d.Set("port", c.ConfigurationEndpoint.Port)
|
|
|
|
}
|
|
|
|
d.Set("subnet_group_name", c.CacheSubnetGroupName)
|
|
|
|
d.Set("security_group_names", c.CacheSecurityGroups)
|
|
|
|
d.Set("security_group_ids", c.SecurityGroups)
|
|
|
|
d.Set("parameter_group_name", c.CacheParameterGroup)
|
2015-05-13 00:05:03 +02:00
|
|
|
|
|
|
|
if err := setCacheNodeData(d, c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-14 18:44:24 +02:00
|
|
|
// 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))
|
|
|
|
}
|
2015-04-26 03:53:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-14 18:44:24 +02:00
|
|
|
func resourceAwsElasticacheClusterUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).elasticacheconn
|
|
|
|
arn, err := buildECARN(d, meta)
|
|
|
|
if err != nil {
|
2015-05-14 18:51:08 +02:00
|
|
|
log.Printf("[DEBUG] Error building ARN for ElastiCache Cluster, not updating Tags for cluster %s", d.Id())
|
2015-05-14 18:44:24 +02:00
|
|
|
} else {
|
|
|
|
if err := setTagsEC(conn, d, arn); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return resourceAwsElasticacheClusterRead(d, meta)
|
|
|
|
}
|
|
|
|
|
2015-05-13 00:05:03 +02:00
|
|
|
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{}{
|
2015-05-14 18:12:07 +02:00
|
|
|
"id": *node.CacheNodeID,
|
|
|
|
"address": *node.Endpoint.Address,
|
|
|
|
"port": int(*node.Endpoint.Port),
|
2015-05-13 00:05:03 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-04-28 18:30:15 +02:00
|
|
|
func resourceAwsElasticacheClusterDelete(d *schema.ResourceData, meta interface{}) error {
|
2015-04-26 03:53:21 +02:00
|
|
|
conn := meta.(*AWSClient).elasticacheconn
|
|
|
|
|
|
|
|
req := &elasticache.DeleteCacheClusterInput{
|
|
|
|
CacheClusterID: aws.String(d.Id()),
|
|
|
|
}
|
|
|
|
_, err := conn.DeleteCacheCluster(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Waiting for deletion: %v", d.Id())
|
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Pending: []string{"creating", "available", "deleting", "incompatible-parameters", "incompatible-network", "restore-failed"},
|
|
|
|
Target: "",
|
|
|
|
Refresh: CacheClusterStateRefreshFunc(conn, d.Id(), "", []string{}),
|
|
|
|
Timeout: 10 * time.Minute,
|
|
|
|
Delay: 10 * time.Second,
|
|
|
|
MinTimeout: 3 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, sterr := stateConf.WaitForState()
|
|
|
|
if sterr != nil {
|
|
|
|
return fmt.Errorf("Error waiting for elasticache (%s) to delete: %s", d.Id(), sterr)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId("")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func CacheClusterStateRefreshFunc(conn *elasticache.ElastiCache, clusterID, givenState string, pending []string) resource.StateRefreshFunc {
|
|
|
|
return func() (interface{}, string, error) {
|
|
|
|
resp, err := conn.DescribeCacheClusters(&elasticache.DescribeCacheClustersInput{
|
2015-05-29 00:36:21 +02:00
|
|
|
CacheClusterID: aws.String(clusterID),
|
|
|
|
ShowCacheNodeInfo: aws.Boolean(true),
|
2015-04-26 03:53:21 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
2015-05-20 13:21:23 +02:00
|
|
|
apierr := err.(awserr.Error)
|
|
|
|
log.Printf("[DEBUG] message: %v, code: %v", apierr.Message(), apierr.Code())
|
|
|
|
if apierr.Message() == fmt.Sprintf("CacheCluster not found: %v", clusterID) {
|
2015-04-26 03:53:21 +02:00
|
|
|
log.Printf("[DEBUG] Detect deletion")
|
|
|
|
return nil, "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[ERROR] CacheClusterStateRefreshFunc: %s", err)
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
c := resp.CacheClusters[0]
|
|
|
|
log.Printf("[DEBUG] status: %v", *c.CacheClusterStatus)
|
|
|
|
|
|
|
|
// return the current state if it's in the pending array
|
|
|
|
for _, p := range pending {
|
|
|
|
s := *c.CacheClusterStatus
|
|
|
|
if p == s {
|
|
|
|
log.Printf("[DEBUG] Return with status: %v", *c.CacheClusterStatus)
|
|
|
|
return c, p, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// return given state if it's not in pending
|
|
|
|
if givenState != "" {
|
2015-05-29 17:09:54 +02:00
|
|
|
// check to make sure we have the node count we're expecting
|
|
|
|
if int64(len(c.CacheNodes)) != *c.NumCacheNodes {
|
|
|
|
log.Printf("[DEBUG] Node count is not what is expected: %d found, %d expected", len(c.CacheNodes), *c.NumCacheNodes)
|
|
|
|
return nil, "creating", nil
|
|
|
|
}
|
2015-05-29 00:36:21 +02:00
|
|
|
// loop the nodes and check their status as well
|
|
|
|
for _, n := range c.CacheNodes {
|
|
|
|
if n.CacheNodeStatus != nil && *n.CacheNodeStatus != "available" {
|
|
|
|
log.Printf("[DEBUG] Node (%s) is not yet available, status: %s", *n.CacheNodeID, *n.CacheNodeStatus)
|
|
|
|
return nil, "creating", nil
|
|
|
|
}
|
|
|
|
}
|
2015-04-26 03:53:21 +02:00
|
|
|
return c, givenState, nil
|
|
|
|
}
|
|
|
|
log.Printf("[DEBUG] current status: %v", *c.CacheClusterStatus)
|
|
|
|
return c, *c.CacheClusterStatus, nil
|
|
|
|
}
|
|
|
|
}
|
2015-05-14 18:44:24 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|