diff --git a/azurerm/helpers/azure/servicebus.go b/azurerm/helpers/azure/servicebus.go index 99cdfe3db0ae..4ffd552e2434 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, 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.", ) } diff --git a/azurerm/helpers/azure/servicebus_test.go b/azurerm/helpers/azure/servicebus_test.go new file mode 100644 index 000000000000..266c7445dae7 --- /dev/null +++ b/azurerm/helpers/azure/servicebus_test.go @@ -0,0 +1,76 @@ +package azure + +import ( + "strings" + "testing" +) + +func TestValidateServiceBusTopicName(t *testing.T) { + tests := []struct { + name string + input string + valid bool + }{ + { + name: "Empty value", + input: "", + valid: false, + }, + { + name: "Invalid name with only 1 letter", + input: "a", + valid: true, + }, + { + name: "Invalid name starts with underscore", + input: "_a", + valid: false, + }, + { + name: "Invalid name ends with period", + input: "a.", + valid: false, + }, + { + name: "Valid name with numbers", + input: "12345", + valid: true, + }, + { + name: "Valid name with only 1 number", + input: "1", + valid: true, + }, + { + name: "Valid name with hyphens", + input: "malcolm-in-the-middle", + valid: true, + }, + { + name: "Valid name with 259 characters", + input: strings.Repeat("w", 259), + valid: true, + }, + { + name: "Valid name with 260 characters", + input: strings.Repeat("w", 260), + valid: true, + }, + { + name: "Invalid name with 261 characters", + input: strings.Repeat("w", 261), + valid: false, + }, + } + + 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) + } + }) + } +}