From f8f76aa107880994d54c336c50b7729dbe3d5975 Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Tue, 13 Mar 2018 20:54:26 -0500 Subject: [PATCH] NIC: setting internal_fqdn and some other missing props regardless of if they're empty --- azurerm/resource_arm_network_interface.go | 48 ++++++++-------- .../resource_arm_network_interface_test.go | 55 +++++++++++++++++++ 2 files changed, 77 insertions(+), 26 deletions(-) diff --git a/azurerm/resource_arm_network_interface.go b/azurerm/resource_arm_network_interface.go index 7a65ea890d67..397d5e67f813 100644 --- a/azurerm/resource_arm_network_interface.go +++ b/azurerm/resource_arm_network_interface.go @@ -308,16 +308,11 @@ 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) } - iface := *resp.InterfacePropertiesFormat + props := *resp.InterfacePropertiesFormat - if iface.MacAddress != nil { - if *iface.MacAddress != "" { - d.Set("mac_address", iface.MacAddress) - } - } - - if iface.IPConfigurations != nil && len(*iface.IPConfigurations) > 0 { - configs := *iface.IPConfigurations + d.Set("mac_address", props.MacAddress) + if props.IPConfigurations != nil && len(*props.IPConfigurations) > 0 { + configs := *props.IPConfigurations if configs[0].InterfaceIPConfigurationPropertiesFormat != nil { privateIPAddress := configs[0].InterfaceIPConfigurationPropertiesFormat.PrivateIPAddress @@ -336,48 +331,49 @@ func resourceArmNetworkInterfaceRead(d *schema.ResourceData, meta interface{}) e } } - if iface.IPConfigurations != nil { - configs := flattenNetworkInterfaceIPConfigurations(iface.IPConfigurations) + 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 iface.VirtualMachine != nil { - d.Set("virtual_machine_id", *iface.VirtualMachine.ID) + if vm := props.VirtualMachine; vm != nil { + d.Set("virtual_machine_id", *vm.ID) } var appliedDNSServers []string var dnsServers []string - if iface.DNSSettings != nil { - if iface.DNSSettings.AppliedDNSServers != nil && len(*iface.DNSSettings.AppliedDNSServers) > 0 { - for _, applied := range *iface.DNSSettings.AppliedDNSServers { + if dns := props.DNSSettings; dns != nil { + if appliedServers := dns.AppliedDNSServers; appliedServers != nil && len(appliedDNSServers) > 0 { + for _, applied := range appliedDNSServers { appliedDNSServers = append(appliedDNSServers, applied) } } - if iface.DNSSettings.DNSServers != nil && len(*iface.DNSSettings.DNSServers) > 0 { - for _, dns := range *iface.DNSSettings.DNSServers { + if servers := dns.DNSServers; servers != nil && len(*servers) > 0 { + for _, dns := range *servers { dnsServers = append(dnsServers, dns) } } - if iface.DNSSettings.InternalFqdn != nil && *iface.DNSSettings.InternalFqdn != "" { - d.Set("internal_fqdn", iface.DNSSettings.InternalFqdn) - } - - d.Set("internal_dns_name_label", iface.DNSSettings.InternalDNSNameLabel) + d.Set("internal_fqdn", props.DNSSettings.InternalFqdn) + d.Set("internal_dns_name_label", props.DNSSettings.InternalDNSNameLabel) } - if iface.NetworkSecurityGroup != nil { - d.Set("network_security_group_id", resp.NetworkSecurityGroup.ID) + if nsg := props.NetworkSecurityGroup; nsg != nil { + d.Set("network_security_group_id", nsg.ID) } else { d.Set("network_security_group_id", "") } d.Set("name", resp.Name) d.Set("resource_group_name", resGroup) - d.Set("location", azureRMNormalizeLocation(*resp.Location)) + + if location := resp.Location; location != nil { + d.Set("location", azureRMNormalizeLocation(*location)) + } + d.Set("applied_dns_servers", appliedDNSServers) d.Set("dns_servers", dnsServers) d.Set("enable_ip_forwarding", resp.EnableIPForwarding) diff --git a/azurerm/resource_arm_network_interface_test.go b/azurerm/resource_arm_network_interface_test.go index 0ad116ee06fb..ef7df918e58d 100644 --- a/azurerm/resource_arm_network_interface_test.go +++ b/azurerm/resource_arm_network_interface_test.go @@ -351,6 +351,25 @@ func TestAccAzureRMNetworkInterface_applicationSecurityGroups(t *testing.T) { }) } +func TestAccAzureRMNetworkInterface_internalFQDN(t *testing.T) { + resourceName := "azurerm_network_interface.test" + rInt := acctest.RandInt() + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMNetworkInterfaceDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAzureRMNetworkInterface_internalFQDN(rInt, testLocation()), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMNetworkInterfaceExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "internal_fqdn", fmt.Sprintf("acctestnic-%d.example.com", rInt)), + ), + }, + }, + }) +} + func testCheckAzureRMNetworkInterfaceExists(name string) resource.TestCheckFunc { return func(s *terraform.State) error { // Ensure we have enough information in state to look up in API @@ -1073,3 +1092,39 @@ resource "azurerm_network_interface" "test" { } `, rInt, location, rInt, rInt, rInt) } + +func testAccAzureRMNetworkInterface_internalFQDN(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_network_interface" "test" { + name = "acctestnic-%d" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + internal_fqdn = "acctestnic-%d.example.com" + + ip_configuration { + name = "testconfiguration1" + subnet_id = "${azurerm_subnet.test.id}" + private_ip_address_allocation = "dynamic" + } +} +`, rInt, location, rInt, rInt, rInt) +}