Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Data Source: aws_elasticache_replication_group #2124

Merged
merged 5 commits into from
Nov 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions aws/data_source_aws_elasticache_replication_group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package aws

import (
"fmt"

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

func dataSourceAwsElasticacheReplicationGroup() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsElasticacheReplicationGroupRead,
Schema: map[string]*schema.Schema{
"replication_group_id": {
Type: schema.TypeString,
Required: true,
},
"replication_group_description": {
Type: schema.TypeString,
Computed: true,
},
"auth_token_enabled": {
Type: schema.TypeBool,
Computed: true,
},
"automatic_failover_enabled": {
Type: schema.TypeBool,
Computed: true,
},
"port": {
Type: schema.TypeInt,
Computed: true,
},
"configuration_endpoint_address": {
Type: schema.TypeString,
Computed: true,
},
"primary_endpoint_address": {
Type: schema.TypeString,
Computed: true,
},
"number_cache_clusters": {
Type: schema.TypeInt,
Computed: true,
},
"node_type": {
Type: schema.TypeString,
Computed: true,
},
"snapshot_window": {
Type: schema.TypeString,
Computed: true,
},
"snapshot_retention_limit": {
Type: schema.TypeInt,
Computed: true,
},
},
}
}

func dataSourceAwsElasticacheReplicationGroupRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).elasticacheconn
input := &elasticache.DescribeReplicationGroupsInput{
ReplicationGroupId: aws.String(d.Get("replication_group_id").(string)),
}

resp, err := conn.DescribeReplicationGroups(input)
if err != nil {
return err
}

var rg *elasticache.ReplicationGroup
for _, r := range resp.ReplicationGroups {
if *r.ReplicationGroupId == d.Get("replication_group_id").(string) {
rg = r
}
}
if rg == nil {
return fmt.Errorf("Elasticache Replication Group (%s) not found", d.Get("replication_group_id").(string))
}

d.SetId(*rg.ReplicationGroupId)
d.Set("replication_group_description", rg.Description)
d.Set("auth_token_enabled", rg.AuthTokenEnabled)
if rg.AutomaticFailover != nil {
switch *rg.AutomaticFailover {
case elasticache.AutomaticFailoverStatusDisabled, elasticache.AutomaticFailoverStatusDisabling:
d.Set("automatic_failover_enabled", false)
case elasticache.AutomaticFailoverStatusEnabled, elasticache.AutomaticFailoverStatusEnabling:
d.Set("automatic_failover_enabled", true)
}
}
if rg.ConfigurationEndpoint != nil {
d.Set("port", rg.ConfigurationEndpoint.Port)
d.Set("configuration_endpoint_address", rg.ConfigurationEndpoint.Address)
} else {
if rg.NodeGroups == nil {
d.SetId("")
return fmt.Errorf("Elasticache Replication Group (%s) doesn't have node groups.", d.Get("replication_group_id").(string))
}
d.Set("port", rg.NodeGroups[0].PrimaryEndpoint.Port)
d.Set("primary_endpoint_address", rg.NodeGroups[0].PrimaryEndpoint.Address)
}
d.Set("number_cache_clusters", len(rg.MemberClusters))
d.Set("node_type", rg.CacheNodeType)
d.Set("snapshot_window", rg.SnapshotWindow)
d.Set("snapshot_retention_limit", rg.SnapshotRetentionLimit)
return nil
}
96 changes: 96 additions & 0 deletions aws/data_source_aws_elasticache_replication_group_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package aws

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccDataSourceAwsElasticacheReplicationGroup_basic(t *testing.T) {
rName := acctest.RandString(10)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsElasticacheReplicationGroupConfig_basic(rName),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.aws_elasticache_replication_group.bar", "replication_group_id", fmt.Sprintf("tf-%s", rName)),
resource.TestCheckResourceAttr("data.aws_elasticache_replication_group.bar", "replication_group_description", "test description"),
resource.TestCheckResourceAttr("data.aws_elasticache_replication_group.bar", "auth_token_enabled", "false"),
resource.TestCheckResourceAttr("data.aws_elasticache_replication_group.bar", "automatic_failover_enabled", "true"),
resource.TestCheckResourceAttr("data.aws_elasticache_replication_group.bar", "port", "6379"),
resource.TestCheckResourceAttrSet("data.aws_elasticache_replication_group.bar", "primary_endpoint_address"),
resource.TestCheckResourceAttr("data.aws_elasticache_replication_group.bar", "number_cache_clusters", "2"),
resource.TestCheckResourceAttr("data.aws_elasticache_replication_group.bar", "node_type", "cache.m1.small"),
resource.TestCheckResourceAttr("data.aws_elasticache_replication_group.bar", "snapshot_window", "01:00-02:00"),
),
},
},
})
}

