-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #854 from dcaro/data_source_network_interface
New Data Source: azurerm_network_interface
- Loading branch information
Showing
5 changed files
with
436 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,248 @@ | ||
package azurerm | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" | ||
) | ||
|
||
func dataSourceArmNetworkInterface() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceArmNetworkInterfaceRead, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
|
||
"location": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"resource_group_name": resourceGroupNameForDataSourceSchema(), | ||
|
||
"network_security_group_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"mac_address": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"virtual_machine_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"ip_configuration": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"subnet_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"private_ip_address": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"private_ip_address_allocation": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"public_ip_address_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"load_balancer_backend_address_pools_ids": { | ||
Type: schema.TypeSet, | ||
Computed: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
Set: schema.HashString, | ||
}, | ||
|
||
"load_balancer_inbound_nat_rules_ids": { | ||
Type: schema.TypeSet, | ||
Computed: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
Set: schema.HashString, | ||
}, | ||
|
||
"primary": { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
|
||
"dns_servers": { | ||
Type: schema.TypeSet, | ||
Computed: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
Set: schema.HashString, | ||
}, | ||
|
||
"internal_dns_name_label": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"applied_dns_servers": { | ||
Type: schema.TypeSet, | ||
Computed: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
Set: schema.HashString, | ||
}, | ||
|
||
"internal_fqdn": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
/** | ||
* As of 2018-01-06: AN (aka. SR-IOV) on Azure is GA on Windows and Linux. | ||
* | ||
* Refer to: https://azure.microsoft.com/en-us/blog/maximize-your-vm-s-performance-with-accelerated-networking-now-generally-available-for-both-windows-and-linux/ | ||
* | ||
* Refer to: https://docs.microsoft.com/en-us/azure/virtual-network/create-vm-accelerated-networking-cli | ||
* For details, VM configuration and caveats. | ||
*/ | ||
"enable_accelerated_networking": { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
}, | ||
|
||
"enable_ip_forwarding": { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
}, | ||
|
||
"private_ip_address": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"private_ip_addresses": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
|
||
"tags": tagsForDataSourceSchema(), | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceArmNetworkInterfaceRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).ifaceClient | ||
ctx := meta.(*ArmClient).StopContext | ||
|
||
resGroup := d.Get("resource_group_name").(string) | ||
name := d.Get("name").(string) | ||
|
||
resp, err := client.Get(ctx, resGroup, name, "") | ||
if err != nil { | ||
if utils.ResponseWasNotFound(resp.Response) { | ||
d.SetId("") | ||
return nil | ||
} | ||
return fmt.Errorf("Error making Read request on Azure Network Interface %q (Resource Group %q): %+v", name, resGroup, err) | ||
} | ||
|
||
d.SetId(*resp.ID) | ||
|
||
iface := *resp.InterfacePropertiesFormat | ||
|
||
d.Set("mac_address", iface.MacAddress) | ||
|
||
if iface.IPConfigurations != nil && len(*iface.IPConfigurations) > 0 { | ||
configs := *iface.IPConfigurations | ||
|
||
if configs[0].InterfaceIPConfigurationPropertiesFormat != nil { | ||
privateIPAddress := configs[0].InterfaceIPConfigurationPropertiesFormat.PrivateIPAddress | ||
d.Set("private_ip_address", *privateIPAddress) | ||
} | ||
|
||
addresses := make([]interface{}, 0) | ||
for _, config := range configs { | ||
if config.InterfaceIPConfigurationPropertiesFormat != nil { | ||
addresses = append(addresses, *config.InterfaceIPConfigurationPropertiesFormat.PrivateIPAddress) | ||
} | ||
} | ||
|
||
if err := d.Set("private_ip_addresses", addresses); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
if iface.IPConfigurations != nil { | ||
d.Set("ip_configuration", flattenNetworkInterfaceIPConfigurations(iface.IPConfigurations)) | ||
} | ||
|
||
if iface.VirtualMachine != nil { | ||
d.Set("virtual_machine_id", *iface.VirtualMachine.ID) | ||
} else { | ||
d.Set("virtual_machine_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 { | ||
appliedDNSServers = append(appliedDNSServers, applied) | ||
} | ||
} | ||
|
||
if iface.DNSSettings.DNSServers != nil && len(*iface.DNSSettings.DNSServers) > 0 { | ||
for _, dns := range *iface.DNSSettings.DNSServers { | ||
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) | ||
} | ||
|
||
if iface.NetworkSecurityGroup != nil { | ||
d.Set("network_security_group_id", resp.NetworkSecurityGroup.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)) | ||
d.Set("applied_dns_servers", appliedDNSServers) | ||
d.Set("dns_servers", dnsServers) | ||
d.Set("enable_ip_forwarding", resp.EnableIPForwarding) | ||
d.Set("enable_accelerated_networking", resp.EnableAcceleratedNetworking) | ||
|
||
flattenAndSetTags(d, resp.Tags) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package azurerm | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/acctest" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
func TestAccDataSourceArmVirtualNetworkInterface_basic(t *testing.T) { | ||
dataSourceName := "data.azurerm_network_interface.test" | ||
ri := acctest.RandInt() | ||
|
||
name := fmt.Sprintf("acctest-nic-%d", ri) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourceArmVirtualNetworkInterface_basic(ri, testLocation()), | ||
}, | ||
{ | ||
Config: testAccDataSourceArmVirtualNetworkInterface_withDataSource(ri, testLocation()), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr(dataSourceName, "name", name), | ||
resource.TestCheckResourceAttrSet(dataSourceName, "id"), | ||
resource.TestCheckResourceAttr(dataSourceName, "private_ip_address", "10.0.1.4"), | ||
resource.TestCheckResourceAttrSet(dataSourceName, "network_security_group_id"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccDataSourceArmVirtualNetworkInterface_basic(rInt int, location string) string { | ||
return fmt.Sprintf(` | ||
resource "azurerm_resource_group" "test" { | ||
name = "acctest-%d-rg" | ||
location = "%s" | ||
} | ||
resource "azurerm_virtual_network" "test" { | ||
name = "acctest-vn-%d" | ||
address_space = ["10.0.0.0/16"] | ||
location = "${azurerm_resource_group.test.location}" | ||
resource_group_name = "${azurerm_resource_group.test.name}" | ||
} | ||
resource "azurerm_network_security_group" "test" { | ||
name = "acctest-nsg-%d" | ||
location = "${azurerm_resource_group.test.location}" | ||
resource_group_name = "${azurerm_resource_group.test.name}" | ||
} | ||
resource "azurerm_subnet" "test" { | ||
name = "subnet1" | ||
resource_group_name = "${azurerm_resource_group.test.name}" | ||
virtual_network_name = "${azurerm_virtual_network.test.name}" | ||
address_prefix = "10.0.1.0/24" | ||
} | ||
resource "azurerm_network_interface" "test" { | ||
name = "acctest-nic-%d" | ||
location = "${azurerm_resource_group.test.location}" | ||
resource_group_name = "${azurerm_resource_group.test.name}" | ||
network_security_group_id = "${azurerm_network_security_group.test.id}" | ||
ip_configuration { | ||
name = "testconfiguration1" | ||
subnet_id = "${azurerm_subnet.test.id}" | ||
private_ip_address_allocation = "dynamic" | ||
} | ||
tags { | ||
environment = "staging" | ||
} | ||
} | ||
`, rInt, location, rInt, rInt, rInt) | ||
} | ||
|
||
func testAccDataSourceArmVirtualNetworkInterface_withDataSource(rInt int, location string) string { | ||
return fmt.Sprintf(` | ||
resource "azurerm_resource_group" "test" { | ||
name = "acctest-%d-rg" | ||
location = "%s" | ||
} | ||
resource "azurerm_virtual_network" "test" { | ||
name = "acctest-vn-%d" | ||
address_space = ["10.0.0.0/16"] | ||
location = "${azurerm_resource_group.test.location}" | ||
resource_group_name = "${azurerm_resource_group.test.name}" | ||
} | ||
resource "azurerm_network_security_group" "test" { | ||
name = "acctest-nsg-%d" | ||
location = "${azurerm_resource_group.test.location}" | ||
resource_group_name = "${azurerm_resource_group.test.name}" | ||
} | ||
resource "azurerm_subnet" "test" { | ||
name = "subnet1" | ||
resource_group_name = "${azurerm_resource_group.test.name}" | ||
virtual_network_name = "${azurerm_virtual_network.test.name}" | ||
address_prefix = "10.0.1.0/24" | ||
} | ||
resource "azurerm_network_interface" "test" { | ||
name = "acctest-nic-%d" | ||
location = "${azurerm_resource_group.test.location}" | ||
resource_group_name = "${azurerm_resource_group.test.name}" | ||
network_security_group_id = "${azurerm_network_security_group.test.id}" | ||
ip_configuration { | ||
name = "testconfiguration1" | ||
subnet_id = "${azurerm_subnet.test.id}" | ||
private_ip_address_allocation = "dynamic" | ||
} | ||
tags { | ||
environment = "staging" | ||
} | ||
} | ||
data "azurerm_network_interface" "test" { | ||
name = "acctest-nic-%d" | ||
resource_group_name = "${azurerm_resource_group.test.name}" | ||
} | ||
`, rInt, location, rInt, rInt, rInt, rInt) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.