-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring the PostgreSQL Virtual Network Rule (#1882)
- Loading branch information
1 parent
ed19944
commit d1a7b19
Showing
5 changed files
with
302 additions
and
327 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package validate | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
) | ||
|
||
func VirtualNetworkRuleName(v interface{}, k string) (ws []string, errors []error) { | ||
value := v.(string) | ||
|
||
// Cannot be empty | ||
if len(value) == 0 { | ||
errors = append(errors, fmt.Errorf( | ||
"%q cannot be an empty string: %q", k, value)) | ||
} | ||
|
||
// Cannot be more than 128 characters | ||
if len(value) > 128 { | ||
errors = append(errors, fmt.Errorf( | ||
"%q cannot be longer than 128 characters: %q", k, value)) | ||
} | ||
|
||
// Must only contain alphanumeric characters or hyphens | ||
if !regexp.MustCompile(`^[A-Za-z0-9-]*$`).MatchString(value) { | ||
errors = append(errors, fmt.Errorf( | ||
"%q can only contain alphanumeric characters and hyphens: %q", | ||
k, value)) | ||
} | ||
|
||
// Cannot end in a hyphen | ||
if regexp.MustCompile(`-$`).MatchString(value) { | ||
errors = append(errors, fmt.Errorf( | ||
"%q cannot end with a hyphen: %q", k, value)) | ||
} | ||
|
||
// Cannot start with a number or hyphen | ||
if regexp.MustCompile(`^[0-9-]`).MatchString(value) { | ||
errors = append(errors, fmt.Errorf( | ||
"%q cannot start with a number or hyphen: %q", k, value)) | ||
} | ||
|
||
return | ||
} |
126 changes: 126 additions & 0 deletions
126
azurerm/helpers/validate/virtual_network_rule_name_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package validate | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/acctest" | ||
) | ||
|
||
func TestVirtualNetworkRule_invalidNameValidation(t *testing.T) { | ||
cases := []struct { | ||
Value string | ||
ErrCount int | ||
}{ | ||
// Must only contain alphanumeric characters or hyphens (4 cases) | ||
{ | ||
Value: "test!Rule", | ||
ErrCount: 1, | ||
}, | ||
{ | ||
Value: "test_Rule", | ||
ErrCount: 1, | ||
}, | ||
{ | ||
Value: "test:Rule", | ||
ErrCount: 1, | ||
}, | ||
{ | ||
Value: "test'Rule", | ||
ErrCount: 1, | ||
}, | ||
// Cannot be more than 128 characters (1 case - ensure starts with a letter) | ||
{ | ||
Value: fmt.Sprintf("v%s", acctest.RandString(128)), | ||
ErrCount: 1, | ||
}, | ||
// Cannot be empty (1 case) | ||
{ | ||
Value: "", | ||
ErrCount: 1, | ||
}, | ||
// Cannot end in a hyphen (1 case) | ||
{ | ||
Value: "testRule-", | ||
ErrCount: 1, | ||
}, | ||
// Cannot start with a number or hyphen (2 cases) | ||
{ | ||
Value: "7testRule", | ||
ErrCount: 1, | ||
}, | ||
{ | ||
Value: "-testRule", | ||
ErrCount: 1, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
_, errors := VirtualNetworkRuleName(tc.Value, "azurerm_postgresql_virtual_network_rule") | ||
|
||
if len(errors) != tc.ErrCount { | ||
t.Fatalf("Bad: Expected the Azure RM PostgreSQL Virtual Network Rule Name to trigger a validation error.") | ||
} | ||
} | ||
} | ||
|
||
func TestResourceAzureRMPostgreSQLVirtualNetworkRule_validNameValidation(t *testing.T) { | ||
cases := []struct { | ||
Value string | ||
ErrCount int | ||
}{ | ||
// Test all lowercase | ||
{ | ||
Value: "thisisarule", | ||
ErrCount: 0, | ||
}, | ||
// Test all uppercase | ||
{ | ||
Value: "THISISARULE", | ||
ErrCount: 0, | ||
}, | ||
// Test alternating cases | ||
{ | ||
Value: "tHiSiSaRuLe", | ||
ErrCount: 0, | ||
}, | ||
// Test hyphens | ||
{ | ||
Value: "this-is-a-rule", | ||
ErrCount: 0, | ||
}, | ||
// Test multiple hyphens in a row | ||
{ | ||
Value: "this----1s----a----ru1e", | ||
ErrCount: 0, | ||
}, | ||
// Test numbers (except for first character) | ||
{ | ||
Value: "v1108501298509850810258091285091820-5", | ||
ErrCount: 0, | ||
}, | ||
// Test a lot of hyphens and numbers | ||
{ | ||
Value: "x-5-4-1-2-5-2-6-1-5-2-5-1-2-5-6-2-2", | ||
ErrCount: 0, | ||
}, | ||
// Test exactly 128 characters | ||
{ | ||
Value: fmt.Sprintf("v%s", acctest.RandString(127)), | ||
ErrCount: 0, | ||
}, | ||
// Test short, 1-letter name | ||
{ | ||
Value: "V", | ||
ErrCount: 0, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
_, errors := VirtualNetworkRuleName(tc.Value, "azurerm_postgresql_virtual_network_rule") | ||
|
||
if len(errors) != tc.ErrCount { | ||
t.Fatalf("Bad: Expected the Virtual Network Rule Name pass name validation successfully but triggered a validation error.") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.