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

Fix: Corrected regexp for eventhub name #1795

Merged
merged 1 commit into from
Aug 20, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions azurerm/helpers/azure/eventhub.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,28 @@ import (
func ValidateEventHubNamespaceName() 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, underscores 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.",
"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 ValidateEventHubName() 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.",
regexp.MustCompile("^[a-zA-Z0-9]([-._a-zA-Z0-9]{0,48}[a-zA-Z0-9])?$"),
"The event hub name can contain only letters, numbers, periods (.), hyphens (-),and underscores (_), up to 50 characters, and it must begin and end with a letter or number.",
)
}

func ValidateEventHubConsumerName() 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.",
regexp.MustCompile("^[a-zA-Z0-9]([-._a-zA-Z0-9]{0,48}[a-zA-Z0-9])?$"),
"The consumer group name can contain only letters, numbers, periods (.), hyphens (-),and underscores (_), up to 50 characters, and it must begin and end with a letter or number.",
)
}

func ValidateEventHubAuthorizationRuleName() schema.SchemaValidateFunc {
return validation.StringMatch(
regexp.MustCompile("^[a-zA-Z0-9][-._a-zA-Z0-9]{0,48}([a-zA-Z0-9])?$"),
"The name can contain only letters, numbers, periods, hyphens and underscores. The name must start and end with a letter or number and be less the 50 characters long.",
regexp.MustCompile("^[a-zA-Z0-9]([-._a-zA-Z0-9]{0,48}[a-zA-Z0-9])?$"),
"The authorization rule name can contain only letters, numbers, periods, hyphens and underscores. The name must start and end with a letter or number and be up to 50 characters long.",
)
}

Expand Down
65 changes: 65 additions & 0 deletions azurerm/helpers/azure/eventhub_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package azure

import (
"strings"
"testing"
)

func TestValidateEventHubName(t *testing.T) {
tests := []struct {
name string
input string
valid bool
}{
{
name: "Valid short name",
input: "abc",
valid: true,
},
{
name: "Valid short name",
input: "a",
valid: true,
},
{
name: "Valid name with dot",
input: "a.b",
valid: true,
},
{
name: "Just a digit",
input: "1",
valid: true,
},
{
name: "Invalid long name",
input: strings.Repeat("a", 51),
valid: false,
},
{
name: "Invalid short name",
input: ".",
valid: false,
},
{
name: "Invalid name with period at end",
input: "a.",
valid: false,
},
{
name: "empty name",
input: "",
valid: false,
},
}
var validationFunction = ValidateEventHubName()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := validationFunction(tt.input, "")
valid := err == nil
if valid != tt.valid {
t.Errorf("Expected valid status %t but got %t for input %s", tt.valid, valid, tt.input)
}
})
}
}