Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

azurerm_sql_virtual_network_rule name validation #6968

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -217,16 +217,22 @@ func ValidateSqlVirtualNetworkRuleName(v interface{}, k string) (warnings []stri
"%q cannot be an empty string: %q", k, value))
}

// Cannot be more than 128 characters
if len(value) > 128 {
// Cannot be shorter than 2 characters
if len(value) == 1 {
errors = append(errors, fmt.Errorf(
"%q cannot be longer than 128 characters: %q", k, value))
"%q cannot be shorter than 2 characters: %q", k, value))
}

// Must only contain alphanumeric characters or hyphens
if !regexp.MustCompile(`^[A-Za-z0-9-]*$`).MatchString(value) {
// Cannot be more than 64 characters
if len(value) > 64 {
errors = append(errors, fmt.Errorf(
"%q can only contain alphanumeric characters and hyphens: %q",
"%q cannot be longer than 64 characters: %q", k, value))
}

// Must only contain alphanumeric characters, underscores, periods or hyphens
if !regexp.MustCompile(`^[A-Za-z0-9-\._]*$`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q can only contain alphanumeric characters, underscores, periods and hyphens: %q",
k, value))
}

Expand All @@ -236,10 +242,16 @@ func ValidateSqlVirtualNetworkRuleName(v interface{}, k string) (warnings []stri
"%q cannot end with a hyphen: %q", k, value))
}

// Cannot start with a number or hyphen
if regexp.MustCompile(`^[0-9-]`).MatchString(value) {
// Cannot end in a period
if regexp.MustCompile(`\.$`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q cannot end with a period: %q", k, value))
}

// Cannot start with a period, underscore or hyphen
if regexp.MustCompile(`^[\._-]`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q cannot start with a number or hyphen: %q", k, value))
"%q cannot start with a period, underscore or hyphen: %q", k, value))
}

// There are multiple returns in the case that there is more than one invalid
Expand Down
Expand Up @@ -209,13 +209,13 @@ func TestResourceAzureRMSqlVirtualNetworkRule_invalidNameValidation(t *testing.T
Value string
ErrCount int
}{
// Must only contain alphanumeric characters or hyphens (4 cases)
// Must only contain alphanumeric characters, periods, underscores or hyphens (4 cases)
{
Value: "test!Rule",
ErrCount: 1,
},
{
Value: "test_Rule",
Value: "test&Rule",
ErrCount: 1,
},
{
Expand All @@ -226,24 +226,38 @@ func TestResourceAzureRMSqlVirtualNetworkRule_invalidNameValidation(t *testing.T
Value: "test'Rule",
ErrCount: 1,
},
// Cannot be more than 128 characters (1 case - ensure starts with a letter)
// Cannot be more than 64 characters (1 case - ensure starts with a letter)
{
Value: fmt.Sprintf("v%s", acctest.RandString(128)),
Value: fmt.Sprintf("v%s", acctest.RandString(64)),
ErrCount: 1,
},
// Cannot be empty (1 case)
{
Value: "",
ErrCount: 1,
},
// Cannot be single character (1 case)
{
Value: "a",
ErrCount: 1,
},
// Cannot end in a hyphen (1 case)
{
Value: "testRule-",
ErrCount: 1,
},
// Cannot start with a number or hyphen (2 cases)
// Cannot end in a period (1 case)
{
Value: "testRule.",
ErrCount: 1,
},
// Cannot start with a period, underscore or hyphen (3 cases)
{
Value: ".testRule",
ErrCount: 1,
},
{
Value: "7testRule",
Value: "_testRule",
ErrCount: 1,
},
{
Expand Down Expand Up @@ -295,24 +309,54 @@ func TestResourceAzureRMSqlVirtualNetworkRule_validNameValidation(t *testing.T)
Value: "this----1s----a----ru1e",
ErrCount: 0,
},
// Test numbers (except for first character)
// Test underscores
{
Value: "v1108501298509850810258091285091820-5",
Value: "this_is_a_rule",
ErrCount: 0,
},
// Test ending with underscore
{
Value: "this_is_a_rule_",
ErrCount: 0,
},
// Test multiple underscoress in a row
{
Value: "this____1s____a____ru1e",
ErrCount: 0,
},
// Test periods
{
Value: "this.is.a.rule",
ErrCount: 0,
},
// Test multiple periods in a row
{
Value: "this....1s....a....ru1e",
ErrCount: 0,
},
// Test numbers
{
Value: "1108501298509850810258091285091820-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
// Test a lot of underscores and numbers
{
Value: "x_5_4_1_2_5_2_6_1_5_2_5_1_2_5_6_2_2",
ErrCount: 0,
},
// Test a lot of periods and numbers
{
Value: fmt.Sprintf("v%s", acctest.RandString(127)),
Value: "x.5.4.1.2.5.2.6.1.5.2.5.1.2.5.6.2.2",
ErrCount: 0,
},
// Test short, 1-letter name
// Test exactly 64 characters
{
Value: "V",
Value: fmt.Sprintf("v%s", acctest.RandString(63)),
ErrCount: 0,
},
}
Expand Down