From 1e0814c262bed5e849f99efde00b42605fe767fc Mon Sep 17 00:00:00 2001 From: v-cheye Date: Mon, 25 May 2020 10:19:28 +0800 Subject: [PATCH 1/7] Fix validation for serivce bus topic name --- azurerm/helpers/azure/servicebus.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/azurerm/helpers/azure/servicebus.go b/azurerm/helpers/azure/servicebus.go index 99cdfe3db0ae..67732ac54d55 100644 --- a/azurerm/helpers/azure/servicebus.go +++ b/azurerm/helpers/azure/servicebus.go @@ -28,8 +28,8 @@ func ValidateServiceBusSubscriptionName() schema.SchemaValidateFunc { func ValidateServiceBusTopicName() schema.SchemaValidateFunc { return validation.StringMatch( - regexp.MustCompile("^[a-zA-Z][-._~a-zA-Z0-9]{0,258}([a-zA-Z0-9])?$"), - "The topic name can contain only letters, numbers, periods, hyphens, tildas and underscores. The namespace must start with a letter, and it must end with a letter or number and be less then 260 characters long.", + regexp.MustCompile("^[a-zA-Z0-9][-._/a-zA-Z0-9]{0,258}[a-zA-Z0-9]$"), + "The topic name can contain only letters, numbers, periods, hyphens, slashes and underscores. The namespace must start with a letter or number, and it must end with a letter or number and be less then 260 characters long.", ) } From 18385f9343c7c91d6fcb3ca7add57e075a3dc2c9 Mon Sep 17 00:00:00 2001 From: v-cheye Date: Mon, 25 May 2020 10:43:33 +0800 Subject: [PATCH 2/7] Update code --- azurerm/helpers/azure/servicebus.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/azurerm/helpers/azure/servicebus.go b/azurerm/helpers/azure/servicebus.go index 67732ac54d55..4543948b406f 100644 --- a/azurerm/helpers/azure/servicebus.go +++ b/azurerm/helpers/azure/servicebus.go @@ -28,8 +28,8 @@ func ValidateServiceBusSubscriptionName() schema.SchemaValidateFunc { func ValidateServiceBusTopicName() schema.SchemaValidateFunc { return validation.StringMatch( - regexp.MustCompile("^[a-zA-Z0-9][-._/a-zA-Z0-9]{0,258}[a-zA-Z0-9]$"), - "The topic name can contain only letters, numbers, periods, hyphens, slashes and underscores. The namespace must start with a letter or number, and it must end with a letter or number and be less then 260 characters long.", + regexp.MustCompile("^[a-zA-Z0-9][-._~a-zA-Z0-9]{0,258}([a-zA-Z0-9])?$"), + "The topic name can contain only letters, numbers, periods, hyphens, tildas and underscores. The namespace must start with a letter or number, and it must end with a letter or number and be less then 260 characters long.", ) } From bbc9ca6c7371496b7cb057039006c552e4a237bf Mon Sep 17 00:00:00 2001 From: v-cheye Date: Mon, 25 May 2020 17:25:38 +0800 Subject: [PATCH 3/7] Update code --- azurerm/helpers/azure/servicebus.go | 13 ++-- azurerm/helpers/azure/servicebus_test.go | 64 +++++++++++++++++++ .../resource_arm_servicebus_topic.go | 2 +- 3 files changed, 73 insertions(+), 6 deletions(-) create mode 100644 azurerm/helpers/azure/servicebus_test.go diff --git a/azurerm/helpers/azure/servicebus.go b/azurerm/helpers/azure/servicebus.go index 4543948b406f..b2552667d7d9 100644 --- a/azurerm/helpers/azure/servicebus.go +++ b/azurerm/helpers/azure/servicebus.go @@ -26,11 +26,14 @@ func ValidateServiceBusSubscriptionName() schema.SchemaValidateFunc { ) } -func ValidateServiceBusTopicName() schema.SchemaValidateFunc { - return validation.StringMatch( - regexp.MustCompile("^[a-zA-Z0-9][-._~a-zA-Z0-9]{0,258}([a-zA-Z0-9])?$"), - "The topic name can contain only letters, numbers, periods, hyphens, tildas and underscores. The namespace must start with a letter or number, and it must end with a letter or number and be less then 260 characters long.", - ) +func ValidateServiceBusTopicName(v interface{}, k string) (warnings []string, errors []error) { + value := v.(string) + + if !regexp.MustCompile(`^[a-zA-Z0-9]([-._~a-zA-Z0-9]{0,258}[a-zA-Z0-9])?$`).MatchString(value) { + errors = append(errors, fmt.Errorf("%q can contain only letters, numbers, periods, hyphens, tildas and underscores. The namespace must start with a letter or number, and it must end with a letter or number and be less then 260 characters long", k)) + } + + return warnings, errors } func ValidateServiceBusAuthorizationRuleName() schema.SchemaValidateFunc { diff --git a/azurerm/helpers/azure/servicebus_test.go b/azurerm/helpers/azure/servicebus_test.go new file mode 100644 index 000000000000..9326079bea21 --- /dev/null +++ b/azurerm/helpers/azure/servicebus_test.go @@ -0,0 +1,64 @@ +package azure + +import ( + "strings" + "testing" +) + +func TestDataBoxJobName(t *testing.T) { + testData := []struct { + input string + expected bool + }{ + { + input: "", + expected: false, + }, + { + input: "a", + expected: true, + }, + { + input: "_a", + expected: false, + }, + { + input: "a-", + expected: false, + }, + { + input: "12345", + expected: true, + }, + { + input: "1", + expected: true, + }, + { + input: "malcolm-in-the-middle", + expected: true, + }, + { + input: strings.Repeat("w", 259), + expected: true, + }, + { + input: strings.Repeat("w", 260), + expected: true, + }, + { + input: strings.Repeat("w", 261), + expected: false, + }, + } + + for _, v := range testData { + t.Logf("[DEBUG] Testing %q..", v.input) + + _, errors := ValidateServiceBusTopicName(v.input, "name") + actual := len(errors) == 0 + if v.expected != actual { + t.Fatalf("Expected %t but got %t", v.expected, actual) + } + } +} diff --git a/azurerm/internal/services/servicebus/resource_arm_servicebus_topic.go b/azurerm/internal/services/servicebus/resource_arm_servicebus_topic.go index 9d062a41468b..af56f314dd6e 100644 --- a/azurerm/internal/services/servicebus/resource_arm_servicebus_topic.go +++ b/azurerm/internal/services/servicebus/resource_arm_servicebus_topic.go @@ -42,7 +42,7 @@ func resourceArmServiceBusTopic() *schema.Resource { Type: schema.TypeString, Required: true, ForceNew: true, - ValidateFunc: azure.ValidateServiceBusTopicName(), + ValidateFunc: azure.ValidateServiceBusTopicName, }, "namespace_name": { From d154300842d98a83496d16768272ea099bd7ac84 Mon Sep 17 00:00:00 2001 From: v-cheye Date: Mon, 25 May 2020 17:44:20 +0800 Subject: [PATCH 4/7] Update code --- .../data_source_servicebus_topic_authorization_rule.go | 2 +- .../services/servicebus/resource_arm_servicebus_subscription.go | 2 +- .../servicebus/resource_arm_servicebus_subscription_rule.go | 2 +- .../resource_arm_servicebus_topic_authorization_rule.go | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/azurerm/internal/services/servicebus/data_source_servicebus_topic_authorization_rule.go b/azurerm/internal/services/servicebus/data_source_servicebus_topic_authorization_rule.go index 65e94313813d..e554511a32b5 100644 --- a/azurerm/internal/services/servicebus/data_source_servicebus_topic_authorization_rule.go +++ b/azurerm/internal/services/servicebus/data_source_servicebus_topic_authorization_rule.go @@ -36,7 +36,7 @@ func dataSourceArmServiceBusTopicAuthorizationRule() *schema.Resource { "topic_name": { Type: schema.TypeString, Required: true, - ValidateFunc: azure.ValidateServiceBusTopicName(), + ValidateFunc: azure.ValidateServiceBusTopicName, }, "resource_group_name": azure.SchemaResourceGroupName(), diff --git a/azurerm/internal/services/servicebus/resource_arm_servicebus_subscription.go b/azurerm/internal/services/servicebus/resource_arm_servicebus_subscription.go index 4d05e785a4d4..d0b3c82b0a91 100644 --- a/azurerm/internal/services/servicebus/resource_arm_servicebus_subscription.go +++ b/azurerm/internal/services/servicebus/resource_arm_servicebus_subscription.go @@ -52,7 +52,7 @@ func resourceArmServiceBusSubscription() *schema.Resource { Type: schema.TypeString, Required: true, ForceNew: true, - ValidateFunc: azure.ValidateServiceBusTopicName(), + ValidateFunc: azure.ValidateServiceBusTopicName, }, "resource_group_name": azure.SchemaResourceGroupName(), diff --git a/azurerm/internal/services/servicebus/resource_arm_servicebus_subscription_rule.go b/azurerm/internal/services/servicebus/resource_arm_servicebus_subscription_rule.go index ddcf0d31397a..12191a005359 100644 --- a/azurerm/internal/services/servicebus/resource_arm_servicebus_subscription_rule.go +++ b/azurerm/internal/services/servicebus/resource_arm_servicebus_subscription_rule.go @@ -57,7 +57,7 @@ func resourceArmServiceBusSubscriptionRule() *schema.Resource { Type: schema.TypeString, Required: true, ForceNew: true, - ValidateFunc: azure.ValidateServiceBusTopicName(), + ValidateFunc: azure.ValidateServiceBusTopicName, }, "subscription_name": { diff --git a/azurerm/internal/services/servicebus/resource_arm_servicebus_topic_authorization_rule.go b/azurerm/internal/services/servicebus/resource_arm_servicebus_topic_authorization_rule.go index 6bda82477890..d4746626ecd1 100644 --- a/azurerm/internal/services/servicebus/resource_arm_servicebus_topic_authorization_rule.go +++ b/azurerm/internal/services/servicebus/resource_arm_servicebus_topic_authorization_rule.go @@ -53,7 +53,7 @@ func resourceArmServiceBusTopicAuthorizationRule() *schema.Resource { Type: schema.TypeString, Required: true, ForceNew: true, - ValidateFunc: azure.ValidateServiceBusTopicName(), + ValidateFunc: azure.ValidateServiceBusTopicName, }, "resource_group_name": azure.SchemaResourceGroupName(), From afd607473c6578551cccf655266d36d6d1a535ad Mon Sep 17 00:00:00 2001 From: v-cheye Date: Mon, 25 May 2020 17:46:56 +0800 Subject: [PATCH 5/7] Update code --- azurerm/helpers/azure/servicebus_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azurerm/helpers/azure/servicebus_test.go b/azurerm/helpers/azure/servicebus_test.go index 9326079bea21..fbe7811c23f8 100644 --- a/azurerm/helpers/azure/servicebus_test.go +++ b/azurerm/helpers/azure/servicebus_test.go @@ -5,7 +5,7 @@ import ( "testing" ) -func TestDataBoxJobName(t *testing.T) { +func TestValidateServiceBusTopicName(t *testing.T) { testData := []struct { input string expected bool From 2771e20e335bad9eba126b50206cb33cdfddb088 Mon Sep 17 00:00:00 2001 From: v-cheye Date: Tue, 26 May 2020 13:52:45 +0800 Subject: [PATCH 6/7] Update code --- azurerm/helpers/azure/servicebus.go | 13 ++-- azurerm/helpers/azure/servicebus_test.go | 74 +++++++++++-------- .../resource_arm_servicebus_subscription.go | 2 +- ...source_arm_servicebus_subscription_rule.go | 2 +- .../resource_arm_servicebus_topic.go | 2 +- ...arm_servicebus_topic_authorization_rule.go | 2 +- 6 files changed, 52 insertions(+), 43 deletions(-) diff --git a/azurerm/helpers/azure/servicebus.go b/azurerm/helpers/azure/servicebus.go index b2552667d7d9..4ffd552e2434 100644 --- a/azurerm/helpers/azure/servicebus.go +++ b/azurerm/helpers/azure/servicebus.go @@ -26,14 +26,11 @@ func ValidateServiceBusSubscriptionName() schema.SchemaValidateFunc { ) } -func ValidateServiceBusTopicName(v interface{}, k string) (warnings []string, errors []error) { - value := v.(string) - - if !regexp.MustCompile(`^[a-zA-Z0-9]([-._~a-zA-Z0-9]{0,258}[a-zA-Z0-9])?$`).MatchString(value) { - errors = append(errors, fmt.Errorf("%q can contain only letters, numbers, periods, hyphens, tildas and underscores. The namespace must start with a letter or number, and it must end with a letter or number and be less then 260 characters long", k)) - } - - return warnings, errors +func ValidateServiceBusTopicName() schema.SchemaValidateFunc { + return validation.StringMatch( + regexp.MustCompile("^[a-zA-Z0-9]([-._~a-zA-Z0-9]{0,258}[a-zA-Z0-9])?$"), + "The topic name can contain only letters, numbers, periods, hyphens, tildas and underscores. The namespace must start with a letter or number, and it must end with a letter or number and be less then 260 characters long.", + ) } func ValidateServiceBusAuthorizationRuleName() schema.SchemaValidateFunc { diff --git a/azurerm/helpers/azure/servicebus_test.go b/azurerm/helpers/azure/servicebus_test.go index fbe7811c23f8..266c7445dae7 100644 --- a/azurerm/helpers/azure/servicebus_test.go +++ b/azurerm/helpers/azure/servicebus_test.go @@ -6,59 +6,71 @@ import ( ) func TestValidateServiceBusTopicName(t *testing.T) { - testData := []struct { - input string - expected bool + tests := []struct { + name string + input string + valid bool }{ { - input: "", - expected: false, + name: "Empty value", + input: "", + valid: false, }, { - input: "a", - expected: true, + name: "Invalid name with only 1 letter", + input: "a", + valid: true, }, { - input: "_a", - expected: false, + name: "Invalid name starts with underscore", + input: "_a", + valid: false, }, { - input: "a-", - expected: false, + name: "Invalid name ends with period", + input: "a.", + valid: false, }, { - input: "12345", - expected: true, + name: "Valid name with numbers", + input: "12345", + valid: true, }, { - input: "1", - expected: true, + name: "Valid name with only 1 number", + input: "1", + valid: true, }, { - input: "malcolm-in-the-middle", - expected: true, + name: "Valid name with hyphens", + input: "malcolm-in-the-middle", + valid: true, }, { - input: strings.Repeat("w", 259), - expected: true, + name: "Valid name with 259 characters", + input: strings.Repeat("w", 259), + valid: true, }, { - input: strings.Repeat("w", 260), - expected: true, + name: "Valid name with 260 characters", + input: strings.Repeat("w", 260), + valid: true, }, { - input: strings.Repeat("w", 261), - expected: false, + name: "Invalid name with 261 characters", + input: strings.Repeat("w", 261), + valid: false, }, } - for _, v := range testData { - t.Logf("[DEBUG] Testing %q..", v.input) - - _, errors := ValidateServiceBusTopicName(v.input, "name") - actual := len(errors) == 0 - if v.expected != actual { - t.Fatalf("Expected %t but got %t", v.expected, actual) - } + var validationFunction = ValidateServiceBusTopicName() + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + _, err := validationFunction(tt.input, "name") + valid := err == nil + if valid != tt.valid { + t.Errorf("Expected valid status %t but got %t for input %s", tt.valid, valid, tt.input) + } + }) } } diff --git a/azurerm/internal/services/servicebus/resource_arm_servicebus_subscription.go b/azurerm/internal/services/servicebus/resource_arm_servicebus_subscription.go index d0b3c82b0a91..4d05e785a4d4 100644 --- a/azurerm/internal/services/servicebus/resource_arm_servicebus_subscription.go +++ b/azurerm/internal/services/servicebus/resource_arm_servicebus_subscription.go @@ -52,7 +52,7 @@ func resourceArmServiceBusSubscription() *schema.Resource { Type: schema.TypeString, Required: true, ForceNew: true, - ValidateFunc: azure.ValidateServiceBusTopicName, + ValidateFunc: azure.ValidateServiceBusTopicName(), }, "resource_group_name": azure.SchemaResourceGroupName(), diff --git a/azurerm/internal/services/servicebus/resource_arm_servicebus_subscription_rule.go b/azurerm/internal/services/servicebus/resource_arm_servicebus_subscription_rule.go index 12191a005359..ddcf0d31397a 100644 --- a/azurerm/internal/services/servicebus/resource_arm_servicebus_subscription_rule.go +++ b/azurerm/internal/services/servicebus/resource_arm_servicebus_subscription_rule.go @@ -57,7 +57,7 @@ func resourceArmServiceBusSubscriptionRule() *schema.Resource { Type: schema.TypeString, Required: true, ForceNew: true, - ValidateFunc: azure.ValidateServiceBusTopicName, + ValidateFunc: azure.ValidateServiceBusTopicName(), }, "subscription_name": { diff --git a/azurerm/internal/services/servicebus/resource_arm_servicebus_topic.go b/azurerm/internal/services/servicebus/resource_arm_servicebus_topic.go index af56f314dd6e..9d062a41468b 100644 --- a/azurerm/internal/services/servicebus/resource_arm_servicebus_topic.go +++ b/azurerm/internal/services/servicebus/resource_arm_servicebus_topic.go @@ -42,7 +42,7 @@ func resourceArmServiceBusTopic() *schema.Resource { Type: schema.TypeString, Required: true, ForceNew: true, - ValidateFunc: azure.ValidateServiceBusTopicName, + ValidateFunc: azure.ValidateServiceBusTopicName(), }, "namespace_name": { diff --git a/azurerm/internal/services/servicebus/resource_arm_servicebus_topic_authorization_rule.go b/azurerm/internal/services/servicebus/resource_arm_servicebus_topic_authorization_rule.go index d4746626ecd1..6bda82477890 100644 --- a/azurerm/internal/services/servicebus/resource_arm_servicebus_topic_authorization_rule.go +++ b/azurerm/internal/services/servicebus/resource_arm_servicebus_topic_authorization_rule.go @@ -53,7 +53,7 @@ func resourceArmServiceBusTopicAuthorizationRule() *schema.Resource { Type: schema.TypeString, Required: true, ForceNew: true, - ValidateFunc: azure.ValidateServiceBusTopicName, + ValidateFunc: azure.ValidateServiceBusTopicName(), }, "resource_group_name": azure.SchemaResourceGroupName(), From de443e11cd79691a57cdc637d4fa9148ef7f916c Mon Sep 17 00:00:00 2001 From: v-cheye Date: Tue, 26 May 2020 13:53:53 +0800 Subject: [PATCH 7/7] Update code --- .../data_source_servicebus_topic_authorization_rule.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azurerm/internal/services/servicebus/data_source_servicebus_topic_authorization_rule.go b/azurerm/internal/services/servicebus/data_source_servicebus_topic_authorization_rule.go index e554511a32b5..65e94313813d 100644 --- a/azurerm/internal/services/servicebus/data_source_servicebus_topic_authorization_rule.go +++ b/azurerm/internal/services/servicebus/data_source_servicebus_topic_authorization_rule.go @@ -36,7 +36,7 @@ func dataSourceArmServiceBusTopicAuthorizationRule() *schema.Resource { "topic_name": { Type: schema.TypeString, Required: true, - ValidateFunc: azure.ValidateServiceBusTopicName, + ValidateFunc: azure.ValidateServiceBusTopicName(), }, "resource_group_name": azure.SchemaResourceGroupName(),