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

Data source for azurerm_dev_test_virtual_network #3746

Merged
merged 4 commits into from
Aug 2, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
182 changes: 182 additions & 0 deletions azurerm/data_source_dev_test_virtual_network.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
package azurerm

import (
"fmt"

"github.com/Azure/azure-sdk-for-go/services/devtestlabs/mgmt/2016-05-15/dtl"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func dataSourceArmDevTestVirtualNetwork() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmDevTestVnetRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.NoEmptyStrings,
},

"lab_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.DevTestLabName(),
},

"resource_group_name": azure.SchemaResourceGroupNameForDataSource(),

"unique_identifier": {
Type: schema.TypeString,
Computed: true,
},

"allowed_subnets": {
Type: schema.TypeList,
Computed: true,
MaxItems: 1,
tombuildsstuff marked this conversation as resolved.
Show resolved Hide resolved
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"allow_public_ip": {
Type: schema.TypeString,
Computed: true,
},
"lab_subnet_name": {
Type: schema.TypeString,
Computed: true,
},
"resource_id": {
Type: schema.TypeString,
Computed: true,
},
},
},
},

"subnet_overrides": {
Type: schema.TypeList,
Computed: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"lab_subnet_name": {
Type: schema.TypeString,
Computed: true,
},
"resource_id": {
Type: schema.TypeString,
Computed: true,
},
"use_in_vm_creation_permission": {
Type: schema.TypeString,
Computed: true,
},
"use_public_ip_address_permission": {
Type: schema.TypeString,
Computed: true,
},
"virtual_network_pool_name": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}

func dataSourceArmDevTestVnetRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).devTestLabs.VirtualNetworksClient
ctx := meta.(*ArmClient).StopContext

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

resp, err := client.Get(ctx, resGroup, labName, name, "")
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Error: Virtual Network %q in Dev Test Lab %q (Resource Group %q) was not found", name, labName, resGroup)
}

return fmt.Errorf("Error making Read request on Virtual Network %q in Dev Test Lab %q (Resource Group %q): %+v", name, labName, resGroup, err)
}

if resp.ID == nil || *resp.ID == "" {
return fmt.Errorf("API returns a nil/empty id on Virtual Network %q in Dev Test Lab %q (Resource Group %q): %+v", name, labName, resGroup, err)
}
d.SetId(*resp.ID)

if props := resp.VirtualNetworkProperties; props != nil {
if as := props.AllowedSubnets; as != nil {
if err := d.Set("allowed_subnets", flattenDevTestVirtualNetworkAllowedSubnets(as)); err != nil {
return fmt.Errorf("error setting `allowed_subnets`: %v", err)
}
}
if so := props.SubnetOverrides; so != nil {
if err := d.Set("subnet_overrides", flattenDevTestVirtualNetworkSubnetOverrides(so)); err != nil {
return fmt.Errorf("error setting `subnet_overrides`: %v", err)
}
}
d.Set("unique_identifier", props.UniqueIdentifier)
}
return nil
}

func flattenDevTestVirtualNetworkAllowedSubnets(input *[]dtl.Subnet) []interface{} {
result := make([]interface{}, 0)

if input == nil {
return result
}

for _, v := range *input {
allowedSubnet := make(map[string]interface{})

allowedSubnet["allow_public_ip"] = string(v.AllowPublicIP)

if resourceID := v.ResourceID; resourceID != nil {
allowedSubnet["resource_id"] = *resourceID
}

if labSubnetName := v.LabSubnetName; labSubnetName != nil {
allowedSubnet["lab_subnet_name"] = *labSubnetName
}

result = append(result, allowedSubnet)
}

return result
}

