Skip to content

Commit a7c0116

Browse files
kmsiscokatbyte
authored andcommitted
Data source for azurerm_dev_test_virtual_network (#3746)
1 parent 6415622 commit a7c0116

4 files changed

+323
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
package azurerm
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/Azure/azure-sdk-for-go/services/devtestlabs/mgmt/2016-05-15/dtl"
7+
"github.com/hashicorp/terraform/helper/schema"
8+
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
9+
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
10+
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
11+
)
12+
13+
func dataSourceArmDevTestVirtualNetwork() *schema.Resource {
14+
return &schema.Resource{
15+
Read: dataSourceArmDevTestVnetRead,
16+
Schema: map[string]*schema.Schema{
17+
"name": {
18+
Type: schema.TypeString,
19+
Required: true,
20+
ValidateFunc: validate.NoEmptyStrings,
21+
},
22+
23+
"lab_name": {
24+
Type: schema.TypeString,
25+
Required: true,
26+
ValidateFunc: validate.DevTestLabName(),
27+
},
28+
29+
"resource_group_name": azure.SchemaResourceGroupNameForDataSource(),
30+
31+
"unique_identifier": {
32+
Type: schema.TypeString,
33+
Computed: true,
34+
},
35+
36+
"allowed_subnets": {
37+
Type: schema.TypeList,
38+
Computed: true,
39+
Elem: &schema.Resource{
40+
Schema: map[string]*schema.Schema{
41+
"allow_public_ip": {
42+
Type: schema.TypeString,
43+
Computed: true,
44+
},
45+
"lab_subnet_name": {
46+
Type: schema.TypeString,
47+
Computed: true,
48+
},
49+
"resource_id": {
50+
Type: schema.TypeString,
51+
Computed: true,
52+
},
53+
},
54+
},
55+
},
56+
57+
"subnet_overrides": {
58+
Type: schema.TypeList,
59+
Computed: true,
60+
MaxItems: 1,
61+
Elem: &schema.Resource{
62+
Schema: map[string]*schema.Schema{
63+
"lab_subnet_name": {
64+
Type: schema.TypeString,
65+
Computed: true,
66+
},
67+
"resource_id": {
68+
Type: schema.TypeString,
69+
Computed: true,
70+
},
71+
"use_in_vm_creation_permission": {
72+
Type: schema.TypeString,
73+
Computed: true,
74+
},
75+
"use_public_ip_address_permission": {
76+
Type: schema.TypeString,
77+
Computed: true,
78+
},
79+
"virtual_network_pool_name": {
80+
Type: schema.TypeString,
81+
Computed: true,
82+
},
83+
},
84+
},
85+
},
86+
},
87+
}
88+
}
89+
90+
func dataSourceArmDevTestVnetRead(d *schema.ResourceData, meta interface{}) error {
91+
client := meta.(*ArmClient).devTestLabs.VirtualNetworksClient
92+
ctx := meta.(*ArmClient).StopContext
93+
94+
resGroup := d.Get("resource_group_name").(string)
95+
labName := d.Get("lab_name").(string)
96+
name := d.Get("name").(string)
97+
98+
resp, err := client.Get(ctx, resGroup, labName, name, "")
99+
if err != nil {
100+
if utils.ResponseWasNotFound(resp.Response) {
101+
return fmt.Errorf("Error: Virtual Network %q in Dev Test Lab %q (Resource Group %q) was not found", name, labName, resGroup)
102+
}
103+
104+
return fmt.Errorf("Error making Read request on Virtual Network %q in Dev Test Lab %q (Resource Group %q): %+v", name, labName, resGroup, err)
105+
}
106+
107+
if resp.ID == nil || *resp.ID == "" {
108+
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)
109+
}
110+
d.SetId(*resp.ID)
111+
112+
if props := resp.VirtualNetworkProperties; props != nil {
113+
if as := props.AllowedSubnets; as != nil {
114+
if err := d.Set("allowed_subnets", flattenDevTestVirtualNetworkAllowedSubnets(as)); err != nil {
115+
return fmt.Errorf("error setting `allowed_subnets`: %v", err)
116+
}
117+
}
118+
if so := props.SubnetOverrides; so != nil {
119+
if err := d.Set("subnet_overrides", flattenDevTestVirtualNetworkSubnetOverrides(so)); err != nil {
120+
return fmt.Errorf("error setting `subnet_overrides`: %v", err)
121+
}
122+
}
123+
d.Set("unique_identifier", props.UniqueIdentifier)
124+
}
125+
return nil
126+
}
127+
128+
func flattenDevTestVirtualNetworkAllowedSubnets(input *[]dtl.Subnet) []interface{} {
129+
result := make([]interface{}, 0)
130+
131+
if input == nil {
132+
return result
133+
}
134+
135+
for _, v := range *input {
136+
allowedSubnet := make(map[string]interface{})
137+
138+
allowedSubnet["allow_public_ip"] = string(v.AllowPublicIP)
139+
140+
if resourceID := v.ResourceID; resourceID != nil {
141+
allowedSubnet["resource_id"] = *resourceID
142+
}
143+
144+
if labSubnetName := v.LabSubnetName; labSubnetName != nil {
145+
allowedSubnet["lab_subnet_name"] = *labSubnetName
146+
}
147+
148+
result = append(result, allowedSubnet)
149+
}
150+
151+
return result
152+
}
153+
154+
func flattenDevTestVirtualNetworkSubnetOverrides(input *[]dtl.SubnetOverride) []interface{} {
155+
result := make([]interface{}, 0)
156+
157+
if input == nil {
158+
return result
159+
}
160+
161+
for _, v := range *input {
162+
subnetOverride := make(map[string]interface{})
163+
if v.LabSubnetName != nil {
164+
subnetOverride["lab_subnet_name"] = *v.LabSubnetName
165+
}
166+
if v.ResourceID != nil {
167+
subnetOverride["resource_id"] = *v.ResourceID
168+
}
169+
170+
subnetOverride["use_public_ip_address_permission"] = string(v.UsePublicIPAddressPermission)
171+
subnetOverride["use_in_vm_creation_permission"] = string(v.UseInVMCreationPermission)
172+
173+
if v.VirtualNetworkPoolName != nil {
174+
subnetOverride["virtual_network_pool_name"] = *v.VirtualNetworkPoolName
175+
}
176+
177+
result = append(result, subnetOverride)
178+
}
179+
180+
return result
181+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package azurerm
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform/helper/resource"
9+
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
10+
)
11+
12+
func TestAccDataSourceArmDevTestVirtualNetwork_basic(t *testing.T) {
13+
dataSourceName := "data.azurerm_dev_test_virtual_network.test"
14+
ri := tf.AccRandTimeInt()
15+
16+
name := fmt.Sprintf("acctestdtvn%d", ri)
17+
labName := fmt.Sprintf("acctestdtl%d", ri)
18+
resGroup := fmt.Sprintf("acctestRG-%d", ri)
19+
subnetName := name + "Subnet"
20+
subnetResourceID := fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/virtualNetworks/%s/subnets/%s", os.Getenv("ARM_SUBSCRIPTION_ID"), resGroup, name, subnetName)
21+
22+
config := testAccDataSourceArmDevTestVirtualNetwork_basic(ri, testLocation())
23+
24+
resource.ParallelTest(t, resource.TestCase{
25+
PreCheck: func() { testAccPreCheck(t) },
26+
Providers: testAccProviders,
27+
Steps: []resource.TestStep{
28+
{
29+
Config: config,
30+
Check: resource.ComposeTestCheckFunc(
31+
resource.TestCheckResourceAttr(dataSourceName, "name", name),
32+
resource.TestCheckResourceAttr(dataSourceName, "lab_name", labName),
33+
resource.TestCheckResourceAttr(dataSourceName, "resource_group_name", resGroup),
34+
resource.TestCheckResourceAttr(dataSourceName, "allowed_subnets.0.allow_public_ip", "Allow"),
35+
resource.TestCheckResourceAttr(dataSourceName, "allowed_subnets.0.lab_subnet_name", subnetName),
36+
resource.TestCheckResourceAttr(dataSourceName, "allowed_subnets.0.resource_id", subnetResourceID),
37+
resource.TestCheckResourceAttr(dataSourceName, "subnet_overrides.0.lab_subnet_name", subnetName),
38+
resource.TestCheckResourceAttr(dataSourceName, "subnet_overrides.0.resource_id", subnetResourceID),
39+
resource.TestCheckResourceAttr(dataSourceName, "subnet_overrides.0.use_in_vm_creation_permission", "Allow"),
40+
resource.TestCheckResourceAttr(dataSourceName, "subnet_overrides.0.use_public_ip_address_permission", "Allow"),
41+
resource.TestCheckResourceAttr(dataSourceName, "subnet_overrides.0.virtual_network_pool_name", ""),
42+
),
43+
},
44+
},
45+
})
46+
}
47+
48+
func testAccDataSourceArmDevTestVirtualNetwork_basic(rInt int, location string) string {
49+
return fmt.Sprintf(`
50+
resource "azurerm_resource_group" "test" {
51+
name = "acctestRG-%d"
52+
location = "%s"
53+
}
54+
55+
resource "azurerm_dev_test_lab" "test" {
56+
name = "acctestdtl%d"
57+
location = "${azurerm_resource_group.test.location}"
58+
resource_group_name = "${azurerm_resource_group.test.name}"
59+
}
60+
61+
resource "azurerm_dev_test_virtual_network" "test" {
62+
name = "acctestdtvn%d"
63+
lab_name = "${azurerm_dev_test_lab.test.name}"
64+
resource_group_name = "${azurerm_resource_group.test.name}"
65+
66+
subnet {
67+
use_public_ip_address = "Allow"
68+
use_in_virtual_machine_creation = "Allow"
69+
}
70+
}
71+
72+
data "azurerm_dev_test_virtual_network" "test" {
73+
name = "${azurerm_dev_test_virtual_network.test.name}"
74+
lab_name = "${azurerm_dev_test_lab.test.name}"
75+
resource_group_name = "${azurerm_resource_group.test.name}"
76+
}
77+
78+
79+
`, rInt, location, rInt, rInt)
80+
}

