Skip to content

Commit

Permalink
Update servicebus name validation (#6607)
Browse files Browse the repository at this point in the history
  • Loading branch information
Neil Ye committed Apr 24, 2020
1 parent 232e8e2 commit ec8406a
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 18 deletions.
7 changes: 0 additions & 7 deletions azurerm/helpers/azure/servicebus.go
Expand Up @@ -12,13 +12,6 @@ import (
)

// validation
func ValidateServiceBusNamespaceName() schema.SchemaValidateFunc {
return validation.StringMatch(
regexp.MustCompile("^[a-zA-Z][-a-zA-Z0-9]{4,48}[a-zA-Z0-9]$"),
"The namespace name can contain only letters, numbers, and hyphens. The namespace must start with a letter, and it must end with a letter or number and be between 6 and 50 characters long.",
)
}

func ValidateServiceBusQueueName() schema.SchemaValidateFunc {
return validation.StringMatch(
regexp.MustCompile(`^[a-zA-Z0-9][\w-./~]{0,258}([a-zA-Z0-9])?$`),
Expand Down
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/servicebus/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
Expand All @@ -28,7 +29,7 @@ func dataSourceArmServiceBusNamespaceAuthorizationRule() *schema.Resource {
"namespace_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: azure.ValidateServiceBusNamespaceName(),
ValidateFunc: validate.ServiceBusNamespaceName,
},

"resource_group_name": azure.SchemaResourceGroupNameForDataSource(),
Expand Down
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/servicebus/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
Expand All @@ -29,7 +30,7 @@ func dataSourceArmServiceBusTopicAuthorizationRule() *schema.Resource {
"namespace_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: azure.ValidateServiceBusNamespaceName(),
ValidateFunc: validate.ServiceBusNamespaceName,
},

"topic_name": {
Expand Down
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/servicebus/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
Expand Down Expand Up @@ -46,7 +47,7 @@ func resourceArmServiceBusNamespaceAuthorizationRule() *schema.Resource {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: azure.ValidateServiceBusNamespaceName(),
ValidateFunc: validate.ServiceBusNamespaceName,
},

"resource_group_name": azure.SchemaResourceGroupName(),
Expand Down
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features"
azValidate "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/servicebus/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
Expand Down Expand Up @@ -46,7 +47,7 @@ func resourceArmServiceBusQueue() *schema.Resource {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: azure.ValidateServiceBusNamespaceName(),
ValidateFunc: azValidate.ServiceBusNamespaceName,
},

"resource_group_name": azure.SchemaResourceGroupName(),
Expand Down
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/servicebus/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
Expand Down Expand Up @@ -45,7 +46,7 @@ func resourceArmServiceBusQueueAuthorizationRule() *schema.Resource {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: azure.ValidateServiceBusNamespaceName(),
ValidateFunc: validate.ServiceBusNamespaceName,
},

"queue_name": {
Expand Down
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/servicebus/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
Expand Down Expand Up @@ -44,7 +45,7 @@ func resourceArmServiceBusSubscription() *schema.Resource {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: azure.ValidateServiceBusNamespaceName(),
ValidateFunc: validate.ServiceBusNamespaceName,
},

"topic_name": {
Expand Down
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/servicebus/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
Expand Down Expand Up @@ -49,7 +50,7 @@ func resourceArmServiceBusSubscriptionRule() *schema.Resource {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: azure.ValidateServiceBusNamespaceName(),
ValidateFunc: validate.ServiceBusNamespaceName,
},

"topic_name": {
Expand Down
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features"
azValidate "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/servicebus/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
Expand Down Expand Up @@ -48,7 +49,7 @@ func resourceArmServiceBusTopic() *schema.Resource {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: azure.ValidateServiceBusNamespaceName(),
ValidateFunc: azValidate.ServiceBusNamespaceName,
},

"resource_group_name": azure.SchemaResourceGroupName(),
Expand Down
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/servicebus/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
Expand Down Expand Up @@ -45,7 +46,7 @@ func resourceArmServiceBusTopicAuthorizationRule() *schema.Resource {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: azure.ValidateServiceBusNamespaceName(),
ValidateFunc: validate.ServiceBusNamespaceName,
},

"topic_name": {
Expand Down
15 changes: 13 additions & 2 deletions azurerm/internal/services/servicebus/validate/namespace.go
Expand Up @@ -3,6 +3,7 @@ package validate
import (
"fmt"
"regexp"
"strings"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/servicebus/parse"
)
Expand All @@ -29,10 +30,20 @@ func ServiceBusNamespaceName(i interface{}, k string) (warnings []string, errors
return
}

if matched := regexp.MustCompile("^[a-zA-Z][-a-zA-Z0-9]{0,100}[a-zA-Z0-9]$").MatchString(v); !matched {
errors = append(errors, fmt.Errorf("%q can contain only letters, numbers, and hyphens. The namespace must start with a letter, and it must end with a letter or number", k))
if matched := regexp.MustCompile("^[a-zA-Z][-a-zA-Z0-9]{4,48}[a-zA-Z0-9]$").MatchString(v); !matched {
errors = append(errors, fmt.Errorf("%q can contain only letters, numbers, and hyphens. The namespace must start with a letter, and it must end with a letter or number and be between 6 and 50 characters long", k))
return
}

// The name cannot end with "-", "-sb" or "-mgmt".
// See more details from link https://docs.microsoft.com/en-us/rest/api/servicebus/create-namespace.
illegalSuffixes := []string{"-", "-sb", "-mgmt"}
for _, illegalSuffix := range illegalSuffixes {
if strings.HasSuffix(v, illegalSuffix) {
errors = append(errors, fmt.Errorf("%q cannot end with a hyphen, -sb, or -mgmt", k))
return
}
}

return warnings, errors
}

0 comments on commit ec8406a

Please sign in to comment.