Skip to content

Commit

Permalink
service/elasticache: Support longer Cluster and Replication Group ide…
Browse files Browse the repository at this point in the history
…ntifiers

This also switches some of the hardcoded us-west-2 testing to use the aws_availability_zones data source so the testing is more region agnostic. Now its at least possible to run parallel testing locally in us-east-1 of an EC2-Classic enabled AWS account.

References:

- #9906
- #9131
- #7409
- #5381

Output from acceptance testing:

```
--- PASS: TestAccAWSElasticacheCluster_AZMode_Memcached_Ec2Classic (515.21s)
--- PASS: TestAccAWSElasticacheCluster_AZMode_Redis_Ec2Classic (507.64s)
--- PASS: TestAccAWSElasticacheCluster_Engine_Memcached_Ec2Classic (663.59s)
--- PASS: TestAccAWSElasticacheCluster_Engine_Redis_Ec2Classic (595.65s)
--- PASS: TestAccAWSElasticacheCluster_EngineVersion_Memcached_Ec2Classic (1260.72s)
--- PASS: TestAccAWSElasticacheCluster_EngineVersion_Redis_Ec2Classic (1303.63s)
--- PASS: TestAccAWSElasticacheCluster_multiAZInVpc (737.16s)
--- PASS: TestAccAWSElasticacheCluster_NodeTypeResize_Memcached_Ec2Classic (954.44s)
--- PASS: TestAccAWSElasticacheCluster_NodeTypeResize_Redis_Ec2Classic (1022.11s)
--- PASS: TestAccAWSElasticacheCluster_NumCacheNodes_Decrease (995.54s)
--- PASS: TestAccAWSElasticacheCluster_NumCacheNodes_Increase (1038.00s)
--- PASS: TestAccAWSElasticacheCluster_NumCacheNodes_IncreaseWithPreferredAvailabilityZones (958.56s)
--- PASS: TestAccAWSElasticacheCluster_NumCacheNodes_Redis_Ec2Classic (6.63s)
--- PASS: TestAccAWSElasticacheCluster_ParameterGroupName_Default (596.16s)
--- PASS: TestAccAWSElasticacheCluster_Port_Ec2Classic (576.63s)
--- PASS: TestAccAWSElasticacheCluster_ReplicationGroupID_AvailabilityZone_Ec2Classic (1214.33s)
--- PASS: TestAccAWSElasticacheCluster_ReplicationGroupID_InvalidAttributes (8.37s)
--- PASS: TestAccAWSElasticacheCluster_ReplicationGroupID_MultipleReplica_Ec2Classic (1884.91s)
--- PASS: TestAccAWSElasticacheCluster_ReplicationGroupID_SingleReplica_Ec2Classic (1746.94s)
--- PASS: TestAccAWSElasticacheCluster_SecurityGroup (626.97s)
--- PASS: TestAccAWSElasticacheCluster_snapshotsWithUpdates (1167.22s)
--- PASS: TestAccAWSElasticacheCluster_vpc (646.18s)
--- PASS: TestAccAWSElasticacheReplicationGroup_basic (1857.56s)
--- PASS: TestAccAWSElasticacheReplicationGroup_clusteringAndCacheNodesCausesError (10.99s)
--- PASS: TestAccAWSElasticacheReplicationGroup_ClusterMode_Basic (1875.48s)
--- PASS: TestAccAWSElasticacheReplicationGroup_ClusterMode_NumNodeGroups (3713.74s)
--- PASS: TestAccAWSElasticacheReplicationGroup_enableAtRestEncryption (1419.18s)
--- PASS: TestAccAWSElasticacheReplicationGroup_enableAuthTokenTransitEncryption (734.55s)
--- PASS: TestAccAWSElasticacheReplicationGroup_enableSnapshotting (1917.01s)
--- PASS: TestAccAWSElasticacheReplicationGroup_importBasic (806.91s)
--- PASS: TestAccAWSElasticacheReplicationGroup_multiAzInVpc (950.44s)
--- PASS: TestAccAWSElasticacheReplicationGroup_NumberCacheClusters (2821.85s)
--- PASS: TestAccAWSElasticacheReplicationGroup_NumberCacheClusters_Failover_AutoFailoverDisabled (2029.86s)
--- PASS: TestAccAWSElasticacheReplicationGroup_NumberCacheClusters_Failover_AutoFailoverEnabled (1919.87s)
--- PASS: TestAccAWSElasticacheReplicationGroup_redisClusterInVpc2 (737.16s)
--- PASS: TestAccAWSElasticacheReplicationGroup_updateDescription (1240.24s)
--- PASS: TestAccAWSElasticacheReplicationGroup_updateMaintenanceWindow (1047.59s)
--- PASS: TestAccAWSElasticacheReplicationGroup_updateNodeSize (1985.53s)
--- PASS: TestAccAWSElasticacheReplicationGroup_updateParameterGroup (900.33s)
--- PASS: TestAccAWSElasticacheReplicationGroup_Uppercase (594.69s)
--- PASS: TestAccAWSElasticacheReplicationGroup_vpc (1355.75s)
```
  • Loading branch information