func flattenDevTestVirtualNetworkSubnetOverrides(input *[]dtl.SubnetOverride) []interface{} {
result := make([]interface{}, 0)

if input == nil {
return result
}

for _, v := range *input {
subnetOverride := make(map[string]interface{})
if v.LabSubnetName != nil {
subnetOverride["lab_subnet_name"] = *v.LabSubnetName
}
if v.ResourceID != nil {
subnetOverride["resource_id"] = *v.ResourceID
}

subnetOverride["use_public_ip_address_permission"] = string(v.UsePublicIPAddressPermission)
subnetOverride["use_in_vm_creation_permission"] = string(v.UseInVMCreationPermission)

if v.VirtualNetworkPoolName != nil {
subnetOverride["virtual_network_pool_name"] = *v.VirtualNetworkPoolName
}

result = append(result, subnetOverride)
}

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

import (
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform/helper/resource"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
)

func TestAccDataSourceArmDevTestVirtualNetwork_basic(t *testing.T) {
dataSourceName := "data.azurerm_dev_test_virtual_network.test"
ri := tf.AccRandTimeInt()

name := fmt.Sprintf("acctestdtvn%d", ri)
labName := fmt.Sprintf("acctestdtl%d", ri)
resGroup := fmt.Sprintf("acctestRG-%d", ri)
subnetName := name + "Subnet"
subnetResourceID := fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/virtualNetworks/%s/subnets/%s", os.Getenv("ARM_SUBSCRIPTION_ID"), resGroup, name, subnetName)

config := testAccDataSourceArmDevTestVirtualNetwork_basic(ri, testLocation())

resource.ParallelTest(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, "lab_name", labName),
resource.TestCheckResourceAttr(dataSourceName, "resource_group_name", resGroup),
resource.TestCheckResourceAttr(dataSourceName, "allowed_subnets.0.allow_public_ip", "Allow"),
resource.TestCheckResourceAttr(dataSourceName, "allowed_subnets.0.lab_subnet_name", subnetName),
resource.TestCheckResourceAttr(dataSourceName, "allowed_subnets.0.resource_id", subnetResourceID),
resource.TestCheckResourceAttr(dataSourceName, "subnet_overrides.0.lab_subnet_name", subnetName),
resource.TestCheckResourceAttr(dataSourceName, "subnet_overrides.0.resource_id", subnetResourceID),
resource.TestCheckResourceAttr(dataSourceName, "subnet_overrides.0.use_in_vm_creation_permission", "Allow"),
resource.TestCheckResourceAttr(dataSourceName, "subnet_overrides.0.use_public_ip_address_permission", "Allow"),
resource.TestCheckResourceAttr(dataSourceName, "subnet_overrides.0.virtual_network_pool_name", ""),
),
},
},
})
}

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

resource "azurerm_dev_test_lab" "test" {
name = "acctestdtl%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
}

resource "azurerm_dev_test_virtual_network" "test" {
name = "acctestdtvn%d"
lab_name = "${azurerm_dev_test_lab.test.name}"
resource_group_name = "${azurerm_resource_group.test.name}"

subnet {
use_public_ip_address = "Allow"
use_in_virtual_machine_creation = "Allow"
}
}

data "azurerm_dev_test_virtual_network" "test" {
name = "${azurerm_dev_test_virtual_network.test.name}"
lab_name = "${azurerm_dev_test_lab.test.name}"
resource_group_name = "${azurerm_resource_group.test.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 @@ -125,6 +125,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_cosmosdb_account": dataSourceArmCosmosDbAccount(),
"azurerm_data_lake_store": dataSourceArmDataLakeStoreAccount(),
"azurerm_dev_test_lab": dataSourceArmDevTestLab(),
"azurerm_dev_test_virtual_network": dataSourceArmDevTestVirtualNetwork(),
"azurerm_dns_zone": dataSourceArmDnsZone(),
"azurerm_eventhub_namespace": dataSourceEventHubNamespace(),
"azurerm_express_route_circuit": dataSourceArmExpressRouteCircuit(),
Expand Down
61 changes: 61 additions & 0 deletions website/docs/d/dev_test_virtual_network.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
---
layout: "azurerm"
page_title: "Azure Resource Manager: azurerm_dev_test_virtual_network"
sidebar_current: "docs-azurerm-datasource-dev-test-virtual-network"
description: |-
Gets information about an existing Dev Test Lab Virtual Network.
---

# Data Source: azurerm_dev_test_virtual_network

Use this data source to access information about an existing Dev Test Lab Virtual Network.

## Example Usage

```hcl
data "azurerm_dev_test_virtual_network" "test" {
name = "example-network"
lab_name = "examplelab"
resource_group_name = "example-resource"
}

output "lab_subnet_name" {
value = "${data.azurerm_dev_test_virtual_network.test.allowed_subnets.0.lab_subnet_name}
}
```

## Argument Reference

* `name` - (Required) Specifies the name of the Virtual Network.
* `lab_name` - (Required) Specifies the name of the Dev Test Lab.
* `resource_group_name` - (Required) Specifies the name of the resource group that contains the Virtual Network.

## Attributes Reference

* `allowed_subnets` - The list of subnets enabled for the virtual network as defined below.
* `subnet_overrides` - The list of permission overrides for the subnets as defined below.
* `unique_identifier` - The unique immutable identifier of the virtual network.

---

An `allowed_subnets` block supports the following:

* `allow_public_ip` - Indicates if this subnet allows public IP addresses. Possible values are `Allow`, `Default` and `Deny`.
tombuildsstuff marked this conversation as resolved.
Show resolved Hide resolved

* `lab_subnet_name` - The name of the subnet.

* `resource_id` - The resource identifier for the subnet.

---

An `subnets_override` block supports the following:

* `lab_subnet_name` - The name of the subnet.

* `resource_id` - The resource identifier for the subnet.

* `use_in_vm_creation_permission` - Indicates if the subnet can be used for VM creation. Possible values are `Allow`, `Default` and `Deny`.

* `use_public_ip_permission` - Indicates if the subnet can be assigned public IP addresses. Possible values are `Allow`, `Default` and `Deny`.

* `virtual_network_pool_name` - The virtual network pool associated with this subnet.