azurerm/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ func Provider() terraform.ResourceProvider {
125125
"azurerm_cosmosdb_account": dataSourceArmCosmosDbAccount(),
126126
"azurerm_data_lake_store": dataSourceArmDataLakeStoreAccount(),
127127
"azurerm_dev_test_lab": dataSourceArmDevTestLab(),
128+
"azurerm_dev_test_virtual_network": dataSourceArmDevTestVirtualNetwork(),
128129
"azurerm_dns_zone": dataSourceArmDnsZone(),
129130
"azurerm_eventhub_namespace": dataSourceEventHubNamespace(),
130131
"azurerm_express_route_circuit": dataSourceArmExpressRouteCircuit(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
---
2+
layout: "azurerm"
3+
page_title: "Azure Resource Manager: azurerm_dev_test_virtual_network"
4+
sidebar_current: "docs-azurerm-datasource-dev-test-virtual-network"
5+
description: |-
6+
Gets information about an existing Dev Test Lab Virtual Network.
7+
---
8+
9+
# Data Source: azurerm_dev_test_virtual_network
10+
11+
Use this data source to access information about an existing Dev Test Lab Virtual Network.
12+
13+
## Example Usage
14+
15+
```hcl
16+
data "azurerm_dev_test_virtual_network" "test" {
17+
name = "example-network"
18+
lab_name = "examplelab"
19+
resource_group_name = "example-resource"
20+
}
21+
22+
output "lab_subnet_name" {
23+
value = "${data.azurerm_dev_test_virtual_network.test.allowed_subnets.0.lab_subnet_name}
24+
}
25+
```
26+
27+
## Argument Reference
28+
29+
* `name` - (Required) Specifies the name of the Virtual Network.
30+
* `lab_name` - (Required) Specifies the name of the Dev Test Lab.
31+
* `resource_group_name` - (Required) Specifies the name of the resource group that contains the Virtual Network.
32+
33+
## Attributes Reference
34+
35+
* `allowed_subnets` - The list of subnets enabled for the virtual network as defined below.
36+
* `subnet_overrides` - The list of permission overrides for the subnets as defined below.
37+
* `unique_identifier` - The unique immutable identifier of the virtual network.
38+
39+
---
40+
41+
An `allowed_subnets` block supports the following:
42+
43+
* `allow_public_ip` - Indicates if this subnet allows public IP addresses. Possible values are `Allow`, `Default` and `Deny`.
44+
45+
* `lab_subnet_name` - The name of the subnet.
46+
47+
* `resource_id` - The resource identifier for the subnet.
48+
49+
---
50+
51+
An `subnets_override` block supports the following:
52+
53+
* `lab_subnet_name` - The name of the subnet.
54+
55+
* `resource_id` - The resource identifier for the subnet.
56+
57+
* `use_in_vm_creation_permission` - Indicates if the subnet can be used for VM creation. Possible values are `Allow`, `Default` and `Deny`.
58+
59+
* `use_public_ip_permission` - Indicates if the subnet can be assigned public IP addresses. Possible values are `Allow`, `Default` and `Deny`.
60+
61+
* `virtual_network_pool_name` - The virtual network pool associated with this subnet.

0 commit comments

Comments
 (0)