Skip to content

Commit

Permalink
Fix: Corrected regexp for eventhub name
Browse files Browse the repository at this point in the history
  • Loading branch information
fabotronix committed Aug 19, 2018
1 parent 6289f69 commit 90ac144
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 7 deletions.
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)
}
})
}
}

0 comments on commit 90ac144

Please sign in to comment.