github.com/jsoriano/terraform@v0.6.7-0.20151026070445-8b70867fdd95/builtin/providers/aws/resource_aws_elasticache_cluster.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "log" 6 "sort" 7 "strings" 8 "time" 9 10 "github.com/aws/aws-sdk-go/aws" 11 "github.com/aws/aws-sdk-go/aws/awserr" 12 "github.com/aws/aws-sdk-go/service/elasticache" 13 "github.com/aws/aws-sdk-go/service/iam" 14 "github.com/hashicorp/terraform/helper/hashcode" 15 "github.com/hashicorp/terraform/helper/resource" 16 "github.com/hashicorp/terraform/helper/schema" 17 ) 18 19 func resourceAwsElasticacheCluster() *schema.Resource { 20 return &schema.Resource{ 21 Create: resourceAwsElasticacheClusterCreate, 22 Read: resourceAwsElasticacheClusterRead, 23 Update: resourceAwsElasticacheClusterUpdate, 24 Delete: resourceAwsElasticacheClusterDelete, 25 26 Schema: map[string]*schema.Schema{ 27 "cluster_id": &schema.Schema{ 28 Type: schema.TypeString, 29 Required: true, 30 ForceNew: true, 31 StateFunc: func(val interface{}) string { 32 // Elasticache normalizes cluster ids to lowercase, 33 // so we have to do this too or else we can end up 34 // with non-converging diffs. 35 return strings.ToLower(val.(string)) 36 }, 37 }, 38 "configuration_endpoint": &schema.Schema{ 39 Type: schema.TypeString, 40 Computed: true, 41 }, 42 "engine": &schema.Schema{ 43 Type: schema.TypeString, 44 Required: true, 45 }, 46 "node_type": &schema.Schema{ 47 Type: schema.TypeString, 48 Required: true, 49 ForceNew: true, 50 }, 51 "num_cache_nodes": &schema.Schema{ 52 Type: schema.TypeInt, 53 Required: true, 54 }, 55 "parameter_group_name": &schema.Schema{ 56 Type: schema.TypeString, 57 Optional: true, 58 Computed: true, 59 }, 60 "port": &schema.Schema{ 61 Type: schema.TypeInt, 62 Required: true, 63 ForceNew: true, 64 }, 65 "engine_version": &schema.Schema{ 66 Type: schema.TypeString, 67 Optional: true, 68 Computed: true, 69 }, 70 "maintenance_window": &schema.Schema{ 71 Type: schema.TypeString, 72 Optional: true, 73 Computed: true, 74 }, 75 "subnet_group_name": &schema.Schema{ 76 Type: schema.TypeString, 77 Optional: true, 78 Computed: true, 79 ForceNew: true, 80 }, 81 "security_group_names": &schema.Schema{ 82 Type: schema.TypeSet, 83 Optional: true, 84 Computed: true, 85 ForceNew: true, 86 Elem: &schema.Schema{Type: schema.TypeString}, 87 Set: func(v interface{}) int { 88 return hashcode.String(v.(string)) 89 }, 90 }, 91 "security_group_ids": &schema.Schema{ 92 Type: schema.TypeSet, 93 Optional: true, 94 Computed: true, 95 Elem: &schema.Schema{Type: schema.TypeString}, 96 Set: func(v interface{}) int { 97 return hashcode.String(v.(string)) 98 }, 99 }, 100 // Exported Attributes 101 "cache_nodes": &schema.Schema{ 102 Type: schema.TypeList, 103 Computed: true, 104 Elem: &schema.Resource{ 105 Schema: map[string]*schema.Schema{ 106 "id": &schema.Schema{ 107 Type: schema.TypeString, 108 Computed: true, 109 }, 110 "address": &schema.Schema{ 111 Type: schema.TypeString, 112 Computed: true, 113 }, 114 "port": &schema.Schema{ 115 Type: schema.TypeInt, 116 Computed: true, 117 }, 118 }, 119 }, 120 }, 121 122 // A single-element string list containing an Amazon Resource Name (ARN) that 123 // uniquely identifies a Redis RDB snapshot file stored in Amazon S3. The snapshot 124 // file will be used to populate the node group. 125 // 126 // See also: 127 // https://github.com/aws/aws-sdk-go/blob/4862a174f7fc92fb523fc39e68f00b87d91d2c3d/service/elasticache/api.go#L2079 128 "snapshot_arns": &schema.Schema{ 129 Type: schema.TypeSet, 130 Optional: true, 131 ForceNew: true, 132 Elem: &schema.Schema{Type: schema.TypeString}, 133 Set: func(v interface{}) int { 134 return hashcode.String(v.(string)) 135 }, 136 }, 137 138 "tags": tagsSchema(), 139 140 // apply_immediately is used to determine when the update modifications 141 // take place. 142 // See http://docs.aws.amazon.com/AmazonElastiCache/latest/APIReference/API_ModifyCacheCluster.html 143 "apply_immediately": &schema.Schema{ 144 Type: schema.TypeBool, 145 Optional: true, 146 Computed: true, 147 }, 148 }, 149 } 150 } 151 152 func resourceAwsElasticacheClusterCreate(d *schema.ResourceData, meta interface{}) error { 153 conn := meta.(*AWSClient).elasticacheconn 154 155 clusterId := d.Get("cluster_id").(string) 156 nodeType := d.Get("node_type").(string) // e.g) cache.m1.small 157 numNodes := int64(d.Get("num_cache_nodes").(int)) // 2 158 engine := d.Get("engine").(string) // memcached 159 engineVersion := d.Get("engine_version").(string) // 1.4.14 160 port := int64(d.Get("port").(int)) // e.g) 11211 161 subnetGroupName := d.Get("subnet_group_name").(string) 162 securityNameSet := d.Get("security_group_names").(*schema.Set) 163 securityIdSet := d.Get("security_group_ids").(*schema.Set) 164 165 securityNames := expandStringList(securityNameSet.List()) 166 securityIds := expandStringList(securityIdSet.List()) 167 168 tags := tagsFromMapEC(d.Get("tags").(map[string]interface{})) 169 req := &elasticache.CreateCacheClusterInput{ 170 CacheClusterId: aws.String(clusterId), 171 CacheNodeType: aws.String(nodeType), 172 NumCacheNodes: aws.Int64(numNodes), 173 Engine: aws.String(engine), 174 EngineVersion: aws.String(engineVersion), 175 Port: aws.Int64(port), 176 CacheSubnetGroupName: aws.String(subnetGroupName), 177 CacheSecurityGroupNames: securityNames, 178 SecurityGroupIds: securityIds, 179 Tags: tags, 180 } 181 182 // parameter groups are optional and can be defaulted by AWS 183 if v, ok := d.GetOk("parameter_group_name"); ok { 184 req.CacheParameterGroupName = aws.String(v.(string)) 185 } 186 187 if v, ok := d.GetOk("maintenance_window"); ok { 188 req.PreferredMaintenanceWindow = aws.String(v.(string)) 189 } 190 191 snaps := d.Get("snapshot_arns").(*schema.Set).List() 192 if len(snaps) > 0 { 193 s := expandStringList(snaps) 194 req.SnapshotArns = s 195 log.Printf("[DEBUG] Restoring Redis cluster from S3 snapshot: %#v", s) 196 } 197 198 resp, err := conn.CreateCacheCluster(req) 199 if err != nil { 200 return fmt.Errorf("Error creating Elasticache: %s", err) 201 } 202 203 // Assign the cluster id as the resource ID 204 // Elasticache always retains the id in lower case, so we have to 205 // mimic that or else we won't be able to refresh a resource whose 206 // name contained uppercase characters. 207 d.SetId(strings.ToLower(*resp.CacheCluster.CacheClusterId)) 208 209 pending := []string{"creating"} 210 stateConf := &resource.StateChangeConf{ 211 Pending: pending, 212 Target: "available", 213 Refresh: cacheClusterStateRefreshFunc(conn, d.Id(), "available", pending), 214 Timeout: 10 * time.Minute, 215 Delay: 10 * time.Second, 216 MinTimeout: 3 * time.Second, 217 } 218 219 log.Printf("[DEBUG] Waiting for state to become available: %v", d.Id()) 220 _, sterr := stateConf.WaitForState() 221 if sterr != nil { 222 return fmt.Errorf("Error waiting for elasticache (%s) to be created: %s", d.Id(), sterr) 223 } 224 225 return resourceAwsElasticacheClusterRead(d, meta) 226 } 227 228 func resourceAwsElasticacheClusterRead(d *schema.ResourceData, meta interface{}) error { 229 conn := meta.(*AWSClient).elasticacheconn 230 req := &elasticache.DescribeCacheClustersInput{ 231 CacheClusterId: aws.String(d.Id()), 232 ShowCacheNodeInfo: aws.Bool(true), 233 } 234 235 res, err := conn.DescribeCacheClusters(req) 236 if err != nil { 237 return err 238 } 239 240 if len(res.CacheClusters) == 1 { 241 c := res.CacheClusters[0] 242 d.Set("cluster_id", c.CacheClusterId) 243 d.Set("node_type", c.CacheNodeType) 244 d.Set("num_cache_nodes", c.NumCacheNodes) 245 d.Set("engine", c.Engine) 246 d.Set("engine_version", c.EngineVersion) 247 if c.ConfigurationEndpoint != nil { 248 d.Set("port", c.ConfigurationEndpoint.Port) 249 d.Set("configuration_endpoint", aws.String(fmt.Sprintf("%s:%d", *c.ConfigurationEndpoint.Address, *c.ConfigurationEndpoint.Port))) 250 } 251 252 d.Set("subnet_group_name", c.CacheSubnetGroupName) 253 d.Set("security_group_names", c.CacheSecurityGroups) 254 d.Set("security_group_ids", c.SecurityGroups) 255 d.Set("parameter_group_name", c.CacheParameterGroup) 256 d.Set("maintenance_window", c.PreferredMaintenanceWindow) 257 258 if err := setCacheNodeData(d, c); err != nil { 259 return err 260 } 261 // list tags for resource 262 // set tags 263 arn, err := buildECARN(d, meta) 264 if err != nil { 265 log.Printf("[DEBUG] Error building ARN for ElastiCache Cluster, not setting Tags for cluster %s", *c.CacheClusterId) 266 } else { 267 resp, err := conn.ListTagsForResource(&elasticache.ListTagsForResourceInput{ 268 ResourceName: aws.String(arn), 269 }) 270 271 if err != nil { 272 log.Printf("[DEBUG] Error retrieving tags for ARN: %s", arn) 273 } 274 275 var et []*elasticache.Tag 276 if len(resp.TagList) > 0 { 277 et = resp.TagList 278 } 279 d.Set("tags", tagsToMapEC(et)) 280 } 281 } 282 283 return nil 284 } 285 286 func resourceAwsElasticacheClusterUpdate(d *schema.ResourceData, meta interface{}) error { 287 conn := meta.(*AWSClient).elasticacheconn 288 arn, err := buildECARN(d, meta) 289 if err != nil { 290 log.Printf("[DEBUG] Error building ARN for ElastiCache Cluster, not updating Tags for cluster %s", d.Id()) 291 } else { 292 if err := setTagsEC(conn, d, arn); err != nil { 293 return err 294 } 295 } 296 297 req := &elasticache.ModifyCacheClusterInput{ 298 CacheClusterId: aws.String(d.Id()), 299 ApplyImmediately: aws.Bool(d.Get("apply_immediately").(bool)), 300 } 301 302 requestUpdate := false 303 if d.HasChange("security_group_ids") { 304 if attr := d.Get("security_group_ids").(*schema.Set); attr.Len() > 0 { 305 req.SecurityGroupIds = expandStringList(attr.List()) 306 requestUpdate = true 307 } 308 } 309 310 if d.HasChange("parameter_group_name") { 311 req.CacheParameterGroupName = aws.String(d.Get("parameter_group_name").(string)) 312 requestUpdate = true 313 } 314 315 if d.HasChange("maintenance_window") { 316 req.PreferredMaintenanceWindow = aws.String(d.Get("maintenance_window").(string)) 317 requestUpdate = true 318 } 319 320 if d.HasChange("engine_version") { 321 req.EngineVersion = aws.String(d.Get("engine_version").(string)) 322 requestUpdate = true 323 } 324 325 if d.HasChange("num_cache_nodes") { 326 req.NumCacheNodes = aws.Int64(int64(d.Get("num_cache_nodes").(int))) 327 requestUpdate = true 328 } 329 330 if requestUpdate { 331 log.Printf("[DEBUG] Modifying ElastiCache Cluster (%s), opts:\n%s", d.Id(), req) 332 _, err := conn.ModifyCacheCluster(req) 333 if err != nil { 334 return fmt.Errorf("[WARN] Error updating ElastiCache cluster (%s), error: %s", d.Id(), err) 335 } 336 337 log.Printf("[DEBUG] Waiting for update: %s", d.Id()) 338 pending := []string{"modifying", "rebooting cache cluster nodes", "snapshotting"} 339 stateConf := &resource.StateChangeConf{ 340 Pending: pending, 341 Target: "available", 342 Refresh: cacheClusterStateRefreshFunc(conn, d.Id(), "available", pending), 343 Timeout: 5 * time.Minute, 344 Delay: 5 * time.Second, 345 MinTimeout: 3 * time.Second, 346 } 347 348 _, sterr := stateConf.WaitForState() 349 if sterr != nil { 350 return fmt.Errorf("Error waiting for elasticache (%s) to update: %s", d.Id(), sterr) 351 } 352 } 353 354 return resourceAwsElasticacheClusterRead(d, meta) 355 } 356 357 func setCacheNodeData(d *schema.ResourceData, c *elasticache.CacheCluster) error { 358 sortedCacheNodes := make([]*elasticache.CacheNode, len(c.CacheNodes)) 359 copy(sortedCacheNodes, c.CacheNodes) 360 sort.Sort(byCacheNodeId(sortedCacheNodes)) 361 362 cacheNodeData := make([]map[string]interface{}, 0, len(sortedCacheNodes)) 363 364 for _, node := range sortedCacheNodes { 365 if node.CacheNodeId == nil || node.Endpoint == nil || node.Endpoint.Address == nil || node.Endpoint.Port == nil { 366 return fmt.Errorf("Unexpected nil pointer in: %s", node) 367 } 368 cacheNodeData = append(cacheNodeData, map[string]interface{}{ 369 "id": *node.CacheNodeId, 370 "address": *node.Endpoint.Address, 371 "port": int(*node.Endpoint.Port), 372 }) 373 } 374 375 return d.Set("cache_nodes", cacheNodeData) 376 } 377 378 type byCacheNodeId []*elasticache.CacheNode 379 380 func (b byCacheNodeId) Len() int { return len(b) } 381 func (b byCacheNodeId) Swap(i, j int) { b[i], b[j] = b[j], b[i] } 382 func (b byCacheNodeId) Less(i, j int) bool { 383 return b[i].CacheNodeId != nil && b[j].CacheNodeId != nil && 384 *b[i].CacheNodeId < *b[j].CacheNodeId 385 } 386 387 func resourceAwsElasticacheClusterDelete(d *schema.ResourceData, meta interface{}) error { 388 conn := meta.(*AWSClient).elasticacheconn 389 390 req := &elasticache.DeleteCacheClusterInput{ 391 CacheClusterId: aws.String(d.Id()), 392 } 393 _, err := conn.DeleteCacheCluster(req) 394 if err != nil { 395 return err 396 } 397 398 log.Printf("[DEBUG] Waiting for deletion: %v", d.Id()) 399 stateConf := &resource.StateChangeConf{ 400 Pending: []string{"creating", "available", "deleting", "incompatible-parameters", "incompatible-network", "restore-failed"}, 401 Target: "", 402 Refresh: cacheClusterStateRefreshFunc(conn, d.Id(), "", []string{}), 403 Timeout: 10 * time.Minute, 404 Delay: 10 * time.Second, 405 MinTimeout: 3 * time.Second, 406 } 407 408 _, sterr := stateConf.WaitForState() 409 if sterr != nil { 410 return fmt.Errorf("Error waiting for elasticache (%s) to delete: %s", d.Id(), sterr) 411 } 412 413 d.SetId("") 414 415 return nil 416 } 417 418 func cacheClusterStateRefreshFunc(conn *elasticache.ElastiCache, clusterID, givenState string, pending []string) resource.StateRefreshFunc { 419 return func() (interface{}, string, error) { 420 resp, err := conn.DescribeCacheClusters(&elasticache.DescribeCacheClustersInput{ 421 CacheClusterId: aws.String(clusterID), 422 ShowCacheNodeInfo: aws.Bool(true), 423 }) 424 if err != nil { 425 apierr := err.(awserr.Error) 426 log.Printf("[DEBUG] message: %v, code: %v", apierr.Message(), apierr.Code()) 427 if apierr.Message() == fmt.Sprintf("CacheCluster not found: %v", clusterID) { 428 log.Printf("[DEBUG] Detect deletion") 429 return nil, "", nil 430 } 431 432 log.Printf("[ERROR] CacheClusterStateRefreshFunc: %s", err) 433 return nil, "", err 434 } 435 436 if len(resp.CacheClusters) == 0 { 437 return nil, "", fmt.Errorf("[WARN] Error: no Cache Clusters found for id (%s)", clusterID) 438 } 439 440 var c *elasticache.CacheCluster 441 for _, cluster := range resp.CacheClusters { 442 if *cluster.CacheClusterId == clusterID { 443 log.Printf("[DEBUG] Found matching ElastiCache cluster: %s", *cluster.CacheClusterId) 444 c = cluster 445 } 446 } 447 448 if c == nil { 449 return nil, "", fmt.Errorf("[WARN] Error: no matching Elastic Cache cluster for id (%s)", clusterID) 450 } 451 452 log.Printf("[DEBUG] ElastiCache Cluster (%s) status: %v", clusterID, *c.CacheClusterStatus) 453 454 // return the current state if it's in the pending array 455 for _, p := range pending { 456 log.Printf("[DEBUG] ElastiCache: checking pending state (%s) for cluster (%s), cluster status: %s", pending, clusterID, *c.CacheClusterStatus) 457 s := *c.CacheClusterStatus 458 if p == s { 459 log.Printf("[DEBUG] Return with status: %v", *c.CacheClusterStatus) 460 return c, p, nil 461 } 462 } 463 464 // return given state if it's not in pending 465 if givenState != "" { 466 log.Printf("[DEBUG] ElastiCache: checking given state (%s) of cluster (%s) against cluster status (%s)", givenState, clusterID, *c.CacheClusterStatus) 467 // check to make sure we have the node count we're expecting 468 if int64(len(c.CacheNodes)) != *c.NumCacheNodes { 469 log.Printf("[DEBUG] Node count is not what is expected: %d found, %d expected", len(c.CacheNodes), *c.NumCacheNodes) 470 return nil, "creating", nil 471 } 472 473 log.Printf("[DEBUG] Node count matched (%d)", len(c.CacheNodes)) 474 // loop the nodes and check their status as well 475 for _, n := range c.CacheNodes { 476 log.Printf("[DEBUG] Checking cache node for status: %s", n) 477 if n.CacheNodeStatus != nil && *n.CacheNodeStatus != "available" { 478 log.Printf("[DEBUG] Node (%s) is not yet available, status: %s", *n.CacheNodeId, *n.CacheNodeStatus) 479 return nil, "creating", nil 480 } 481 log.Printf("[DEBUG] Cache node not in expected state") 482 } 483 log.Printf("[DEBUG] ElastiCache returning given state (%s), cluster: %s", givenState, c) 484 return c, givenState, nil 485 } 486 log.Printf("[DEBUG] current status: %v", *c.CacheClusterStatus) 487 return c, *c.CacheClusterStatus, nil 488 } 489 } 490 491 func buildECARN(d *schema.ResourceData, meta interface{}) (string, error) { 492 iamconn := meta.(*AWSClient).iamconn 493 region := meta.(*AWSClient).region 494 // An zero value GetUserInput{} defers to the currently logged in user 495 resp, err := iamconn.GetUser(&iam.GetUserInput{}) 496 if err != nil { 497 return "", err 498 } 499 userARN := *resp.User.Arn 500 accountID := strings.Split(userARN, ":")[4] 501 arn := fmt.Sprintf("arn:aws:elasticache:%s:%s:cluster:%s", region, accountID, d.Id()) 502 return arn, nil 503 }