-
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.
Data source for azurerm_dev_test_virtual_network (#3746)
- Loading branch information
Showing
4 changed files
with
323 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,181 @@ | ||
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, | ||
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 | ||
} |
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,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) | ||
} |
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,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`. | ||
|
||
* `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. |