Skip to content

Commit

Permalink
azurerm_lb - allow private_ip_address to be set to an empty value (
Browse files Browse the repository at this point in the history
…#1481)

* Adding a new `IPv4OfEmpty` validation method.

Tests pass:

```
$ go test ./azurerm/helpers/validate/ -v
=== RUN   TestIP4Address
=== RUN   TestIP4Address/#00
=== RUN   TestIP4Address/0.0.0.0
=== RUN   TestIP4Address/1.2.3.no
=== RUN   TestIP4Address/text
=== RUN   TestIP4Address/1.2.3.4
=== RUN   TestIP4Address/12.34.43.21
=== RUN   TestIP4Address/100.123.199.0
=== RUN   TestIP4Address/255.255.255.255
--- PASS: TestIP4Address (0.00s)
    --- PASS: TestIP4Address/#00 (0.00s)
    --- PASS: TestIP4Address/0.0.0.0 (0.00s)
    --- PASS: TestIP4Address/1.2.3.no (0.00s)
    --- PASS: TestIP4Address/text (0.00s)
    --- PASS: TestIP4Address/1.2.3.4 (0.00s)
    --- PASS: TestIP4Address/12.34.43.21 (0.00s)
    --- PASS: TestIP4Address/100.123.199.0 (0.00s)
    --- PASS: TestIP4Address/255.255.255.255 (0.00s)
=== RUN   TestIPv4AddressOrEmpty
=== RUN   TestIPv4AddressOrEmpty/#00
=== RUN   TestIPv4AddressOrEmpty/0.0.0.0
=== RUN   TestIPv4AddressOrEmpty/1.2.3.no
=== RUN   TestIPv4AddressOrEmpty/text
=== RUN   TestIPv4AddressOrEmpty/1.2.3.4
=== RUN   TestIPv4AddressOrEmpty/12.34.43.21
=== RUN   TestIPv4AddressOrEmpty/100.123.199.0
=== RUN   TestIPv4AddressOrEmpty/255.255.255.255
--- PASS: TestIPv4AddressOrEmpty (0.00s)
    --- PASS: TestIPv4AddressOrEmpty/#00 (0.00s)
    --- PASS: TestIPv4AddressOrEmpty/0.0.0.0 (0.00s)
    --- PASS: TestIPv4AddressOrEmpty/1.2.3.no (0.00s)
    --- PASS: TestIPv4AddressOrEmpty/text (0.00s)
    --- PASS: TestIPv4AddressOrEmpty/1.2.3.4 (0.00s)
    --- PASS: TestIPv4AddressOrEmpty/12.34.43.21 (0.00s)
    --- PASS: TestIPv4AddressOrEmpty/100.123.199.0 (0.00s)
    --- PASS: TestIPv4AddressOrEmpty/255.255.255.255 (0.00s)
=== RUN   TestMACAddress
=== RUN   TestMACAddress/#00
=== RUN   TestMACAddress/text_d
=== RUN   TestMACAddress/12:34:no
=== RUN   TestMACAddress/123:34:56:78:90:ab
=== RUN   TestMACAddress/12:34:56:78:90:NO
=== RUN   TestMACAddress/12:34:56:78:90:ab
=== RUN   TestMACAddress/ab:cd:ef:AB:CD:EF
--- PASS: TestMACAddress (0.00s)
    --- PASS: TestMACAddress/#00 (0.00s)
    --- PASS: TestMACAddress/text_d (0.00s)
    --- PASS: TestMACAddress/12:34:no (0.00s)
    --- PASS: TestMACAddress/123:34:56:78:90:ab (0.00s)
    --- PASS: TestMACAddress/12:34:56:78:90:NO (0.00s)
    --- PASS: TestMACAddress/12:34:56:78:90:ab (0.00s)
    --- PASS: TestMACAddress/ab:cd:ef:AB:CD:EF (0.00s)
=== RUN   TestRFC3339Time
=== RUN   TestRFC3339Time/#00
=== RUN   TestRFC3339Time/this_is_not_a_date
=== RUN   TestRFC3339Time/2000-01-01
=== RUN   TestRFC3339Time/2000-01-01T01:23:45
=== RUN   TestRFC3339Time/2000-01-01T01:23:45Z
=== RUN   TestRFC3339Time/2000-01-01T01:23:45+00:00
--- PASS: TestRFC3339Time (0.00s)
    --- PASS: TestRFC3339Time/#00 (0.00s)
    --- PASS: TestRFC3339Time/this_is_not_a_date (0.00s)
    --- PASS: TestRFC3339Time/2000-01-01 (0.00s)
    --- PASS: TestRFC3339Time/2000-01-01T01:23:45 (0.00s)
    --- PASS: TestRFC3339Time/2000-01-01T01:23:45Z (0.00s)
    --- PASS: TestRFC3339Time/2000-01-01T01:23:45+00:00 (0.00s)
=== RUN   TestRfc3339DateInFutureBy
=== RUN   TestRfc3339DateInFutureBy/empty
=== RUN   TestRfc3339DateInFutureBy/not_a_time
=== RUN   TestRfc3339DateInFutureBy/now_is_not_1_hour_ahead
=== RUN   TestRfc3339DateInFutureBy/now_+_7_hours_is_not_1_hour_ahead
=== RUN   TestRfc3339DateInFutureBy/now_+_7_min_is_7_min_ahead
=== RUN   TestRfc3339DateInFutureBy/now_+_8_min_is_at_least_7_min_ahead
--- PASS: TestRfc3339DateInFutureBy (0.00s)
    --- PASS: TestRfc3339DateInFutureBy/empty (0.00s)
    --- PASS: TestRfc3339DateInFutureBy/not_a_time (0.00s)
    --- PASS: TestRfc3339DateInFutureBy/now_is_not_1_hour_ahead (0.00s)
    --- PASS: TestRfc3339DateInFutureBy/now_+_7_hours_is_not_1_hour_ahead (0.00s)
    --- PASS: TestRfc3339DateInFutureBy/now_+_7_min_is_7_min_ahead (0.00s)
    --- PASS: TestRfc3339DateInFutureBy/now_+_8_min_is_at_least_7_min_ahead (0.00s)
=== RUN   TestURLIsHTTPOrHTTPS
=== RUN   TestURLIsHTTPOrHTTPS/#00
=== RUN   TestURLIsHTTPOrHTTPS/this_is_not_a_url
=== RUN   TestURLIsHTTPOrHTTPS/www.example.com
=== RUN   TestURLIsHTTPOrHTTPS/ftp://www.example.com
=== RUN   TestURLIsHTTPOrHTTPS/http://www.example.com
=== RUN   TestURLIsHTTPOrHTTPS/https://www.example.com
--- PASS: TestURLIsHTTPOrHTTPS (0.00s)
    --- PASS: TestURLIsHTTPOrHTTPS/#00 (0.00s)
    --- PASS: TestURLIsHTTPOrHTTPS/this_is_not_a_url (0.00s)
    --- PASS: TestURLIsHTTPOrHTTPS/www.example.com (0.00s)
    --- PASS: TestURLIsHTTPOrHTTPS/ftp://www.example.com (0.00s)
    --- PASS: TestURLIsHTTPOrHTTPS/http://www.example.com (0.00s)
    --- PASS: TestURLIsHTTPOrHTTPS/https://www.example.com (0.00s)
=== RUN   TestUrlWithScheme
=== RUN   TestUrlWithScheme/TestUrlWithScheme
--- PASS: TestUrlWithScheme (0.00s)
    --- PASS: TestUrlWithScheme/TestUrlWithScheme (0.00s)
PASS
ok  	github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate	0.027s

```

* `IP4Address` -> `IPv4Address`

Tests pass:

```
$ go test ./azurerm/helpers/validate/ -v
=== RUN   TestIPv4Address
=== RUN   TestIPv4Address/#00
=== RUN   TestIPv4Address/0.0.0.0
=== RUN   TestIPv4Address/1.2.3.no
=== RUN   TestIPv4Address/text
=== RUN   TestIPv4Address/1.2.3.4
=== RUN   TestIPv4Address/12.34.43.21
=== RUN   TestIPv4Address/100.123.199.0
=== RUN   TestIPv4Address/255.255.255.255
--- PASS: TestIPv4Address (0.00s)
    --- PASS: TestIPv4Address/#00 (0.00s)
    --- PASS: TestIPv4Address/0.0.0.0 (0.00s)
    --- PASS: TestIPv4Address/1.2.3.no (0.00s)
    --- PASS: TestIPv4Address/text (0.00s)
    --- PASS: TestIPv4Address/1.2.3.4 (0.00s)
    --- PASS: TestIPv4Address/12.34.43.21 (0.00s)
    --- PASS: TestIPv4Address/100.123.199.0 (0.00s)
    --- PASS: TestIPv4Address/255.255.255.255 (0.00s)
=== RUN   TestIPv4AddressOrEmpty
=== RUN   TestIPv4AddressOrEmpty/#00
=== RUN   TestIPv4AddressOrEmpty/0.0.0.0
=== RUN   TestIPv4AddressOrEmpty/1.2.3.no
=== RUN   TestIPv4AddressOrEmpty/text
=== RUN   TestIPv4AddressOrEmpty/1.2.3.4
=== RUN   TestIPv4AddressOrEmpty/12.34.43.21
=== RUN   TestIPv4AddressOrEmpty/100.123.199.0
=== RUN   TestIPv4AddressOrEmpty/255.255.255.255
--- PASS: TestIPv4AddressOrEmpty (0.00s)
    --- PASS: TestIPv4AddressOrEmpty/#00 (0.00s)
    --- PASS: TestIPv4AddressOrEmpty/0.0.0.0 (0.00s)
    --- PASS: TestIPv4AddressOrEmpty/1.2.3.no (0.00s)
    --- PASS: TestIPv4AddressOrEmpty/text (0.00s)
    --- PASS: TestIPv4AddressOrEmpty/1.2.3.4 (0.00s)
    --- PASS: TestIPv4AddressOrEmpty/12.34.43.21 (0.00s)
    --- PASS: TestIPv4AddressOrEmpty/100.123.199.0 (0.00s)
    --- PASS: TestIPv4AddressOrEmpty/255.255.255.255 (0.00s)
=== RUN   TestMACAddress
=== RUN   TestMACAddress/#00
=== RUN   TestMACAddress/text_d
=== RUN   TestMACAddress/12:34:no
=== RUN   TestMACAddress/123:34:56:78:90:ab
=== RUN   TestMACAddress/12:34:56:78:90:NO
=== RUN   TestMACAddress/12:34:56:78:90:ab
=== RUN   TestMACAddress/ab:cd:ef:AB:CD:EF
--- PASS: TestMACAddress (0.00s)
    --- PASS: TestMACAddress/#00 (0.00s)
    --- PASS: TestMACAddress/text_d (0.00s)
    --- PASS: TestMACAddress/12:34:no (0.00s)
    --- PASS: TestMACAddress/123:34:56:78:90:ab (0.00s)
    --- PASS: TestMACAddress/12:34:56:78:90:NO (0.00s)
    --- PASS: TestMACAddress/12:34:56:78:90:ab (0.00s)
    --- PASS: TestMACAddress/ab:cd:ef:AB:CD:EF (0.00s)
=== RUN   TestRFC3339Time
=== RUN   TestRFC3339Time/#00
=== RUN   TestRFC3339Time/this_is_not_a_date
=== RUN   TestRFC3339Time/2000-01-01
=== RUN   TestRFC3339Time/2000-01-01T01:23:45
=== RUN   TestRFC3339Time/2000-01-01T01:23:45Z
=== RUN   TestRFC3339Time/2000-01-01T01:23:45+00:00
--- PASS: TestRFC3339Time (0.00s)
    --- PASS: TestRFC3339Time/#00 (0.00s)
    --- PASS: TestRFC3339Time/this_is_not_a_date (0.00s)
    --- PASS: TestRFC3339Time/2000-01-01 (0.00s)
    --- PASS: TestRFC3339Time/2000-01-01T01:23:45 (0.00s)
    --- PASS: TestRFC3339Time/2000-01-01T01:23:45Z (0.00s)
    --- PASS: TestRFC3339Time/2000-01-01T01:23:45+00:00 (0.00s)
=== RUN   TestRfc3339DateInFutureBy
=== RUN   TestRfc3339DateInFutureBy/empty
=== RUN   TestRfc3339DateInFutureBy/not_a_time
=== RUN   TestRfc3339DateInFutureBy/now_is_not_1_hour_ahead
=== RUN   TestRfc3339DateInFutureBy/now_+_7_hours_is_not_1_hour_ahead
=== RUN   TestRfc3339DateInFutureBy/now_+_7_min_is_7_min_ahead
=== RUN   TestRfc3339DateInFutureBy/now_+_8_min_is_at_least_7_min_ahead
--- PASS: TestRfc3339DateInFutureBy (0.00s)
    --- PASS: TestRfc3339DateInFutureBy/empty (0.00s)
    --- PASS: TestRfc3339DateInFutureBy/not_a_time (0.00s)
    --- PASS: TestRfc3339DateInFutureBy/now_is_not_1_hour_ahead (0.00s)
    --- PASS: TestRfc3339DateInFutureBy/now_+_7_hours_is_not_1_hour_ahead (0.00s)
    --- PASS: TestRfc3339DateInFutureBy/now_+_7_min_is_7_min_ahead (0.00s)
    --- PASS: TestRfc3339DateInFutureBy/now_+_8_min_is_at_least_7_min_ahead (0.00s)
=== RUN   TestURLIsHTTPOrHTTPS
=== RUN   TestURLIsHTTPOrHTTPS/#00
=== RUN   TestURLIsHTTPOrHTTPS/this_is_not_a_url
=== RUN   TestURLIsHTTPOrHTTPS/www.example.com
=== RUN   TestURLIsHTTPOrHTTPS/ftp://www.example.com
=== RUN   TestURLIsHTTPOrHTTPS/http://www.example.com
=== RUN   TestURLIsHTTPOrHTTPS/https://www.example.com
--- PASS: TestURLIsHTTPOrHTTPS (0.00s)
    --- PASS: TestURLIsHTTPOrHTTPS/#00 (0.00s)
    --- PASS: TestURLIsHTTPOrHTTPS/this_is_not_a_url (0.00s)
    --- PASS: TestURLIsHTTPOrHTTPS/www.example.com (0.00s)
    --- PASS: TestURLIsHTTPOrHTTPS/ftp://www.example.com (0.00s)
    --- PASS: TestURLIsHTTPOrHTTPS/http://www.example.com (0.00s)
    --- PASS: TestURLIsHTTPOrHTTPS/https://www.example.com (0.00s)
=== RUN   TestUrlWithScheme
=== RUN   TestUrlWithScheme/TestUrlWithScheme
--- PASS: TestUrlWithScheme (0.00s)
    --- PASS: TestUrlWithScheme/TestUrlWithScheme (0.00s)
PASS
ok  	github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate	0.028s
```

* Adding a test covering setting a blank IP address
  • Loading branch information
tombuildsstuff authored Jul 3, 2018
1 parent 9a553b5 commit 3facf9a
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 6 deletions.
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

0 comments on commit 3facf9a

Please sign in to comment.