Skip to content

Commit

Permalink
provider/azurerm: Add Validation to the Azurerm LoadBalancer Rule name
Browse files Browse the repository at this point in the history
  • Loading branch information
stack72 committed Oct 6, 2016
1 parent 79d6cc8 commit ef82f62
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 4 deletions.
38 changes: 35 additions & 3 deletions builtin/providers/azurerm/resource_arm_loadbalancer_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package azurerm
import (
"fmt"
"log"
"regexp"
"time"

"github.com/Azure/azure-sdk-for-go/arm/network"
Expand All @@ -21,9 +22,10 @@ func resourceArmLoadbalancerRule() *schema.Resource {

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateArmLoadBalancerRuleName,
},

"location": {
Expand Down Expand Up @@ -308,3 +310,33 @@ func expandAzureRmLoadbalancerRule(d *schema.ResourceData, lb *network.LoadBalan

return &lbRule, nil
}

func validateArmLoadBalancerRuleName(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if !regexp.MustCompile(`^[a-zA-Z._-]+$`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"only word characters and hyphens allowed in %q: %q",
k, value))
}

if len(value) > 80 {
errors = append(errors, fmt.Errorf(
"%q cannot be longer than 80 characters: %q", k, value))
}

if len(value) == 0 {
errors = append(errors, fmt.Errorf(
"%q cannot be an empty string: %q", k, value))
}
if !regexp.MustCompile(`[a-zA-Z]$`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q must end with a word character: %q", k, value))
}

if !regexp.MustCompile(`^[a-zA-Z]`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q must start with a word character: %q", k, value))
}

return
}
48 changes: 48 additions & 0 deletions builtin/providers/azurerm/resource_arm_loadbalancer_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,54 @@ import (
"github.com/hashicorp/terraform/terraform"
)

func TestResourceAzureRMLoadBalancerRuleNameLabel_validation(t *testing.T) {
cases := []struct {
Value string
ErrCount int
}{
{
Value: "-word",
ErrCount: 1,
},
{
Value: "testing-",
ErrCount: 1,
},
{
Value: "test123test",
ErrCount: 1,
},
{
Value: acctest.RandStringFromCharSet(81, "abcdedfed"),
ErrCount: 1,
},
{
Value: "test.rule",
ErrCount: 0,
},
{
Value: "test_rule",
ErrCount: 0,
},
{
Value: "test-rule",
ErrCount: 0,
},
{
Value: "TestRule",
ErrCount: 0,
},
}

for _, tc := range cases {
_, errors := validateArmLoadBalancerRuleName(tc.Value, "azurerm_lb_rule")

if len(errors) != tc.ErrCount {
t.Fatalf("Expected the Azure RM LoadBalancer Rule Name Label to trigger a validation error")
}
}
}

func TestAccAzureRMLoadbalancerRule_basic(t *testing.T) {
var lb network.LoadBalancer
ri := acctest.RandInt()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ resource "azurerm_lb_rule" "test" {
location = "West US"
resource_group_name = "${azurerm_resource_group.test.name}"
loadbalancer_id = "${azurerm_lb.test.id}"
name = "LB Rule"
name = "LBRule"
protocol = "Tcp"
frontend_port = 3389
backend_port = 3389
Expand Down

0 comments on commit ef82f62

Please sign in to comment.