Skip to content

Commit

Permalink
Add PrimaryClusterID to Elasticache Replication Group
Browse files Browse the repository at this point in the history
  • Loading branch information
saulshanabrook committed Aug 15, 2015
1 parent 0b4747e commit 9bef561
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 17 deletions.
7 changes: 7 additions & 0 deletions builtin/providers/aws/resource_aws_elasticache_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ func resourceAwsElasticacheCluster() *schema.Resource {
Computed: true,
ForceNew: true,
},
"replication_group_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"security_group_names": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Expand Down Expand Up @@ -151,6 +156,7 @@ 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)
replicationGroupID := d.Get("replication_group_id").(string)

securityNames := expandStringList(securityNameSet.List())
securityIds := expandStringList(securityIdSet.List())
Expand All @@ -167,6 +173,7 @@ func resourceAwsElasticacheClusterCreate(d *schema.ResourceData, meta interface{
CacheSecurityGroupNames: securityNames,
SecurityGroupIDs: securityIds,
Tags: tags,
ReplicationGroupID: aws.String(replicationGroupID),
}

// parameter groups are optional and can be defaulted by AWS
Expand Down
43 changes: 27 additions & 16 deletions builtin/providers/aws/resource_aws_elasticache_replication_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ func resourceAwsElasticacheReplicationGroup() *schema.Resource {
"num_cache_clusters": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Default: 1,
ForceNew: true,
},
"primary_cluster_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"parameter_group_name": &schema.Schema{
Expand Down Expand Up @@ -95,28 +99,30 @@ func resourceAwsElasticacheReplicationGroup() *schema.Resource {
func resourceAwsElasticacheReplicationGroupCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).elasticacheconn

replicationGroupId := d.Get("replication_group_id").(string)
replicationGroupID := d.Get("replication_group_id").(string)
description := d.Get("description").(string)
cacheNodeType := d.Get("cache_node_type").(string)
automaticFailover := d.Get("automatic_failover").(bool)
numCacheClusters := d.Get("num_cache_clusters").(int)
primaryClusterID := d.Get("primary_cluster_id").(string)
engine := d.Get("engine").(string)
engineVersion := d.Get("engine_version").(string)
securityNameSet := d.Get("security_group_names").(*schema.Set)
securityIdSet := d.Get("security_group_ids").(*schema.Set)
securityIDSet := d.Get("security_group_ids").(*schema.Set)
subnetGroupName := d.Get("subnet_group_name").(string)
prefferedCacheClusterAZs := d.Get("preferred_cache_cluster_azs").(*schema.Set)

securityNames := expandStringList(securityNameSet.List())
securityIds := expandStringList(securityIdSet.List())
securityIds := expandStringList(securityIDSet.List())
prefferedAZs := expandStringList(prefferedCacheClusterAZs.List())

req := &elasticache.CreateReplicationGroupInput{
ReplicationGroupID: aws.String(replicationGroupId),
ReplicationGroupID: aws.String(replicationGroupID),
ReplicationGroupDescription: aws.String(description),
CacheNodeType: aws.String(cacheNodeType),
AutomaticFailoverEnabled: aws.Bool(automaticFailover),
NumCacheClusters: aws.Int64(int64(numCacheClusters)),
PrimaryClusterID: aws.String(primaryClusterID),
Engine: aws.String(engine),
CacheSubnetGroupName: aws.String(subnetGroupName),
EngineVersion: aws.String(engineVersion),
Expand All @@ -125,20 +131,27 @@ func resourceAwsElasticacheReplicationGroupCreate(d *schema.ResourceData, meta i
PreferredCacheClusterAZs: prefferedAZs,
}

// 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))
}

if v, ok := d.GetOk("maintenance_window"); ok {
req.PreferredMaintenanceWindow = aws.String(v.(string))
}

_, err := conn.CreateReplicationGroup(req)
if err != nil {
return fmt.Errorf("Error creating Elasticache replication group: %s", err)
}

d.SetId(replicationGroupID)

pending := []string{"creating"}
stateConf := &resource.StateChangeConf{
Pending: pending,
Target: "available",
Refresh: ReplicationGroupStateRefreshFunc(conn, d.Id(), "available", pending),
Refresh: replicationGroupStateRefreshFunc(conn, d.Id(), "available", pending),
Timeout: 60 * time.Minute,
Delay: 20 * time.Second,
MinTimeout: 5 * time.Second,
Expand All @@ -150,8 +163,6 @@ func resourceAwsElasticacheReplicationGroupCreate(d *schema.ResourceData, meta i
return fmt.Errorf("Error waiting for elasticache (%s) to be created: %s", d.Id(), sterr)
}

d.SetId(replicationGroupId)

return nil
}

Expand Down Expand Up @@ -179,8 +190,8 @@ func resourceAwsElasticacheReplicationGroupRead(d *schema.ResourceData, meta int
d.Set("description", c.Description)
d.Set("automatic_failover", c.AutomaticFailover)
d.Set("num_cache_clusters", len(c.MemberClusters))
d.Set("primary_endpoint", res.ReplicationGroups[0].NodeGroups[0].PrimaryEndpoint.Address)
}
d.Set("primary_endpoint", res.ReplicationGroups[0].NodeGroups[0].PrimaryEndpoint.Address)

return nil
}
Expand All @@ -204,13 +215,13 @@ func resourceAwsElasticacheReplicationGroupUpdate(d *schema.ResourceData, meta i
}