func TestAccDataSourceAwsElasticacheReplicationGroup_ClusterMode(t *testing.T) {
rName := acctest.RandString(10)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsElasticacheReplicationGroupConfig_ClusterMode(rName),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.aws_elasticache_replication_group.cluster", "replication_group_id", fmt.Sprintf("tf-%s", rName)),
resource.TestCheckResourceAttr("data.aws_elasticache_replication_group.cluster", "replication_group_description", "test description"),
resource.TestCheckResourceAttr("data.aws_elasticache_replication_group.cluster", "auth_token_enabled", "false"),
resource.TestCheckResourceAttr("data.aws_elasticache_replication_group.cluster", "automatic_failover_enabled", "true"),
resource.TestCheckResourceAttr("data.aws_elasticache_replication_group.cluster", "port", "6379"),
resource.TestCheckResourceAttrSet("data.aws_elasticache_replication_group.cluster", "configuration_endpoint_address"),
resource.TestCheckResourceAttr("data.aws_elasticache_replication_group.cluster", "node_type", "cache.m1.small"),
),
},
},
})
}

func testAccDataSourceAwsElasticacheReplicationGroupConfig_basic(rName string) string {
return fmt.Sprintf(`
data "aws_availability_zones" "available" {}

resource "aws_elasticache_replication_group" "bar" {
replication_group_id = "tf-%s"
replication_group_description = "test description"
node_type = "cache.m1.small"
number_cache_clusters = 2
port = 6379
parameter_group_name = "default.redis3.2"
availability_zones = ["${data.aws_availability_zones.available.names[0]}", "${data.aws_availability_zones.available.names[1]}"]
automatic_failover_enabled = true
snapshot_window = "01:00-02:00"
}

data "aws_elasticache_replication_group" "bar" {
replication_group_id = "${aws_elasticache_replication_group.bar.replication_group_id}"
}`, rName)
}

func testAccDataSourceAwsElasticacheReplicationGroupConfig_ClusterMode(rName string) string {
return fmt.Sprintf(`
resource "aws_elasticache_replication_group" "cluster" {
replication_group_id = "tf-%s"
replication_group_description = "test description"
node_type = "cache.m1.small"
port = 6379
parameter_group_name = "default.redis3.2.cluster.on"
automatic_failover_enabled = true
cluster_mode {
replicas_per_node_group = 1
num_node_groups = 2
}
}

data "aws_elasticache_replication_group" "cluster" {
replication_group_id = "${aws_elasticache_replication_group.cluster.replication_group_id}"
}`, rName)
}
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ func Provider() terraform.ResourceProvider {
"aws_eip": dataSourceAwsEip(),
"aws_elastic_beanstalk_solution_stack": dataSourceAwsElasticBeanstalkSolutionStack(),
"aws_elasticache_cluster": dataSourceAwsElastiCacheCluster(),
"aws_elasticache_replication_group": dataSourceAwsElasticacheReplicationGroup(),
"aws_elb_hosted_zone_id": dataSourceAwsElbHostedZoneId(),
"aws_elb_service_account": dataSourceAwsElbServiceAccount(),
"aws_iam_account_alias": dataSourceAwsIamAccountAlias(),
Expand Down
3 changes: 3 additions & 0 deletions website/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@
<li<%= sidebar_current("docs-aws-datasource-elasticache-cluster") %>>
<a href="/docs/providers/aws/d/elasticache_cluster.html">aws_elasticache_cluster</a>
</li>
<li<%= sidebar_current("docs-aws-datasource-elasticache-replication-group") %>>
<a href="/docs/providers/aws/d/elasticache_replication_group.html">aws_elasticache_replication_group</a>
</li>
<li<%= sidebar_current("docs-aws-datasource-elb-hosted-zone-id") %>>
<a href="/docs/providers/aws/d/elb_hosted_zone_id.html">aws_elb_hosted_zone_id</a>
</li>
Expand Down
41 changes: 41 additions & 0 deletions website/docs/d/elasticache_replication_group.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
layout: "aws"
page_title: "AWS: aws_elasticache_replication_group"
sidebar_current: "docs-aws-datasource-elasticache-replication-group"
description: |-
Get information on an ElastiCache Replication Group resource.
---

# aws_elasticache_replication_group

Use this data source to get information about an Elasticache Replication Group.

## Example Usage

```hcl
data "aws_elasticache_replication_group" "bar" {
replication_group_id = "example"
}
```

## Argument Reference

The following arguments are supported:

* `replication_group_id` – (Required) The identifier for the replication group.

## Attributes Reference

The following attributes are exported:

* `replication_group_id` - The identifier for the replication group.
* `replication_group_description` - The description of the replication group.
* `auth_token_enabled` - A flag that enables using an AuthToken (password) when issuing Redis commands.
* `automatic_failover_enabled` - A flag whether a read-only replica will be automatically promoted to read/write primary if the existing primary fails.
* `node_type` – The cluster node type.
* `number_cache_clusters` – The number of cache clusters that the replication group has.
* `snapshot_window` - The daily time range (in UTC) during which ElastiCache begins taking a daily snapshot of your node group (shard).
* `snapshot_retention_limit` - The number of days for which ElastiCache retains automatic cache cluster snapshots before deleting them.
* `port` – The port number on which the configuration endpoint will accept connections.
* `configuration_endpoint_address` - The configuration endpoint address to allow host discovery.
* `primary_endpoint_address` - The endpoint of the primary node in this node group (shard).