Skip to content

Commit

Permalink
resource/aws_dax_cluster: Refactor now invalid validateElastiCacheClu…
Browse files Browse the repository at this point in the history
…sterId into inline ValidateFunc

Reference: #8424

Output from acceptance testing:

```
--- PASS: TestAccAWSDAXCluster_encryption_disabled (739.98s)
--- PASS: TestAccAWSDAXCluster_basic (756.03s)
--- PASS: TestAccAWSDAXCluster_importBasic (796.86s)
--- PASS: TestAccAWSDAXCluster_encryption_enabled (865.80s)
--- PASS: TestAccAWSDAXCluster_resize (1414.26s)
```
  • Loading branch information
bflad committed Aug 30, 2019
1 parent 31b52a3 commit f6b1364
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 67 deletions.
11 changes: 9 additions & 2 deletions aws/resource_aws_dax_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package aws
import (
"fmt"
"log"
"regexp"
"sort"
"strings"
"time"
Expand All @@ -11,6 +12,7 @@ import (
"github.com/aws/aws-sdk-go/service/dax"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)

func resourceAwsDaxCluster() *schema.Resource {
Expand Down Expand Up @@ -41,8 +43,13 @@ func resourceAwsDaxCluster() *schema.Resource {
StateFunc: func(val interface{}) string {
return strings.ToLower(val.(string))
},
// DAX follows the same naming convention as ElastiCache clusters
ValidateFunc: validateElastiCacheClusterId,
ValidateFunc: validation.All(
validation.StringLenBetween(1, 20),
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"),
),
},
"iam_role_arn": {
Type: schema.TypeString,
Expand Down
25 changes: 0 additions & 25 deletions aws/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,31 +218,6 @@ func validateNeptuneEngine() schema.SchemaValidateFunc {
}, false)
}

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

func validateASGScheduleTimestamp(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
_, err := time.Parse(awsAutoscalingScheduleTimeLayout, value)
Expand Down
40 changes: 0 additions & 40 deletions aws/validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -630,46 +630,6 @@ func TestValidateSagemakerName(t *testing.T) {
}
}

func TestResourceAWSElastiCacheClusterIdValidation(t *testing.T) {
cases := []struct {
Value string
ErrCount int
}{
{
Value: "tEsting",
ErrCount: 1,
},
{
Value: "t.sting",
ErrCount: 1,
},
{
Value: "t--sting",
ErrCount: 1,
},
{
Value: "1testing",
ErrCount: 1,
},
{
Value: "testing-",
ErrCount: 1,
},
{
Value: randomString(65),
ErrCount: 1,
},
}

for _, tc := range cases {
_, errors := validateElastiCacheClusterId(tc.Value, "aws_elasticache_cluster_cluster_id")

if len(errors) != tc.ErrCount {
t.Fatalf("Expected the ElastiCache Cluster cluster_id to trigger a validation error")
}
}
}

func TestValidateDbEventSubscriptionName(t *testing.T) {
validNames := []string{
"valid-name",
Expand Down

0 comments on commit f6b1364

Please sign in to comment.