-
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 #533 from dcaro/vnet_data_source
New Data Source: `azurerm_virtual_network`
- Loading branch information
Showing
4 changed files
with
321 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,132 @@ | ||
package azurerm | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/Azure/azure-sdk-for-go/arm/network" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" | ||
) | ||
|
||
func dataSourceArmVirtualNetwork() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceArmVnetRead, | ||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
|
||
"resource_group_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
|
||
"address_spaces": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
|
||
"dns_servers": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
|
||
"subnets": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
|
||
"vnet_peerings": { | ||
Type: schema.TypeMap, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceArmVnetRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).vnetClient | ||
|
||
resGroup := d.Get("resource_group_name").(string) | ||
name := d.Get("name").(string) | ||
|
||
resp, err := client.Get(resGroup, name, "") | ||
if err != nil { | ||
if utils.ResponseWasNotFound(resp.Response) { | ||
return fmt.Errorf("Error making Read request on Azure virtual network %q (resource group %q): %+v", name, resGroup, err) | ||
} | ||
return err | ||
} | ||
|
||
d.SetId(*resp.ID) | ||
|
||
if props := resp.VirtualNetworkPropertiesFormat; props != nil { | ||
addressSpaces := flattenVnetAddressPrefixes(props.AddressSpace.AddressPrefixes) | ||
if err := d.Set("address_spaces", addressSpaces); err != nil { | ||
return err | ||
} | ||
|
||
dnsServers := flattenVnetAddressPrefixes(props.DhcpOptions.DNSServers) | ||
if err := d.Set("dns_servers", dnsServers); err != nil { | ||
return err | ||
} | ||
|
||
subnets := flattenVnetSubnetsNames(props.Subnets) | ||
if err := d.Set("subnets", subnets); err != nil { | ||
return err | ||
} | ||
|
||
vnetPeerings := flattenVnetPeerings(props.VirtualNetworkPeerings) | ||
if err := d.Set("vnet_peerings", vnetPeerings); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func flattenVnetAddressPrefixes(input *[]string) []interface{} { | ||
prefixes := make([]interface{}, 0) | ||
|
||
if myprefixes := input; myprefixes != nil { | ||
for _, prefix := range *myprefixes { | ||
prefixes = append(prefixes, prefix) | ||
} | ||
} | ||
return prefixes | ||
} | ||
|
||
func flattenVnetSubnetsNames(input *[]network.Subnet) []interface{} { | ||
subnets := make([]interface{}, 0) | ||
|
||
if mysubnets := input; mysubnets != nil { | ||
for _, subnet := range *mysubnets { | ||
subnets = append(subnets, *subnet.Name) | ||
} | ||
} | ||
return subnets | ||
} | ||
|
||
func flattenVnetPeerings(input *[]network.VirtualNetworkPeering) map[string]interface{} { | ||
output := make(map[string]interface{}, 0) | ||
|
||
if peerings := input; peerings != nil { | ||
for _, vnetpeering := range *peerings { | ||
key := *vnetpeering.Name | ||
value := *vnetpeering.RemoteVirtualNetwork.ID | ||
|
||
output[key] = value | ||
|
||
} | ||
} | ||
return output | ||
} |
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,151 @@ | ||
package azurerm | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/acctest" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
func TestAccDataSourceArmVirtualNetwork_basic(t *testing.T) { | ||
dataSourceName := "data.azurerm_virtual_network.test" | ||
ri := acctest.RandInt() | ||
|
||
name := fmt.Sprintf("acctestvnet-%d", ri) | ||
config := testAccDataSourceArmVirtualNetwork_basic(ri, testLocation()) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: config, | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr(dataSourceName, "name", name), | ||
resource.TestCheckResourceAttr(dataSourceName, "dns_servers.0", "10.0.0.4"), | ||
resource.TestCheckResourceAttr(dataSourceName, "address_spaces.0", "10.0.0.0/16"), | ||
resource.TestCheckResourceAttr(dataSourceName, "subnets.0", "subnet1"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccDataSourceArmVirtualNetwork_peering(t *testing.T) { | ||
dataSourceName := "data.azurerm_virtual_network.test" | ||
ri := acctest.RandInt() | ||
|
||
virtualNetworkName := fmt.Sprintf("acctestvnet-1-%d", ri) | ||
location := testLocation() | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourceArmVirtualNetwork_peering(ri, location), | ||
}, | ||
{ | ||
Config: testAccDataSourceArmVirtualNetwork_peeringWithDataSource(ri, location), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr(dataSourceName, "name", virtualNetworkName), | ||
resource.TestCheckResourceAttr(dataSourceName, "address_spaces.0", "10.0.1.0/24"), | ||
resource.TestCheckResourceAttr(dataSourceName, "vnet_peerings.%", "1"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccDataSourceArmVirtualNetwork_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 = "acctestvnet-%d" | ||
address_space = ["10.0.0.0/16"] | ||
location = "${azurerm_resource_group.test.location}" | ||
resource_group_name = "${azurerm_resource_group.test.name}" | ||
dns_servers = ["10.0.0.4"] | ||
subnet { | ||
name = "subnet1" | ||
address_prefix = "10.0.1.0/24" | ||
} | ||
} | ||
data "azurerm_virtual_network" "test" { | ||
resource_group_name = "${azurerm_resource_group.test.name}" | ||
name = "${azurerm_virtual_network.test.name}" | ||
} | ||
`, rInt, location, rInt) | ||
} | ||
|
||
func testAccDataSourceArmVirtualNetwork_peering(rInt int, location string) string { | ||
return fmt.Sprintf(` | ||
resource "azurerm_resource_group" "test" { | ||
name = "acctest%d-rg" | ||
location = "%s" | ||
} | ||
resource "azurerm_virtual_network" "test1" { | ||
name = "acctestvnet-1-%d" | ||
address_space = ["10.0.1.0/24"] | ||
location = "${azurerm_resource_group.test.location}" | ||
resource_group_name = "${azurerm_resource_group.test.name}" | ||
} | ||
resource "azurerm_virtual_network" "test2" { | ||
name = "acctestvnet-2-%d" | ||
address_space = ["10.0.2.0/24"] | ||
location = "${azurerm_resource_group.test.location}" | ||
resource_group_name = "${azurerm_resource_group.test.name}" | ||
} | ||
resource "azurerm_virtual_network_peering" "test1" { | ||
name = "peer-1to2" | ||
resource_group_name = "${azurerm_resource_group.test.name}" | ||
virtual_network_name = "${azurerm_virtual_network.test1.name}" | ||
remote_virtual_network_id = "${azurerm_virtual_network.test2.id}" | ||
} | ||
`, rInt, location, rInt, rInt) | ||
} | ||
|
||
func testAccDataSourceArmVirtualNetwork_peeringWithDataSource(rInt int, location string) string { | ||
return fmt.Sprintf(` | ||
resource "azurerm_resource_group" "test" { | ||
name = "acctest%d-rg" | ||
location = "%s" | ||
} | ||
resource "azurerm_virtual_network" "test1" { | ||
name = "acctestvnet-1-%d" | ||
address_space = ["10.0.1.0/24"] | ||
location = "${azurerm_resource_group.test.location}" | ||
resource_group_name = "${azurerm_resource_group.test.name}" | ||
} | ||
resource "azurerm_virtual_network" "test2" { | ||
name = "acctestvnet-2-%d" | ||
address_space = ["10.0.2.0/24"] | ||
location = "${azurerm_resource_group.test.location}" | ||
resource_group_name = "${azurerm_resource_group.test.name}" | ||
} | ||
resource "azurerm_virtual_network_peering" "test1" { | ||
name = "peer-1to2" | ||
resource_group_name = "${azurerm_resource_group.test.name}" | ||
virtual_network_name = "${azurerm_virtual_network.test1.name}" | ||
remote_virtual_network_id = "${azurerm_virtual_network.test2.id}" | ||
} | ||
data "azurerm_virtual_network" "test" { | ||
resource_group_name = "${azurerm_resource_group.test.name}" | ||
name = "${azurerm_virtual_network.test1.name}" | ||
} | ||
`, rInt, location, 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
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,37 @@ | ||
--- | ||
layout: "azurerm" | ||
page_title: "Azure Resource Manager: azurerm_virtual_network" | ||
sidebar_current: "docs-azurerm-datasource-virtual-network" | ||
description: |- | ||
Get information about the specified Virtual Network. | ||
--- | ||
|
||
# azurerm_virtual_network | ||
|
||
Use this data source to access the properties of an Azure Virtual Network. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "azurerm_virtual_network" "test" { | ||
name = "production" | ||
resource_group_name = "networking" | ||
} | ||
output "virtual_network_id" { | ||
value = "${data.azurerm_virtual_network.test.id}" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
* `name` - (Required) Specifies the name of the Virtual Network. | ||
* `resource_group_name` - (Required) Specifies the name of the resource group the Virtual Network is located in. | ||
|
||
## Attributes Reference | ||
|
||
* `id` - The ID of the virtual network. | ||
* `address_spaces` - The list of address spaces used by the virtual network. | ||
* `dns_servers` - The list of DNS servers used by the virtual network. | ||
* `subnets` - The list of name of the subnets that are attached to this virtual network. | ||
* `vnet_peerings` - A mapping of name - virtual network id of the virtual network peerings. |