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

New Data Source: azurerm_virtual_network #533

Merged
merged 7 commits into from
Dec 8, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
127 changes: 127 additions & 0 deletions azurerm/data_source_vnet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package azurerm

import (
"fmt"
"net/http"

"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 dataSourceArmVnet() *schema.Resource {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we rename this to dataSourceArmVirtualNetwork to match the other 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 {
vnetClient := meta.(*ArmClient).vnetClient

resGroup := d.Get("resource_group_name").(string)
name := d.Get("name").(string)

resp, err := vnetClient.Get(resGroup, name, "")
if err != nil {
if resp.StatusCode == http.StatusNotFound {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we change this to use utils.ResponseWasNotFound() to handle the connection being dropped (where this will currently crash)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sense, will do so.

d.SetId("")
}
return fmt.Errorf("Error making Read request on Azure virtual network %s: %s", name, err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we change this second formatting argument to %+v - which outputs the full exception, rather than just the friendly message (sometimes the actual error is hidden)

}

d.SetId(*resp.ID)

if props := resp.VirtualNetworkPropertiesFormat; props != nil {
address_spaces := flattenVnetAddressPrefixes(props.AddressSpace.AddressPrefixes)
if err := d.Set("address_spaces", address_spaces); err != nil {
return err
}

dns_servers := flattenVnetAddressPrefixes(props.DhcpOptions.DNSServers)
if err := d.Set("dns_servers", dns_servers); err != nil {
return err
}

subnets := flattenVnetSubnetsNames(props.Subnets)
if err := d.Set("subnets", subnets); err != nil {
return err
}

vnet_peerings := flattenVnetPeerings(props.VirtualNetworkPeerings)
if err := d.Set("vnet_peerings", vnet_peerings); err != nil {
return err
}
}
return nil
}

func flattenVnetAddressPrefixes(input *[]string) []interface{} {
prefixes := make([]interface{}, 0)

for _, prefix := range *input {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in order to handle a potential crash - can we update this to:

if prefixes := input; prefixes != nil {
  for _, prefix := range *prefixes {
    # ..
  }
}

at the moment - if input isn't returned (such as, for a resource created through an older API version) - this will crash (this check should fix that)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tombuildsstuff let me know if the change that I made here is what you are expecting.

prefixes = append(prefixes, prefix)
}
return prefixes
}

func flattenVnetSubnetsNames(input *[]network.Subnet) []interface{} {
subnets := make([]interface{}, 0)

for _, subnet := range *input {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(same here)

subnets = append(subnets, *subnet.Name)
}
return subnets
}

func flattenVnetPeerings(input *[]network.VirtualNetworkPeering) map[string]interface{} {
output := make(map[string]interface{}, 0)

for _, vnetpeering := range *input {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(same here)

key := *vnetpeering.Name
value := *vnetpeering.RemoteVirtualNetwork.ID

output[key] = value

}
return output
}
119 changes: 119 additions & 0 deletions azurerm/data_source_vnet_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package azurerm

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccDataSourceAzureRMVnet_basic(t *testing.T) {
dataSourceName := "data.azurerm_vnet.test"
ri := acctest.RandInt()

name := fmt.Sprintf("acctestvnet-%d", ri)
config := testAccDataSourceAzureRMVnet_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 TestAccDataSourceAzureRMVnet_peering(t *testing.T) {
dataSourceName := "data.azurerm_vnet.test"
ri := acctest.RandInt()

name_vnet_1 := fmt.Sprintf("acctestvnet-1-%d", ri)
config := testAccDataSourceAzureRMVnet_peering(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_vnet_1),
resource.TestCheckResourceAttr(dataSourceName, "address_spaces.0", "10.0.1.0/24"),
resource.TestCheckResourceAttr(dataSourceName, "vnet_peerings.%", "1"),
),
},
},
})
}

func testAccDataSourceAzureRMVnet_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_vnet" "test" {
resource_group_name = "${azurerm_resource_group.test.name}"
name = "${azurerm_virtual_network.test.name}"
}

`, rInt, location, rInt)
}

func testAccDataSourceAzureRMVnet_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}"
}

data "azurerm_vnet" "test" {
resource_group_name = "${azurerm_resource_group.test.name}"
name = "${azurerm_virtual_network.test1.name}"
}
`, rInt, location, rInt, rInt)
}
1 change: 1 addition & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_snapshot": dataSourceArmSnapshot(),
"azurerm_subnet": dataSourceArmSubnet(),
"azurerm_subscription": dataSourceArmSubscription(),
"azurerm_vnet": dataSourceArmVnet(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we name this azurerm_virtual_network to match the other resource?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

of course, done :)

},

ResourcesMap: map[string]*schema.Resource{
Expand Down