if d.HasChange("engine_version") {
engine_version := d.Get("engine_version").(string)
req.EngineVersion = aws.String(engine_version)
engineVersion := d.Get("engine_version").(string)
req.EngineVersion = aws.String(engineVersion)
}

if d.HasChange("security_group_ids") {
securityIdSet := d.Get("security_group_ids").(*schema.Set)
securityIds := expandStringList(securityIdSet.List())
securityIDSet := d.Get("security_group_ids").(*schema.Set)
securityIds := expandStringList(securityIDSet.List())
req.SecurityGroupIDs = securityIds
}

Expand Down Expand Up @@ -250,7 +261,7 @@ func resourceAwsElasticacheReplicationGroupDelete(d *schema.ResourceData, meta i
stateConf := &resource.StateChangeConf{
Pending: []string{"creating", "available", "deleting"},
Target: "",
Refresh: ReplicationGroupStateRefreshFunc(conn, d.Id(), "", []string{}),
Refresh: replicationGroupStateRefreshFunc(conn, d.Id(), "", []string{}),
Timeout: 15 * time.Minute,
Delay: 20 * time.Second,
MinTimeout: 5 * time.Second,
Expand All @@ -264,7 +275,7 @@ func resourceAwsElasticacheReplicationGroupDelete(d *schema.ResourceData, meta i
return nil
}

func ReplicationGroupStateRefreshFunc(conn *elasticache.ElastiCache, replicationGroupID, givenState string, pending []string) resource.StateRefreshFunc {
func replicationGroupStateRefreshFunc(conn *elasticache.ElastiCache, replicationGroupID, givenState string, pending []string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
resp, err := conn.DescribeReplicationGroups(&elasticache.DescribeReplicationGroupsInput{
ReplicationGroupID: aws.String(replicationGroupID),
Expand All @@ -280,7 +291,7 @@ func ReplicationGroupStateRefreshFunc(conn *elasticache.ElastiCache, replication
}
}

log.Printf("[ERROR] ReplicationGroupStateRefreshFunc: %s", err)
log.Printf("[ERROR] replicationGroupStateRefreshFunc: %s", err)
return nil, "", err
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package aws

import (
"fmt"
"testing"
"log"
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/elasticache"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ supported node types
cache cluster will have. For Redis, this value must be 1. For Memcache, this
value must be between 1 and 20

* `replication_group_id` – (Optional, Redis only) The ID of the replication
group to which this cache cluster should belong. If specified, the cache cluster
will be added to the specified replication group as a read replica;
otherwise, the cache cluster will be a standalone primary that is not part of
any replication group.

* `parameter_group_name` – (Required) Name of the parameter group to associate
with this cache cluster

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
---
layout: "aws"
page_title: "AWS: aws_elasticache_replication_group"
sidebar_current: "docs-aws-resource-elasticache-replication-group"
description: |-
Provides an ElastiCache Replication Group resource.
---

# aws\_elasticache\_replication\_group

Provides an ElastiCache Replication Group resource.

## Example Usage

```
resource "aws_elasticache_replication_group" "redis" {
replication_group_id = "users-redis"
description = "users redis"
engine = "redis"
cache_node_type = "cache.m3.medium"
num_cache_clusters = 2
automatic_failover = true
subnet_group_name = "${aws_elasticache_subnet_group.redis.name}"
security_group_ids = ["${aws_security_group.redis.id}"]
}
```

## Argument Reference

The following arguments are supported:

* `replication_group_id` – (Required) Replication group identifier. This
parameter is stored as a lowercase string

* `description` – (Required) The description of the replication group.

* `engine` – (Optional) The name of the cache engine to be used for the cache clusters in this replication group.
The only current valid value is `redis`

* `engine_version` – (Optional) Version number of the cache engine to be used.
See [Selecting a Cache Engine and Version](http://docs.aws.amazon.com/AmazonElastiCache/latest/UserGuide/SelectEngine.html)
in the AWS Documentation center for supported versions

* `cache_node_type` – (Required) The compute and memory capacity of the nodes. See
[Available Cache Node Types](http://aws.amazon.com/elasticache/details#Available_Cache_Node_Types) for
supported node types

* `automatic_failover` - (Optional) Specifies whether a read-only replica will be automatically promoted to read/write primary if the existing primary fails.
If true, Multi-AZ is enabled for this replication group. If false, Multi-AZ is disabled for this replication group.

* `num_cache_clusters` – (Optional) The number of cache clusters this replication group will initially have. If `automatic_failover` is enabled, the value of this parameter must be at least 2.
Either this or `primary_cluster_id` is required.

* `primary_cluster_id` - (Optional) The identifier of the cache cluster that
will serve as the primary for this replication group. This cache cluster must already exist and have a status of available.
Either this or `num_cache_clusters` is required.

* `parameter_group_name` – (Required) Name of the parameter group to associate
with this cache cluster

* `preferred_cache_cluster_azs` - (Optional) A list of EC2 availability zones in which the replication group's cache clusters will be created. The order of the availability zones in the list is not important. If not provided, AWS will chose them for you.

* `subnet_group_name` – (Optional, VPC only) Name of the subnet group to be used
for the cache cluster.

* `security_group_names` – (Optional, EC2 Classic only) List of security group
names to associate with this cache cluster

* `security_group_ids` – (Optional, VPC only) One or more VPC security groups associated
with the cache cluster


## Attributes Reference

The following attributes are exported:

* `primary_endpoint` - The address of the primary node.

0 comments on commit 9bef561

Please sign in to comment.