Skip to content

Commit

Permalink
azurerm_lb: Don't assume IPv4 IP addresses (#6125)
Browse files Browse the repository at this point in the history
* Don't assume IPv4 IP address on load balancer

The azure load balancer resource's frontend_ip_configuration argument assumes
the IP address is only going to be IPv4. This removes that restriction to be
in line with the rest of the terraform code that does not have that validation.
  • Loading branch information
fabianofranz authored Mar 21, 2020
1 parent f68facf commit 8fe2f86
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 140 deletions.
37 changes: 0 additions & 37 deletions azurerm/helpers/validate/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,6 @@ import (
"regexp"
)

func IPv6Address(i interface{}, k string) (warnings []string, errors []error) {
return validateIpv6Address(i, k, false)
}

func validateIpv6Address(i interface{}, k string, allowEmpty bool) (warnings []string, errors []error) { // nolint: unparam
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 six := ip.To16(); six == nil {
errors = append(errors, fmt.Errorf("%q is not a valid IPv6 address: %q", k, v))
}

return warnings, errors
}

func CIDR(i interface{}, k string) (warnings []string, errors []error) {
cidr := i.(string)

Expand Down Expand Up @@ -67,20 +44,6 @@ func validateIpv4Address(i interface{}, k string, allowEmpty bool) (warnings []s
return warnings, errors
}

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

if _, err := net.ParseMAC(v); err != nil {
errors = append(errors, fmt.Errorf("%q is not a valid MAC address: %q (%v)", k, i, err))
}

return warnings, errors
}

func PortNumber(i interface{}, k string) (warnings []string, errors []error) {
return validatePortNumber(i, k, false)
}
Expand Down
96 changes: 0 additions & 96 deletions azurerm/helpers/validate/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,56 +43,6 @@ func TestCIDR(t *testing.T) {
}
}

func TestIPv6Address(t *testing.T) {
cases := []struct {
IP string
Errors int
}{
{
IP: "",
Errors: 1,
},
{
IP: "0.0.0.0",
Errors: 0,
},
{
IP: "not:a:real:address:1:2:3:4",
Errors: 1,
},
{
IP: "text",
Errors: 1,
},
{
IP: "::",
Errors: 0,
},
{
IP: "0:0:0:0:0:0:0:0",
Errors: 0,
},
{
IP: "2001:0db8:85a3:0:0:8a2e:0370:7334",
Errors: 0,
},
{
IP: "2001:0db8:85a3:0000:0000:8a2e:0370:7334",
Errors: 0,
},
}

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

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

func TestIPv4Address(t *testing.T) {
cases := []struct {
IP string
Expand Down Expand Up @@ -193,52 +143,6 @@ func TestIPv4AddressOrEmpty(t *testing.T) {
}
}

func TestMACAddress(t *testing.T) {
cases := []struct {
MAC string
Errors int
}{
{
MAC: "",
Errors: 1,
},
{
MAC: "text d",
Errors: 1,
},
{
MAC: "12:34:no",
Errors: 1,
},
{
MAC: "123:34:56:78:90:ab",
Errors: 1,
},
{
MAC: "12:34:56:78:90:NO",
Errors: 1,
},
{
MAC: "12:34:56:78:90:ab",
Errors: 0,
},
{
MAC: "ab:cd:ef:AB:CD:EF",
Errors: 0,
},
}

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

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

func TestPortNumber(t *testing.T) {
cases := []struct {
Port int
Expand Down
4 changes: 2 additions & 2 deletions azurerm/internal/services/dns/resource_arm_dns_aaaa_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (

"github.com/Azure/azure-sdk-for-go/services/dns/mgmt/2018-05-01/dns"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/dns/parse"
Expand Down Expand Up @@ -57,7 +57,7 @@ func resourceArmDnsAAAARecord() *schema.Resource {
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validate.IPv6Address,
ValidateFunc: validation.IsIPv6Address,
},
Set: azure.HashIPv6Address,
ConflictsWith: []string{"target_resource_id"},
Expand Down
12 changes: 7 additions & 5 deletions azurerm/internal/services/network/resource_arm_lb.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/suppress"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
Expand Down Expand Up @@ -81,10 +80,13 @@ func resourceArmLoadBalancer() *schema.Resource {
},

"private_ip_address": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validate.IPv4AddressOrEmpty,
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.Any(
validation.IsIPAddress,
validation.StringIsEmpty,
),
},

"private_ip_address_version": {
Expand Down

0 comments on commit 8fe2f86

Please sign in to comment.