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

Implement private_ip_address_version for network interfaces. #2548

Merged
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
177 changes: 103 additions & 74 deletions azurerm/resource_arm_network_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func resourceArmNetworkInterface() *schema.Resource {

"subnet_id": {
Type: schema.TypeString,
Required: true,
Optional: true,
DiffSuppressFunc: suppress.CaseDifference,
ValidateFunc: azure.ValidateResourceID,
},
Expand All @@ -81,6 +81,17 @@ func resourceArmNetworkInterface() *schema.Resource {
Optional: true,
},

"private_ip_address_version": {
Type: schema.TypeString,
Optional: true,
Default: string(network.IPv4),
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
string(network.IPv4),
string(network.IPv6),
}, false),
},

"private_ip_address_allocation": {
Type: schema.TypeString,
Required: true,
Expand Down Expand Up @@ -210,6 +221,7 @@ func resourceArmNetworkInterface() *schema.Resource {
Default: false,
},

// todo consider removing this one day as it is exposed in `private_ip_addresses.0`
"private_ip_address": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -351,72 +363,70 @@ func resourceArmNetworkInterfaceRead(d *schema.ResourceData, meta interface{}) e
return fmt.Errorf("Error making Read request on Azure Network Interface %q (Resource Group %q): %+v", name, resGroup, err)
}

props := *resp.InterfacePropertiesFormat

