Skip to content

Commit

Permalink
Merge pull request #6064 from njuCZ/issue_6062
Browse files Browse the repository at this point in the history
`azurerm_postgresql_server` - align server name validation rule with the portal
  • Loading branch information
tombuildsstuff committed Mar 19, 2020
2 parents 753bf29 + 0033dc8 commit 674a236
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (
)

func ValidatePSQLServerName(i interface{}, k string) (_ []string, errors []error) {
if m, regexErrs := validate.RegExHelper(i, k, `^[0-9a-z]{2}[-0-9a-z]{0,60}[0-9a-z]$`); !m {
errors = append(regexErrs, fmt.Errorf("%q can contain only lowercase letters, numbers, and '-', but can't start or end with '-' or have more than 63 characters.", k))
if m, regexErrs := validate.RegExHelper(i, k, `^[0-9a-z][-0-9a-z]{1,61}[0-9a-z]$`); !m {
errors = append(regexErrs, fmt.Errorf("%q can contain only lowercase letters, numbers, and '-', but can't start or end with '-'. And must be at least 3 characters and at most 63 characters", k))
}

return nil, errors
Expand Down
68 changes: 68 additions & 0 deletions azurerm/internal/services/postgres/validation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package postgres

import (
"testing"
)

func TestValidatePSQLServerName(t *testing.T) {
testData := []struct {
input string
expected bool
}{
{
// empty
input: "",
expected: false,
},
{
// basic example
input: "ab-c",
expected: true,
},
{
// can't contain upper case letter
input: "AbcD",
expected: false,
},
{
// can't start with a hyphen
input: "-abc",
expected: false,
},
{
// can't contain underscore
input: "ab_c",
expected: false,
},
{
// can't end with hyphen
input: "abc-",
expected: false,
},
{
// can not short than 3 characters
input: "ab",
expected: false,
},
{
// 63 chars
input: "abcdefghijklmnopqrstuvwxyzabcdefabcdefghijklmnopqrstuvwxyzabcde",
expected: true,
},
{
// 64 chars
input: "abcdefghijklmnopqrstuvwxyzabcdefabcdefghijklmnopqrstuvwxyzabcdef",
expected: false,
},
}

for _, v := range testData {
t.Logf("[DEBUG] Testing %q..", v.input)

_, errors := ValidatePSQLServerName(v.input, "name")
actual := len(errors) == 0
if v.expected != actual {
t.Fatalf("Expected %t but got %t", v.expected, actual)
}
}
}

0 comments on commit 674a236

Please sign in to comment.