diff --git a/aws/data_source_aws_elasticache_replication_group.go b/aws/data_source_aws_elasticache_replication_group.go new file mode 100644 index 00000000000..02aa41cfe7b --- /dev/null +++ b/aws/data_source_aws_elasticache_replication_group.go @@ -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 +} diff --git a/aws/data_source_aws_elasticache_replication_group_test.go b/aws/data_source_aws_elasticache_replication_group_test.go new file mode 100644 index 00000000000..0a13345b2ee --- /dev/null +++ b/aws/data_source_aws_elasticache_replication_group_test.go @@ -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) +} diff --git a/aws/provider.go b/aws/provider.go index c0ad59f1747..51e0ead1f30 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -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(), diff --git a/website/aws.erb b/website/aws.erb index 09f08082a86..12962e114ef 100644 --- a/website/aws.erb +++ b/website/aws.erb @@ -101,6 +101,9 @@ > aws_elasticache_cluster + > + aws_elasticache_replication_group + > aws_elb_hosted_zone_id diff --git a/website/docs/d/elasticache_replication_group.html.markdown b/website/docs/d/elasticache_replication_group.html.markdown new file mode 100644 index 00000000000..53b41abfbd4 --- /dev/null +++ b/website/docs/d/elasticache_replication_group.html.markdown @@ -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).