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

provider/aws: aws_elasticache_cluster data source #14895

Merged
merged 3 commits into from
May 31, 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
236 changes: 236 additions & 0 deletions builtin/providers/aws/data_source_aws_elasticache_cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
package aws

import (
"fmt"
"log"
"strings"

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

func dataSourceAwsElastiCacheCluster() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsElastiCacheClusterRead,

Schema: map[string]*schema.Schema{
"cluster_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
StateFunc: func(v interface{}) string {
value := v.(string)
return strings.ToLower(value)
},
},

"node_type": {
Type: schema.TypeString,
Computed: true,
},

"num_cache_nodes": {
Type: schema.TypeInt,
Computed: true,
},

"subnet_group_name": {
Type: schema.TypeString,
Computed: true,
},

"engine": {
Type: schema.TypeString,
Computed: true,
},

"engine_version": {
Type: schema.TypeString,
Computed: true,
},

"parameter_group_name": {
Type: schema.TypeString,
Computed: true,
},

"replication_group_id": {
Type: schema.TypeString,
Computed: true,
},

"security_group_names": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},

"security_group_ids": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},

"maintenance_window": {
Type: schema.TypeString,
Computed: true,
},

"snapshot_window": {
Type: schema.TypeString,
Computed: true,
},

"snapshot_retention_limit": {
Type: schema.TypeInt,
Computed: true,
},

"availability_zone": {
Type: schema.TypeString,
Computed: true,
},

"notification_topic_arn": {
Type: schema.TypeString,
Computed: true,
},

"port": {
Type: schema.TypeInt,
Computed: true,
},

"configuration_endpoint": {
Type: schema.TypeString,
Computed: true,
},

"cluster_address": {
Type: schema.TypeString,
Computed: true,
},

"arn": {
Type: schema.TypeString,
Computed: true,
},

"cache_nodes": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"address": {
Type: schema.TypeString,
Computed: true,
},
"port": {
Type: schema.TypeInt,
Computed: true,
},
"availability_zone": {
Type: schema.TypeString,
Computed: true,
},
},
},
},

"tags": tagsSchemaComputed(),
},
}
}

func dataSourceAwsElastiCacheClusterRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).elasticacheconn

req := &elasticache.DescribeCacheClustersInput{
CacheClusterId: aws.String(d.Get("cluster_id").(string)),
ShowCacheNodeInfo: aws.Bool(true),
}

resp, err := conn.DescribeCacheClusters(req)
if err != nil {
return err
}

if len(resp.CacheClusters) < 1 {
return fmt.Errorf("Your query returned no results. Please change your search criteria and try again.")
}
if len(resp.CacheClusters) > 1 {
return fmt.Errorf("Your query returned more than one result. Please try a more specific search criteria.")
}

cluster := resp.CacheClusters[0]

d.SetId(*cluster.CacheClusterId)

d.Set("cluster_id", cluster.CacheClusterId)
d.Set("node_type", cluster.CacheNodeType)
d.Set("num_cache_nodes", cluster.NumCacheNodes)
d.Set("subnet_group_name", cluster.CacheSubnetGroupName)
d.Set("engine", cluster.Engine)
d.Set("engine_version", cluster.EngineVersion)
d.Set("security_group_names", flattenElastiCacheSecurityGroupNames(cluster.CacheSecurityGroups))
d.Set("security_group_ids", flattenElastiCacheSecurityGroupIds(cluster.SecurityGroups))

if cluster.CacheParameterGroup != nil {
d.Set("parameter_group_name", cluster.CacheParameterGroup.CacheParameterGroupName)
}

if cluster.ReplicationGroupId != nil {
d.Set("replication_group_id", cluster.ReplicationGroupId)
}

d.Set("maintenance_window", cluster.PreferredMaintenanceWindow)
d.Set("snapshot_window", cluster.SnapshotWindow)
d.Set("snapshot_retention_limit", cluster.SnapshotRetentionLimit)
d.Set("availability_zone", cluster.PreferredAvailabilityZone)

if cluster.NotificationConfiguration != nil {
if *cluster.NotificationConfiguration.TopicStatus == "active" {
d.Set("notification_topic_arn", cluster.NotificationConfiguration.TopicArn)
}
}

if cluster.ConfigurationEndpoint != nil {
d.Set("port", cluster.ConfigurationEndpoint.Port)
d.Set("configuration_endpoint", aws.String(fmt.Sprintf("%s:%d", *cluster.ConfigurationEndpoint.Address, *cluster.ConfigurationEndpoint.Port)))
d.Set("cluster_address", aws.String(fmt.Sprintf("%s", *cluster.ConfigurationEndpoint.Address)))
}

if err := setCacheNodeData(d, cluster); err != nil {
return err
}

arn, err := buildECARN(d.Id(), meta.(*AWSClient).partition, meta.(*AWSClient).accountid, meta.(*AWSClient).region)
if err != nil {
log.Printf("[DEBUG] Error building ARN for ElastiCache Cluster %s", *cluster.CacheClusterId)
}
d.Set("arn", arn)

tagResp, err := conn.ListTagsForResource(&elasticache.ListTagsForResourceInput{
ResourceName: aws.String(arn),
})

if err != nil {
log.Printf("[DEBUG] Error retrieving tags for ARN: %s", arn)
}

var et []*elasticache.Tag
if len(tagResp.TagList) > 0 {
et = tagResp.TagList
}
d.Set("tags", tagsToMapEC(et))

