From 9cc33a71dde0469cdb3d0c48867487d17ebefd3e Mon Sep 17 00:00:00 2001 From: Rob Selway Date: Sat, 16 May 2020 17:01:41 +0100 Subject: [PATCH 1/3] Align azurerm_sql_virtual_network_rule name validation with azure validation --- .../resource_arm_sql_virtual_network_rule.go | 30 +++++--- ...ource_arm_sql_virtual_network_rule_test.go | 68 +++++++++++++++---- 2 files changed, 77 insertions(+), 21 deletions(-) diff --git a/azurerm/internal/services/sql/resource_arm_sql_virtual_network_rule.go b/azurerm/internal/services/sql/resource_arm_sql_virtual_network_rule.go index 06f4f89ebd67..67503c41aaec 100644 --- a/azurerm/internal/services/sql/resource_arm_sql_virtual_network_rule.go +++ b/azurerm/internal/services/sql/resource_arm_sql_virtual_network_rule.go @@ -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) < 2 { 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)) } @@ -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 diff --git a/azurerm/internal/services/sql/tests/resource_arm_sql_virtual_network_rule_test.go b/azurerm/internal/services/sql/tests/resource_arm_sql_virtual_network_rule_test.go index 2f3f0e37197b..b4a078687966 100644 --- a/azurerm/internal/services/sql/tests/resource_arm_sql_virtual_network_rule_test.go +++ b/azurerm/internal/services/sql/tests/resource_arm_sql_virtual_network_rule_test.go @@ -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, }, { @@ -226,9 +226,9 @@ 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) @@ -236,14 +236,28 @@ func TestResourceAzureRMSqlVirtualNetworkRule_invalidNameValidation(t *testing.T 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, }, { @@ -295,9 +309,34 @@ func TestResourceAzureRMSqlVirtualNetworkRule_validNameValidation(t *testing.T) Value: "this----1s----a----ru1e", ErrCount: 0, }, - // Test numbers (except for first character) + // Test underscores + { + Value: "this_is_a_rule", + ErrCount: 0, + }, + // Test ending with underscore + { + Value: "this_is_a_rule_", + ErrCount: 0, + }, + // Test multiple hyphens in a row + { + Value: "this____1s____a____ru1e", + ErrCount: 0, + }, + // Test periods + { + Value: "this.is_..rule", + ErrCount: 0, + }, + // Test multiple periods in a row + { + Value: "this....1s....a....ru1e", + ErrCount: 0, + }, + // Test numbers { - Value: "v1108501298509850810258091285091820-5", + Value: "1108501298509850810258091285091820-5", ErrCount: 0, }, // Test a lot of hyphens and numbers @@ -305,14 +344,19 @@ func TestResourceAzureRMSqlVirtualNetworkRule_validNameValidation(t *testing.T) 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, }, } From e9ecab41fa7953f6eb302233e859ff2049e4fe20 Mon Sep 17 00:00:00 2001 From: Rob Selway Date: Sat, 16 May 2020 17:07:10 +0100 Subject: [PATCH 2/3] minor test fixes --- .../sql/tests/resource_arm_sql_virtual_network_rule_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/azurerm/internal/services/sql/tests/resource_arm_sql_virtual_network_rule_test.go b/azurerm/internal/services/sql/tests/resource_arm_sql_virtual_network_rule_test.go index b4a078687966..124419dbfa3f 100644 --- a/azurerm/internal/services/sql/tests/resource_arm_sql_virtual_network_rule_test.go +++ b/azurerm/internal/services/sql/tests/resource_arm_sql_virtual_network_rule_test.go @@ -319,14 +319,14 @@ func TestResourceAzureRMSqlVirtualNetworkRule_validNameValidation(t *testing.T) Value: "this_is_a_rule_", ErrCount: 0, }, - // Test multiple hyphens in a row + // Test multiple underscoress in a row { Value: "this____1s____a____ru1e", ErrCount: 0, }, // Test periods { - Value: "this.is_..rule", + Value: "this.is.a.rule", ErrCount: 0, }, // Test multiple periods in a row From f183a6a1e06897add2a5d92b09f0346dbbd89673 Mon Sep 17 00:00:00 2001 From: Rob Selway Date: Sun, 17 May 2020 12:43:17 +0100 Subject: [PATCH 3/3] Test fix - name too short error returned for empty name --- .../services/sql/resource_arm_sql_virtual_network_rule.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azurerm/internal/services/sql/resource_arm_sql_virtual_network_rule.go b/azurerm/internal/services/sql/resource_arm_sql_virtual_network_rule.go index 67503c41aaec..38e320ee178c 100644 --- a/azurerm/internal/services/sql/resource_arm_sql_virtual_network_rule.go +++ b/azurerm/internal/services/sql/resource_arm_sql_virtual_network_rule.go @@ -218,7 +218,7 @@ func ValidateSqlVirtualNetworkRuleName(v interface{}, k string) (warnings []stri } // Cannot be shorter than 2 characters - if len(value) < 2 { + if len(value) == 1 { errors = append(errors, fmt.Errorf( "%q cannot be shorter than 2 characters: %q", k, value)) }