Skip to content

Commit

Permalink
Allow underscore in database_name [redshift] (hashicorp#10019)
Browse files Browse the repository at this point in the history
* allow underscore in database_name [redshift]

Fixes hashicorp#10009

* Added Test Cases To Validate Redshift DBName

* Remove Old Test Cases Regarding Redshift DBName Validation

* Added More Test Cases For Redshift DBName
  • Loading branch information
sharmaansh21 authored and fatmcgav committed Feb 27, 2017
1 parent 84c426c commit ab8f5de
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 38 deletions.
8 changes: 6 additions & 2 deletions builtin/providers/aws/resource_aws_redshift_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
72 changes: 36 additions & 36 deletions builtin/providers/aws/resource_aws_redshift_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ab8f5de

Please sign in to comment.