d.Set("mac_address", props.MacAddress)
if props.IPConfigurations != nil && len(*props.IPConfigurations) > 0 {
configs := *props.IPConfigurations
d.Set("name", resp.Name)
d.Set("resource_group_name", resGroup)
if location := resp.Location; location != nil {
d.Set("location", azureRMNormalizeLocation(*location))
}

if configs[0].InterfaceIPConfigurationPropertiesFormat != nil {
privateIPAddress := configs[0].InterfaceIPConfigurationPropertiesFormat.PrivateIPAddress
d.Set("private_ip_address", *privateIPAddress)
}
if props := resp.InterfacePropertiesFormat; props != nil {

d.Set("mac_address", props.MacAddress)
addresses := make([]interface{}, 0)
for _, config := range configs {
if config.InterfaceIPConfigurationPropertiesFormat != nil {
addresses = append(addresses, *config.InterfaceIPConfigurationPropertiesFormat.PrivateIPAddress)
if configs := props.IPConfigurations; configs != nil {
for i, config := range *props.IPConfigurations {
if ipProps := config.InterfaceIPConfigurationPropertiesFormat; ipProps != nil {
if v := ipProps.PrivateIPAddress; v != nil {
if i == 0 {
d.Set("private_ip_address", *v)
}
addresses = append(addresses, *v)
}
}
}
}

if err := d.Set("private_ip_addresses", addresses); err != nil {
return err
}
}

if props.IPConfigurations != nil {
configs := flattenNetworkInterfaceIPConfigurations(props.IPConfigurations)
if err := d.Set("ip_configuration", configs); err != nil {
return fmt.Errorf("Error setting `ip_configuration`: %+v", err)
if props.IPConfigurations != nil {
configs := flattenNetworkInterfaceIPConfigurations(props.IPConfigurations)
if err := d.Set("ip_configuration", configs); err != nil {
return fmt.Errorf("Error setting `ip_configuration`: %+v", err)
}
}
}

if vm := props.VirtualMachine; vm != nil {
d.Set("virtual_machine_id", *vm.ID)
}

var appliedDNSServers []string
var dnsServers []string
if dnsSettings := props.DNSSettings; dnsSettings != nil {
if s := dnsSettings.AppliedDNSServers; s != nil {
appliedDNSServers = *s
if vm := props.VirtualMachine; vm != nil {
d.Set("virtual_machine_id", vm.ID)
}

if s := dnsSettings.DNSServers; s != nil {
dnsServers = *s
}
var appliedDNSServers []string
var dnsServers []string
if dnsSettings := props.DNSSettings; dnsSettings != nil {
if s := dnsSettings.AppliedDNSServers; s != nil {
appliedDNSServers = *s
}

d.Set("internal_fqdn", dnsSettings.InternalFqdn)
d.Set("internal_dns_name_label", dnsSettings.InternalDNSNameLabel)
}
if s := dnsSettings.DNSServers; s != nil {
dnsServers = *s
}

d.Set("applied_dns_servers", appliedDNSServers)
d.Set("dns_servers", dnsServers)
d.Set("internal_fqdn", dnsSettings.InternalFqdn)
d.Set("internal_dns_name_label", dnsSettings.InternalDNSNameLabel)
}

if nsg := props.NetworkSecurityGroup; nsg != nil {
d.Set("network_security_group_id", nsg.ID)
} else {
d.Set("network_security_group_id", "")
}
d.Set("applied_dns_servers", appliedDNSServers)
d.Set("dns_servers", dnsServers)

d.Set("name", resp.Name)
d.Set("resource_group_name", resGroup)
if location := resp.Location; location != nil {
d.Set("location", azureRMNormalizeLocation(*location))
}
if nsg := props.NetworkSecurityGroup; nsg != nil {
d.Set("network_security_group_id", nsg.ID)
} else {
d.Set("network_security_group_id", "")
}

d.Set("enable_ip_forwarding", resp.EnableIPForwarding)
d.Set("enable_accelerated_networking", resp.EnableAcceleratedNetworking)
d.Set("enable_ip_forwarding", resp.EnableIPForwarding)
d.Set("enable_accelerated_networking", resp.EnableAcceleratedNetworking)
}

flattenAndSetTags(d, resp.Tags)

Expand Down Expand Up @@ -456,18 +466,20 @@ func resourceArmNetworkInterfaceDelete(d *schema.ResourceData, meta interface{})
data := configRaw.(map[string]interface{})

subnet_id := data["subnet_id"].(string)
subnetId, err2 := parseAzureResourceID(subnet_id)
if err2 != nil {
return err2
}
subnetName := subnetId.Path["subnets"]
if !sliceContainsValue(subnetNamesToLock, subnetName) {
subnetNamesToLock = append(subnetNamesToLock, subnetName)
}
if subnet_id != "" {
subnetId, err2 := parseAzureResourceID(subnet_id)
if err2 != nil {
return err2
}
subnetName := subnetId.Path["subnets"]
if !sliceContainsValue(subnetNamesToLock, subnetName) {
subnetNamesToLock = append(subnetNamesToLock, subnetName)
}

virtualNetworkName := subnetId.Path["virtualNetworks"]
if !sliceContainsValue(virtualNetworkNamesToLock, virtualNetworkName) {
virtualNetworkNamesToLock = append(virtualNetworkNamesToLock, virtualNetworkName)
virtualNetworkName := subnetId.Path["virtualNetworks"]
if !sliceContainsValue(virtualNetworkNamesToLock, virtualNetworkName) {
virtualNetworkNamesToLock = append(virtualNetworkNamesToLock, virtualNetworkName)
}
}
}

Expand Down Expand Up @@ -497,13 +509,21 @@ func flattenNetworkInterfaceIPConfigurations(ipConfigs *[]network.InterfaceIPCon
props := ipConfig.InterfaceIPConfigurationPropertiesFormat

niIPConfig["name"] = *ipConfig.Name
niIPConfig["subnet_id"] = *props.Subnet.ID

if props.Subnet != nil && props.Subnet.ID != nil {
niIPConfig["subnet_id"] = *props.Subnet.ID
}

niIPConfig["private_ip_address_allocation"] = strings.ToLower(string(props.PrivateIPAllocationMethod))

if props.PrivateIPAllocationMethod == network.Static {
niIPConfig["private_ip_address"] = *props.PrivateIPAddress
}

if props.PrivateIPAddressVersion != "" {
niIPConfig["private_ip_address_version"] = string(props.PrivateIPAddressVersion)
}

if props.PublicIPAddress != nil {
niIPConfig["public_ip_address_id"] = *props.PublicIPAddress.ID
}
Expand Down Expand Up @@ -560,29 +580,38 @@ func expandAzureRmNetworkInterfaceIpConfigurations(d *schema.ResourceData) ([]ne

subnet_id := data["subnet_id"].(string)
private_ip_allocation_method := data["private_ip_address_allocation"].(string)
private_ip_address_version := network.IPVersion(data["private_ip_address_version"].(string))

allocationMethod := network.IPAllocationMethod(private_ip_allocation_method)
properties := network.InterfaceIPConfigurationPropertiesFormat{
Subnet: &network.Subnet{
ID: &subnet_id,
},
PrivateIPAllocationMethod: allocationMethod,
PrivateIPAddressVersion: private_ip_address_version,
}

subnetId, err := parseAzureResourceID(subnet_id)
if err != nil {
return []network.InterfaceIPConfiguration{}, nil, nil, err
if private_ip_address_version == network.IPv4 && subnet_id == "" {
return nil, nil, nil, fmt.Errorf("A Subnet ID must be specified for an IPv4 Network Interface.")
}

subnetName := subnetId.Path["subnets"]
virtualNetworkName := subnetId.Path["virtualNetworks"]
if subnet_id != "" {
properties.Subnet = &network.Subnet{
ID: &subnet_id,
}

if !sliceContainsValue(subnetNamesToLock, subnetName) {
subnetNamesToLock = append(subnetNamesToLock, subnetName)
}
subnetId, err := parseAzureResourceID(subnet_id)
if err != nil {
return []network.InterfaceIPConfiguration{}, nil, nil, err
}

subnetName := subnetId.Path["subnets"]
virtualNetworkName := subnetId.Path["virtualNetworks"]

if !sliceContainsValue(virtualNetworkNamesToLock, virtualNetworkName) {
virtualNetworkNamesToLock = append(virtualNetworkNamesToLock, virtualNetworkName)
if !sliceContainsValue(subnetNamesToLock, subnetName) {
subnetNamesToLock = append(subnetNamesToLock, subnetName)
}

if !sliceContainsValue(virtualNetworkNamesToLock, virtualNetworkName) {
virtualNetworkNamesToLock = append(virtualNetworkNamesToLock, virtualNetworkName)
}
}

if v := data["private_ip_address"].(string); v != "" {
Expand Down
89 changes: 80 additions & 9 deletions azurerm/resource_arm_network_interface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func testSweepNetworkInterfaces(region string) error {
return nil
}

func TestAccAzureRMNetworkInterface_basic(t *testing.T) {
func TestAccAzureRMNetworkInterface_disappears(t *testing.T) {
resourceName := "azurerm_network_interface.test"
rInt := acctest.RandInt()
resource.ParallelTest(t, resource.TestCase{
Expand All @@ -87,18 +87,15 @@ func TestAccAzureRMNetworkInterface_basic(t *testing.T) {
Config: testAccAzureRMNetworkInterface_basic(rInt, testLocation()),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMNetworkInterfaceExists(resourceName),
testCheckAzureRMNetworkInterfaceDisappears(resourceName),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ExpectNonEmptyPlan: true,
},
},
})
}

func TestAccAzureRMNetworkInterface_disappears(t *testing.T) {
func TestAccAzureRMNetworkInterface_basic(t *testing.T) {
resourceName := "azurerm_network_interface.test"
rInt := acctest.RandInt()
resource.ParallelTest(t, resource.TestCase{
Expand All @@ -110,9 +107,12 @@ func TestAccAzureRMNetworkInterface_disappears(t *testing.T) {
Config: testAccAzureRMNetworkInterface_basic(rInt, testLocation()),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMNetworkInterfaceExists(resourceName),
testCheckAzureRMNetworkInterfaceDisappears(resourceName),
),
ExpectNonEmptyPlan: true,
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
Expand Down Expand Up @@ -366,6 +366,27 @@ func TestAccAzureRMNetworkInterface_IPAddressesBug1286(t *testing.T) {
})
}

func TestAccAzureRMNetworkInterface_IPAddressesFeature2543(t *testing.T) {
resourceName := "azurerm_network_interface.test"
rInt := acctest.RandInt()
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMNetworkInterfaceDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMNetworkInterface_withIPv6Addresses(rInt, testLocation()),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMNetworkInterfaceExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "ip_configuration.0.private_ip_address_version", "IPv4"),
resource.TestCheckResourceAttr(resourceName, "ip_configuration.1.private_ip_address_version", "IPv6"),
resource.TestCheckResourceAttrSet(resourceName, "private_ip_address"),
),
},
},
})
}

func TestAccAzureRMNetworkInterface_bug7986(t *testing.T) {
rInt := acctest.RandInt()
resource.ParallelTest(t, resource.TestCase{
Expand Down Expand Up @@ -912,6 +933,56 @@ resource "azurerm_network_interface" "test" {
`, rInt, location, rInt, rInt, rInt)
}

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

resource "azurerm_virtual_network" "test" {
name = "acctestvn-%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 = "testsubnet"
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_public_ip" "test" {
name = "test-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
public_ip_address_allocation = "static"
domain_name_label = "${azurerm_resource_group.test.name}"
}

resource "azurerm_network_interface" "test" {
name = "acctestni-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"

ip_configuration {
name = "testconfiguration1"
subnet_id = "${azurerm_subnet.test.id}"
private_ip_address_allocation = "dynamic"
primary = true
}

ip_configuration {
name = "testconfiguration2"
private_ip_address_version = "IPv6"
private_ip_address_allocation = "dynamic"
}
}
`, rInt, location, rInt, rInt, rInt)
}

func testAccAzureRMNetworkInterface_multipleLoadBalancers(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
Expand Down
Loading