return nil

}
72 changes: 72 additions & 0 deletions builtin/providers/aws/data_source_aws_elasticache_cluster_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package aws

import (
"fmt"
"testing"

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

func TestAccAWSDataElasticacheCluster_basic(t *testing.T) {
rInt := acctest.RandInt()
rString := acctest.RandString(10)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccAWSElastiCacheClusterConfigWithDataSource(rString, rInt),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.aws_elasticache_cluster.bar", "engine", "memcached"),
resource.TestCheckResourceAttr("data.aws_elasticache_cluster.bar", "node_type", "cache.m1.small"),
resource.TestCheckResourceAttr("data.aws_elasticache_cluster.bar", "port", "11211"),
resource.TestCheckResourceAttr("data.aws_elasticache_cluster.bar", "num_cache_nodes", "1"),
resource.TestCheckResourceAttrSet("data.aws_elasticache_cluster.bar", "configuration_endpoint"),
resource.TestCheckResourceAttrSet("data.aws_elasticache_cluster.bar", "cluster_address"),
resource.TestCheckResourceAttrSet("data.aws_elasticache_cluster.bar", "availability_zone"),
),
},
},
})
}

func testAccAWSElastiCacheClusterConfigWithDataSource(rString string, rInt int) string {
return fmt.Sprintf(`
provider "aws" {
region = "us-east-1"
}

resource "aws_security_group" "bar" {
name = "tf-test-security-group-%d"
description = "tf-test-security-group-descr"
ingress {
from_port = -1
to_port = -1
protocol = "icmp"
cidr_blocks = ["0.0.0.0/0"]
}
}

resource "aws_elasticache_security_group" "bar" {
name = "tf-test-security-group-%d"
description = "tf-test-security-group-descr"
security_group_names = ["${aws_security_group.bar.name}"]
}

resource "aws_elasticache_cluster" "bar" {
cluster_id = "tf-%s"
engine = "memcached"
node_type = "cache.m1.small"
num_cache_nodes = 1
port = 11211
parameter_group_name = "default.memcached1.4"
security_group_names = ["${aws_elasticache_security_group.bar.name}"]
}

data "aws_elasticache_cluster" "bar" {
cluster_id = "${aws_elasticache_cluster.bar.cluster_id}"
}

`, rInt, rInt, rString)
}
1 change: 1 addition & 0 deletions builtin/providers/aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ func Provider() terraform.ResourceProvider {
"aws_ecs_task_definition": dataSourceAwsEcsTaskDefinition(),
"aws_efs_file_system": dataSourceAwsEfsFileSystem(),
"aws_eip": dataSourceAwsEip(),
"aws_elasticache_cluster": dataSourceAwsElastiCacheCluster(),
"aws_elb_hosted_zone_id": dataSourceAwsElbHostedZoneId(),
"aws_elb_service_account": dataSourceAwsElbServiceAccount(),
"aws_iam_account_alias": dataSourceAwsIamAccountAlias(),
Expand Down
14 changes: 7 additions & 7 deletions builtin/providers/aws/resource_aws_elasticache_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestAccAWSElasticacheCluster_basic(t *testing.T) {
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSElasticacheClusterDestroy,
Steps: []resource.TestStep{
resource.TestStep{
{
Config: testAccAWSElasticacheClusterConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSElasticacheSecurityGroupExists("aws_elasticache_security_group.bar"),
Expand All @@ -47,7 +47,7 @@ func TestAccAWSElasticacheCluster_snapshotsWithUpdates(t *testing.T) {
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSElasticacheClusterDestroy,
Steps: []resource.TestStep{
resource.TestStep{
{
Config: preConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSElasticacheSecurityGroupExists("aws_elasticache_security_group.bar"),
Expand All @@ -59,7 +59,7 @@ func TestAccAWSElasticacheCluster_snapshotsWithUpdates(t *testing.T) {
),
},

resource.TestStep{
{
Config: postConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSElasticacheSecurityGroupExists("aws_elasticache_security_group.bar"),
Expand All @@ -86,7 +86,7 @@ func TestAccAWSElasticacheCluster_decreasingCacheNodes(t *testing.T) {
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSElasticacheClusterDestroy,
Steps: []resource.TestStep{
resource.TestStep{
{
Config: preConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSElasticacheSecurityGroupExists("aws_elasticache_security_group.bar"),
Expand All @@ -96,7 +96,7 @@ func TestAccAWSElasticacheCluster_decreasingCacheNodes(t *testing.T) {
),
},

resource.TestStep{
{
Config: postConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSElasticacheSecurityGroupExists("aws_elasticache_security_group.bar"),
Expand All @@ -117,7 +117,7 @@ func TestAccAWSElasticacheCluster_vpc(t *testing.T) {
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSElasticacheClusterDestroy,
Steps: []resource.TestStep{
resource.TestStep{
{
Config: testAccAWSElasticacheClusterInVPCConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSElasticacheSubnetGroupExists("aws_elasticache_subnet_group.bar", &csg),
Expand All @@ -139,7 +139,7 @@ func TestAccAWSElasticacheCluster_multiAZInVpc(t *testing.T) {
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSElasticacheClusterDestroy,
Steps: []resource.TestStep{
resource.TestStep{
{
Config: testAccAWSElasticacheClusterMultiAZInVPCConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSElasticacheSubnetGroupExists("aws_elasticache_subnet_group.bar", &csg),
Expand Down
Loading