bflad committed Aug 30, 2019
1 parent bc7a949 commit 31b52a3
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 156 deletions.
9 changes: 8 additions & 1 deletion aws/resource_aws_elasticache_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"log"
"regexp"
"sort"
"strings"
"time"
Expand Down Expand Up @@ -95,7 +96,13 @@ func resourceAwsElasticacheCluster() *schema.Resource {
// with non-converging diffs.
return strings.ToLower(val.(string))
},
ValidateFunc: validateElastiCacheClusterId,
ValidateFunc: validation.All(
validation.StringLenBetween(1, 50),
validation.StringMatch(regexp.MustCompile(`^[0-9a-z-]+$`), "must contain only lowercase alphanumeric characters and hyphens"),
validation.StringMatch(regexp.MustCompile(`^[a-z]`), "must begin with a lowercase letter"),
validateStringNotMatch(regexp.MustCompile(`--`), "cannot contain two consecutive hyphens"),
validateStringNotMatch(regexp.MustCompile(`-$`), "cannot end with a hyphen"),
),
},
"configuration_endpoint": {
Type: schema.TypeString,
Expand Down
62 changes: 36 additions & 26 deletions aws/resource_aws_elasticache_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func TestAccAWSElasticacheCluster_Engine_Memcached_Ec2Classic(t *testing.T) {
defer os.Setenv("AWS_DEFAULT_REGION", oldvar)

var ec elasticache.CacheCluster
rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(8))
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_elasticache_cluster.bar"

resource.ParallelTest(t, resource.TestCase{
Expand Down Expand Up @@ -110,7 +110,7 @@ func TestAccAWSElasticacheCluster_Engine_Redis_Ec2Classic(t *testing.T) {
defer os.Setenv("AWS_DEFAULT_REGION", oldvar)

var ec elasticache.CacheCluster
rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(8))
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_elasticache_cluster.bar"

resource.ParallelTest(t, resource.TestCase{
Expand Down Expand Up @@ -141,7 +141,7 @@ func TestAccAWSElasticacheCluster_Engine_Redis_Ec2Classic(t *testing.T) {

func TestAccAWSElasticacheCluster_ParameterGroupName_Default(t *testing.T) {
var ec elasticache.CacheCluster
rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(8))
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_elasticache_cluster.test"

resource.ParallelTest(t, resource.TestCase{
Expand Down Expand Up @@ -177,7 +177,7 @@ func TestAccAWSElasticacheCluster_Port_Ec2Classic(t *testing.T) {

var ec elasticache.CacheCluster
port := 11212
rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(8))
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_elasticache_cluster.bar"

resource.ParallelTest(t, resource.TestCase{
Expand Down Expand Up @@ -271,7 +271,7 @@ func TestAccAWSElasticacheCluster_snapshotsWithUpdates(t *testing.T) {

func TestAccAWSElasticacheCluster_NumCacheNodes_Decrease(t *testing.T) {
var ec elasticache.CacheCluster
rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(8))
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_elasticache_cluster.bar"

resource.ParallelTest(t, resource.TestCase{
Expand Down Expand Up @@ -299,7 +299,7 @@ func TestAccAWSElasticacheCluster_NumCacheNodes_Decrease(t *testing.T) {

func TestAccAWSElasticacheCluster_NumCacheNodes_Increase(t *testing.T) {
var ec elasticache.CacheCluster
rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(8))
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_elasticache_cluster.bar"

resource.ParallelTest(t, resource.TestCase{
Expand Down Expand Up @@ -327,7 +327,7 @@ func TestAccAWSElasticacheCluster_NumCacheNodes_Increase(t *testing.T) {

func TestAccAWSElasticacheCluster_NumCacheNodes_IncreaseWithPreferredAvailabilityZones(t *testing.T) {
var ec elasticache.CacheCluster
rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(8))
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_elasticache_cluster.bar"

resource.ParallelTest(t, resource.TestCase{
Expand Down Expand Up @@ -369,8 +369,6 @@ func TestAccAWSElasticacheCluster_vpc(t *testing.T) {
testAccCheckAWSElasticacheSubnetGroupExists("aws_elasticache_subnet_group.bar", &csg),
testAccCheckAWSElasticacheClusterExists("aws_elasticache_cluster.bar", &ec),
testAccCheckAWSElasticacheClusterAttributes(&ec),
resource.TestCheckResourceAttr(
"aws_elasticache_cluster.bar", "availability_zone", "us-west-2a"),
),
},
},
Expand Down Expand Up @@ -404,7 +402,7 @@ func TestAccAWSElasticacheCluster_AZMode_Memcached_Ec2Classic(t *testing.T) {
defer os.Setenv("AWS_DEFAULT_REGION", oldvar)

var cluster elasticache.CacheCluster
rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(8))
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_elasticache_cluster.bar"

resource.ParallelTest(t, resource.TestCase{
Expand Down Expand Up @@ -441,7 +439,7 @@ func TestAccAWSElasticacheCluster_AZMode_Redis_Ec2Classic(t *testing.T) {
defer os.Setenv("AWS_DEFAULT_REGION", oldvar)

var cluster elasticache.CacheCluster
rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(8))
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_elasticache_cluster.bar"

resource.ParallelTest(t, resource.TestCase{
Expand Down Expand Up @@ -474,7 +472,7 @@ func TestAccAWSElasticacheCluster_EngineVersion_Memcached_Ec2Classic(t *testing.
defer os.Setenv("AWS_DEFAULT_REGION", oldvar)

var pre, mid, post elasticache.CacheCluster
rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(8))
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_elasticache_cluster.bar"

resource.ParallelTest(t, resource.TestCase{
Expand Down Expand Up @@ -515,7 +513,7 @@ func TestAccAWSElasticacheCluster_EngineVersion_Redis_Ec2Classic(t *testing.T) {
defer os.Setenv("AWS_DEFAULT_REGION", oldvar)

var pre, mid, post elasticache.CacheCluster
rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(8))
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_elasticache_cluster.bar"

resource.ParallelTest(t, resource.TestCase{
Expand Down Expand Up @@ -556,7 +554,7 @@ func TestAccAWSElasticacheCluster_NodeTypeResize_Memcached_Ec2Classic(t *testing
defer os.Setenv("AWS_DEFAULT_REGION", oldvar)

var pre, post elasticache.CacheCluster
rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(8))
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_elasticache_cluster.bar"

resource.ParallelTest(t, resource.TestCase{
Expand Down Expand Up @@ -589,7 +587,7 @@ func TestAccAWSElasticacheCluster_NodeTypeResize_Redis_Ec2Classic(t *testing.T)
defer os.Setenv("AWS_DEFAULT_REGION", oldvar)

var pre, post elasticache.CacheCluster
rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(8))
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_elasticache_cluster.bar"

resource.ParallelTest(t, resource.TestCase{
Expand Down Expand Up @@ -621,7 +619,7 @@ func TestAccAWSElasticacheCluster_NumCacheNodes_Redis_Ec2Classic(t *testing.T) {
os.Setenv("AWS_DEFAULT_REGION", "us-east-1")
defer os.Setenv("AWS_DEFAULT_REGION", oldvar)

rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(8))
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccEC2ClassicPreCheck(t) },
Expand All @@ -641,7 +639,7 @@ func TestAccAWSElasticacheCluster_ReplicationGroupID_InvalidAttributes(t *testin
os.Setenv("AWS_DEFAULT_REGION", "us-east-1")
defer os.Setenv("AWS_DEFAULT_REGION", oldvar)

rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(8))
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccEC2ClassicPreCheck(t) },
Expand Down Expand Up @@ -723,7 +721,7 @@ func TestAccAWSElasticacheCluster_ReplicationGroupID_AvailabilityZone_Ec2Classic

var cluster elasticache.CacheCluster
var replicationGroup elasticache.ReplicationGroup
rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(7))
rName := acctest.RandomWithPrefix("tf-acc-test")
clusterResourceName := "aws_elasticache_cluster.replica"
replicationGroupResourceName := "aws_elasticache_replication_group.test"

Expand Down Expand Up @@ -751,7 +749,7 @@ func TestAccAWSElasticacheCluster_ReplicationGroupID_SingleReplica_Ec2Classic(t

var cluster elasticache.CacheCluster
var replicationGroup elasticache.ReplicationGroup
rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(7))
rName := acctest.RandomWithPrefix("tf-acc-test")
clusterResourceName := "aws_elasticache_cluster.replica"
replicationGroupResourceName := "aws_elasticache_replication_group.test"

Expand Down Expand Up @@ -782,7 +780,7 @@ func TestAccAWSElasticacheCluster_ReplicationGroupID_MultipleReplica_Ec2Classic(

var cluster1, cluster2 elasticache.CacheCluster
var replicationGroup elasticache.ReplicationGroup
rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(7))
rName := acctest.RandomWithPrefix("tf-acc-test")
clusterResourceName1 := "aws_elasticache_cluster.replica.0"
clusterResourceName2 := "aws_elasticache_cluster.replica.1"
replicationGroupResourceName := "aws_elasticache_replication_group.test"
Expand Down Expand Up @@ -1095,6 +1093,12 @@ resource "aws_elasticache_cluster" "bar" {
}

var testAccAWSElasticacheClusterInVPCConfig = fmt.Sprintf(`
data "aws_availability_zones" "available" {
# InsufficientCacheClusterCapacity: cache.m1.small (VPC) is not currently supported in the availability zone us-east-1b
blacklisted_zone_ids = ["use1-az1"]
state = "available"
}
resource "aws_vpc" "foo" {
cidr_block = "192.168.0.0/16"
tags = {
Expand All @@ -1105,7 +1109,7 @@ resource "aws_vpc" "foo" {
resource "aws_subnet" "foo" {
vpc_id = "${aws_vpc.foo.id}"
cidr_block = "192.168.0.0/20"
availability_zone = "us-west-2a"
availability_zone = "${data.aws_availability_zones.available.names[0]}"
tags = {
Name = "tf-acc-elasticache-cluster-in-vpc"
}
Expand Down Expand Up @@ -1143,7 +1147,7 @@ resource "aws_elasticache_cluster" "bar" {
security_group_ids = ["${aws_security_group.bar.id}"]
parameter_group_name = "default.redis2.8"
notification_topic_arn = "${aws_sns_topic.topic_example.arn}"
availability_zone = "us-west-2a"
availability_zone = "${data.aws_availability_zones.available.names[0]}"
}
resource "aws_sns_topic" "topic_example" {
Expand All @@ -1152,6 +1156,12 @@ resource "aws_sns_topic" "topic_example" {
`, acctest.RandInt(), acctest.RandInt(), acctest.RandString(10))

var testAccAWSElasticacheClusterMultiAZInVPCConfig = fmt.Sprintf(`
data "aws_availability_zones" "available" {
# InsufficientCacheClusterCapacity: cache.m1.small (VPC) is not currently supported in the availability zone us-east-1b
blacklisted_zone_ids = ["use1-az1"]
state = "available"
}
resource "aws_vpc" "foo" {
cidr_block = "192.168.0.0/16"
tags = {
Expand All @@ -1162,7 +1172,7 @@ resource "aws_vpc" "foo" {
resource "aws_subnet" "foo" {
vpc_id = "${aws_vpc.foo.id}"
cidr_block = "192.168.0.0/20"
availability_zone = "us-west-2a"
availability_zone = "${data.aws_availability_zones.available.names[0]}"
tags = {
Name = "tf-acc-elasticache-cluster-multi-az-in-vpc-foo"
}
Expand All @@ -1171,7 +1181,7 @@ resource "aws_subnet" "foo" {
resource "aws_subnet" "bar" {
vpc_id = "${aws_vpc.foo.id}"
cidr_block = "192.168.16.0/20"
availability_zone = "us-east-1c"
availability_zone = "${data.aws_availability_zones.available.names[1]}"
tags = {
Name = "tf-acc-elasticache-cluster-multi-az-in-vpc-bar"
}
Expand Down Expand Up @@ -1208,8 +1218,8 @@ resource "aws_elasticache_cluster" "bar" {
security_group_ids = ["${aws_security_group.bar.id}"]
az_mode = "cross-az"
preferred_availability_zones = [
"us-west-2a",
"us-west-2b"
"${data.aws_availability_zones.available.names[0]}",
"${data.aws_availability_zones.available.names[1]}"
]
}
`, acctest.RandInt(), acctest.RandInt(), acctest.RandString(10))
Expand Down
33 changes: 7 additions & 26 deletions aws/resource_aws_elasticache_replication_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,13 @@ func resourceAwsElasticacheReplicationGroup() *schema.Resource {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateAwsElastiCacheReplicationGroupId,
ValidateFunc: validation.All(
validation.StringLenBetween(1, 40),
validation.StringMatch(regexp.MustCompile(`^[0-9a-zA-Z-]+$`), "must contain only alphanumeric characters and hyphens"),
validation.StringMatch(regexp.MustCompile(`^[a-zA-Z]`), "must begin with a letter"),
validateStringNotMatch(regexp.MustCompile(`--`), "cannot contain two consecutive hyphens"),
validateStringNotMatch(regexp.MustCompile(`-$`), "cannot end with a hyphen"),
),
StateFunc: func(val interface{}) string {
return strings.ToLower(val.(string))
},
Expand Down Expand Up @@ -919,28 +925,3 @@ func validateAwsElastiCacheReplicationGroupEngine(v interface{}, k string) (ws [
}
return
}

func validateAwsElastiCacheReplicationGroupId(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if (len(value) < 1) || (len(value) > 20) {
errors = append(errors, fmt.Errorf(
"%q must contain from 1 to 20 alphanumeric characters or hyphens", k))
}
if !regexp.MustCompile(`^[0-9a-zA-Z-]+$`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"only alphanumeric characters and hyphens allowed in %q", k))
}
if !regexp.MustCompile(`^[a-zA-Z]`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"first character of %q must be a letter", k))
}
if regexp.MustCompile(`--`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q cannot contain two consecutive hyphens", k))
}
if regexp.MustCompile(`-$`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q cannot end with a hyphen", k))
}
return
}
Loading

0 comments on commit 31b52a3

Please sign in to comment.