diff --git a/builtin/providers/aws/resource_aws_redshift_cluster.go b/builtin/providers/aws/resource_aws_redshift_cluster.go index a1603d957..c95127ea3 100644 --- a/builtin/providers/aws/resource_aws_redshift_cluster.go +++ b/builtin/providers/aws/resource_aws_redshift_cluster.go @@ -844,9 +844,13 @@ func validateRedshiftClusterIdentifier(v interface{}, k string) (ws []string, er func validateRedshiftClusterDbName(v interface{}, k string) (ws []string, errors []error) { value := v.(string) - if !regexp.MustCompile(`^[0-9a-z]+$`).MatchString(value) { + if !regexp.MustCompile(`^[0-9A-Za-z_$]+$`).MatchString(value) { errors = append(errors, fmt.Errorf( - "only lowercase letters and numeric characters allowed in %q", k)) + "only alphanumeric characters, underscores, and dollar signs are 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 or underscore", k)) } if len(value) > 64 { errors = append(errors, fmt.Errorf( diff --git a/builtin/providers/aws/resource_aws_redshift_cluster_test.go b/builtin/providers/aws/resource_aws_redshift_cluster_test.go index 9ef6ffcb1..54df6422f 100644 --- a/builtin/providers/aws/resource_aws_redshift_cluster_test.go +++ b/builtin/providers/aws/resource_aws_redshift_cluster_test.go @@ -13,6 +13,42 @@ import ( "github.com/hashicorp/terraform/terraform" ) +func TestValidateRedshiftClusterDbName(t *testing.T) { + validNames := []string{ + "testdbname", + "test_dbname", + "testdbname123", + "TestDBname", + "testdbname$hashicorp", + "_dbname", + } + for _, v := range validNames { + _, errors := validateRedshiftClusterDbName(v, "name") + if len(errors) != 0 { + t.Fatalf("%q should be a valid Redshift DBName: %q", v, errors) + } + } + + invalidNames := []string{ + "!", + "/", + " ", + ":", + ";", + "test name", + "/slash-at-the-beginning", + "slash-at-the-end/", + "", + randomString(100), + } + for _, v := range invalidNames { + _, errors := validateRedshiftClusterDbName(v, "name") + if len(errors) == 0 { + t.Fatalf("%q should be an invalid Redshift DBName", v) + } + } +} + func TestAccAWSRedshiftCluster_basic(t *testing.T) { var v redshift.Cluster @@ -340,42 +376,6 @@ func TestResourceAWSRedshiftClusterIdentifierValidation(t *testing.T) { } } -func TestResourceAWSRedshiftClusterDbNameValidation(t *testing.T) { - cases := []struct { - Value string - ErrCount int - }{ - { - Value: "tEsting", - ErrCount: 1, - }, - { - Value: "testing1", - ErrCount: 0, - }, - { - Value: "testing-", - ErrCount: 1, - }, - { - Value: "", - ErrCount: 2, - }, - { - Value: randomString(65), - ErrCount: 1, - }, - } - - for _, tc := range cases { - _, errors := validateRedshiftClusterDbName(tc.Value, "aws_redshift_cluster_database_name") - - if len(errors) != tc.ErrCount { - t.Fatalf("Expected the Redshift Cluster database_name to trigger a validation error") - } - } -} - func TestResourceAWSRedshiftClusterFinalSnapshotIdentifierValidation(t *testing.T) { cases := []struct { Value string