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

azurerm_lb - allow private_ip_address to be set to an empty value #1481

Merged
merged 3 commits into from
Jul 3, 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: 13 additions & 1 deletion azurerm/helpers/validate/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,25 @@ import (
"net"
)

func IP4Address(i interface{}, k string) (_ []string, errors []error) {
func IPv4Address(i interface{}, k string) (_ []string, errors []error) {
return validateIpv4Address(i, k, false)
}

func IPv4AddressOrEmpty(i interface{}, k string) (_ []string, errors []error) {
return validateIpv4Address(i, k, true)
}

func validateIpv4Address(i interface{}, k string, allowEmpty bool) (_ []string, errors []error) {
v, ok := i.(string)
if !ok {
errors = append(errors, fmt.Errorf("expected type of %q to be string", k))
return
}

if v == "" && allowEmpty {
return
}

ip := net.ParseIP(v)
if four := ip.To4(); four == nil {
errors = append(errors, fmt.Errorf("%q is not a valid IP4 address: %q", k, v))
Expand Down
56 changes: 53 additions & 3 deletions azurerm/helpers/validate/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package validate

import "testing"

func TestIP4Address(t *testing.T) {
func TestIPv4Address(t *testing.T) {
cases := []struct {
IP string
Errors int
Expand Down Expand Up @@ -43,10 +43,60 @@ func TestIP4Address(t *testing.T) {

for _, tc := range cases {
t.Run(tc.IP, func(t *testing.T) {
_, errors := IP4Address(tc.IP, "test")
_, errors := IPv4Address(tc.IP, "test")

if len(errors) != tc.Errors {
t.Fatalf("Expected IP4Address to return %d error(s) not %d", len(errors), tc.Errors)
t.Fatalf("Expected IPv4Address to return %d error(s) not %d", len(errors), tc.Errors)
}
})
}
}

func TestIPv4AddressOrEmpty(t *testing.T) {
cases := []struct {
IP string
Errors int
}{
{
IP: "",
Errors: 0,
},
{
IP: "0.0.0.0",
Errors: 0,
},
{
IP: "1.2.3.no",
Errors: 1,
},
{
IP: "text",
Errors: 1,
},
{
IP: "1.2.3.4",
Errors: 0,
},
{
IP: "12.34.43.21",
Errors: 0,
},
{
IP: "100.123.199.0",
Errors: 0,
},
{
IP: "255.255.255.255",
Errors: 0,
},
}

for _, tc := range cases {
t.Run(tc.IP, func(t *testing.T) {
_, errors := IPv4AddressOrEmpty(tc.IP, "test")

if len(errors) != tc.Errors {
t.Fatalf("Expected IPv4AddressOrEmpty to return %d error(s) not %d", len(errors), tc.Errors)
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion azurerm/resource_arm_loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func resourceArmLoadBalancer() *schema.Resource {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validate.IP4Address,
ValidateFunc: validate.IPv4AddressOrEmpty,
},

"public_ip_address_id": {
Expand Down
58 changes: 58 additions & 0 deletions azurerm/resource_arm_loadbalancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,27 @@ func TestAccAzureRMLoadBalancer_tags(t *testing.T) {
})
}

func TestAccAzureRMLoadBalancer_emptyPrivateIP(t *testing.T) {
resourceName := "azurerm_lb.test"
var lb network.LoadBalancer
ri := acctest.RandInt()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMLoadBalancer_emptyIPAddress(ri, testLocation()),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMLoadBalancerExists(resourceName, &lb),
resource.TestCheckResourceAttrSet(resourceName, "frontend_ip_configuration.0.private_ip_address"),
),
},
},
})
}

func testCheckAzureRMLoadBalancerExists(name string, lb *network.LoadBalancer) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
Expand Down Expand Up @@ -407,3 +428,40 @@ resource "azurerm_lb" "test" {
}
}`, rInt, location, rInt, rInt, rInt)
}

func testAccAzureRMLoadBalancer_emptyIPAddress(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}

resource "azurerm_virtual_network" "test" {
name = "acctvn-%d"
address_space = ["10.0.0.0/16"]
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
}

resource "azurerm_subnet" "test" {
name = "acctsub-%d"
resource_group_name = "${azurerm_resource_group.test.name}"
virtual_network_name = "${azurerm_virtual_network.test.name}"
address_prefix = "10.0.2.0/24"
}

resource "azurerm_lb" "test" {
name = "acctestlb-%d"
resource_group_name = "${azurerm_resource_group.test.name}"
location = "${azurerm_resource_group.test.location}"
sku = "Basic"

frontend_ip_configuration {
name = "Internal"
private_ip_address_allocation = "Dynamic"
private_ip_address = ""
subnet_id = "${azurerm_subnet.test.id}"
}
}
`, rInt, location, rInt, rInt, rInt)
}
2 changes: 1 addition & 1 deletion azurerm/resource_arm_virtual_network_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func resourceArmVirtualNetworkGateway() *schema.Resource {
"vpn_client_configuration.0.root_certificate",
"vpn_client_configuration.0.revoked_certificate",
},
ValidateFunc: validate.IP4Address,
ValidateFunc: validate.IPv4Address,
},
"radius_server_secret": {
Type: schema.TypeString,
Expand Down