From ab8f5de8e70bcc6de0b2f9a012bab3dea655185c Mon Sep 17 00:00:00 2001 From: Anshul Sharma Date: Fri, 11 Nov 2016 15:29:00 +0530 Subject: [PATCH] Allow underscore in database_name [redshift] (#10019) * allow underscore in database_name [redshift] Fixes #10009 * Added Test Cases To Validate Redshift DBName * Remove Old Test Cases Regarding Redshift DBName Validation * Added More Test Cases For Redshift DBName --- .../aws/resource_aws_redshift_cluster.go | 8 ++- .../aws/resource_aws_redshift_cluster_test.go | 72 +++++++++---------- 2 files changed, 42 insertions(+), 38 deletions(-) diff --git a/builtin/providers/aws/resource_aws_redshift_cluster.go b/builtin/providers/aws/resource_aws_redshift_cluster.go index a1603d957345..c95127ea372d 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 9ef6ffcb1015..54df6422f55c 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