-
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 #6512 from terraform-providers/d/private-dns-zone
New data source azurerm_private_dns_zone
- Loading branch information
Showing
5 changed files
with
359 additions
and
1 deletion.
There are no files selected for viewing
157 changes: 157 additions & 0 deletions
157
azurerm/internal/services/privatedns/data_source_private_dns_zone.go
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,157 @@ | ||
package privatedns | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/Azure/azure-sdk-for-go/services/privatedns/mgmt/2018-09-01/privatedns" | ||
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2018-05-01/resources" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" | ||
) | ||
|
||
func dataSourceArmPrivateDnsZone() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceArmPrivateDnsZoneRead, | ||
|
||
Timeouts: &schema.ResourceTimeout{ | ||
Read: schema.DefaultTimeout(5 * time.Minute), | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
|
||
"resource_group_name": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
|
||
"number_of_record_sets": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
|
||
"max_number_of_record_sets": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
|
||
"max_number_of_virtual_network_links": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
|
||
"max_number_of_virtual_network_links_with_registration": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
|
||
"tags": tags.SchemaDataSource(), | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceArmPrivateDnsZoneRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).PrivateDns.PrivateZonesClient | ||
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
name := d.Get("name").(string) | ||
resourceGroup := d.Get("resource_group_name").(string) | ||
|
||
var ( | ||
resp *privatedns.PrivateZone | ||
) | ||
if resourceGroup != "" { | ||
zone, err := client.Get(ctx, resourceGroup, name) | ||
if err != nil { | ||
if utils.ResponseWasNotFound(resp.Response) { | ||
return fmt.Errorf("Private DNS Zone %q (Resource Group %q) was not found", name, resourceGroup) | ||
} | ||
return fmt.Errorf("reading Private DNS Zone %q (Resource Group %q): %+v", name, resourceGroup, err) | ||
} | ||
resp = &zone | ||
} else { | ||
resourcesClient := meta.(*clients.Client).Resource.ResourcesClient | ||
|
||
zone, err := findPrivateZone(ctx, client, resourcesClient, name) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if zone == nil { | ||
return fmt.Errorf("Private DNS Zone %q was not found", name) | ||
} | ||
|
||
resp = &zone.zone | ||
resourceGroup = zone.resourceGroup | ||
} | ||
|
||
if resp.ID == nil || *resp.ID == "" { | ||
return fmt.Errorf("retrieving Private DNS Zone %q (Resource Group %q)", name, resourceGroup) | ||
} | ||
d.SetId(*resp.ID) | ||
d.Set("name", name) | ||
d.Set("resource_group_name", resourceGroup) | ||
|
||
if props := resp.PrivateZoneProperties; props != nil { | ||
d.Set("number_of_record_sets", props.NumberOfRecordSets) | ||
d.Set("max_number_of_record_sets", props.MaxNumberOfRecordSets) | ||
d.Set("max_number_of_virtual_network_links", props.MaxNumberOfVirtualNetworkLinks) | ||
d.Set("max_number_of_virtual_network_links_with_registration", props.MaxNumberOfVirtualNetworkLinksWithRegistration) | ||
} | ||
|
||
return tags.FlattenAndSet(d, resp.Tags) | ||
} | ||
|
||
type privateDnsZone struct { | ||
zone privatedns.PrivateZone | ||
resourceGroup string | ||
} | ||
|
||
func findPrivateZone(ctx context.Context, client *privatedns.PrivateZonesClient, resourcesClient *resources.Client, name string) (*privateDnsZone, error) { | ||
filter := fmt.Sprintf("resourceType eq 'Microsoft.Network/privateDnsZones' and name eq '%s'", name) | ||
privateZones, err := resourcesClient.List(ctx, filter, "", nil) | ||
|
||
if err != nil { | ||
return nil, fmt.Errorf("Error listing Private DNS Zones: %+v", err) | ||
} | ||
|
||
if len(privateZones.Values()) > 1 { | ||
return nil, fmt.Errorf("More than one Private DNS Zone found with name: %q", name) | ||
} | ||
|
||
for _, z := range privateZones.Values() { | ||
if z.ID == nil { | ||
continue | ||
} | ||
|
||
id, err := azure.ParseAzureResourceID(*z.ID) | ||
|
||
if err != nil { | ||
continue | ||
} | ||
|
||
zone, err := client.Get(ctx, id.ResourceGroup, name) | ||
|
||
if err != nil { | ||
return nil, fmt.Errorf("Error retrieving Private DNS Zone %q in resource group %q: %+v", name, id.ResourceGroup, err) | ||
} | ||
|
||
return &privateDnsZone{ | ||
zone: zone, | ||
resourceGroup: id.ResourceGroup, | ||
}, nil | ||
} | ||
|
||
return nil, fmt.Errorf("No Private DNS Zones found with name: %q", name) | ||
} |
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
147 changes: 147 additions & 0 deletions
147
azurerm/internal/services/privatedns/tests/data_source_private_dns_zone_test.go
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,147 @@ | ||
package tests | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance" | ||
) | ||
|
||
func TestAccDataSourceAzureRMPrivateDNSZone_basic(t *testing.T) { | ||
data := acceptance.BuildTestData(t, "data.azurerm_private_dns_zone", "test") | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { acceptance.PreCheck(t) }, | ||
Providers: acceptance.SupportedProviders, | ||
CheckDestroy: testCheckAzureRMPrivateDnsZoneDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourcePrivateDNSZone_basic(data), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr(data.ResourceName, "tags.%", "0"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccDataSourceAzureRMPrivateDNSZone_tags(t *testing.T) { | ||
data := acceptance.BuildTestData(t, "data.azurerm_private_dns_zone", "test") | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { acceptance.PreCheck(t) }, | ||
Providers: acceptance.SupportedProviders, | ||
CheckDestroy: testCheckAzureRMPrivateDnsZoneDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourcePrivateDNSZone_tags(data), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr(data.ResourceName, "tags.%", "1"), | ||
resource.TestCheckResourceAttr(data.ResourceName, "tags.hello", "world"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccDataSourceAzureRMPrivateDNSZone_withoutResourceGroupName(t *testing.T) { | ||
data := acceptance.BuildTestData(t, "data.azurerm_private_dns_zone", "test") | ||
resourceGroupName := fmt.Sprintf("acctestRG-%d", data.RandomInteger) | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { acceptance.PreCheck(t) }, | ||
Providers: acceptance.SupportedProviders, | ||
CheckDestroy: testCheckAzureRMPrivateDnsZoneDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourcePrivateDNSZone_onlyNamePrep(data, resourceGroupName), | ||
}, | ||
{ | ||
Config: testAccDataSourcePrivateDNSZone_onlyName(data, resourceGroupName), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr(data.ResourceName, "resource_group_name", resourceGroupName), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccDataSourcePrivateDNSZone_basic(data acceptance.TestData) string { | ||
return fmt.Sprintf(` | ||
provider "azurerm" { | ||
features {} | ||
} | ||
resource "azurerm_resource_group" "test" { | ||
name = "acctestRG-%d" | ||
location = "%s" | ||
} | ||
resource "azurerm_private_dns_zone" "test" { | ||
name = "acctestzone%d.internal" | ||
resource_group_name = azurerm_resource_group.test.name | ||
} | ||
data "azurerm_private_dns_zone" "test" { | ||
name = azurerm_private_dns_zone.test.name | ||
resource_group_name = azurerm_private_dns_zone.test.resource_group_name | ||
} | ||
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger) | ||
} | ||
|
||
func testAccDataSourcePrivateDNSZone_tags(data acceptance.TestData) string { | ||
return fmt.Sprintf(` | ||
provider "azurerm" { | ||
features {} | ||
} | ||
resource "azurerm_resource_group" "test" { | ||
name = "acctestRG-%d" | ||
location = "%s" | ||
} | ||
resource "azurerm_private_dns_zone" "test" { | ||
name = "acctestzone%d.internal" | ||
resource_group_name = azurerm_resource_group.test.name | ||
tags = { | ||
hello = "world" | ||
} | ||
} | ||
data "azurerm_private_dns_zone" "test" { | ||
name = azurerm_private_dns_zone.test.name | ||
resource_group_name = azurerm_private_dns_zone.test.resource_group_name | ||
} | ||
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger) | ||
} | ||
|
||
func testAccDataSourcePrivateDNSZone_onlyNamePrep(data acceptance.TestData, resourceGroupName string) string { | ||
return fmt.Sprintf(` | ||
provider "azurerm" { | ||
features {} | ||
} | ||
resource "azurerm_resource_group" "test" { | ||
name = "%s" | ||
location = "%s" | ||
} | ||
resource "azurerm_private_dns_zone" "test" { | ||
name = "acctestzone%d.internal" | ||
resource_group_name = azurerm_resource_group.test.name | ||
} | ||
`, resourceGroupName, data.Locations.Primary, data.RandomInteger) | ||
} | ||
|
||
func testAccDataSourcePrivateDNSZone_onlyName(data acceptance.TestData, resourceGroupName string) string { | ||
template := testAccDataSourcePrivateDNSZone_onlyNamePrep(data, resourceGroupName) | ||
return fmt.Sprintf(` | ||
%s | ||
data "azurerm_private_dns_zone" "test" { | ||
name = azurerm_private_dns_zone.test.name | ||
} | ||
`, template) | ||
} |
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,48 @@ | ||
--- | ||
subcategory: "Private DNS" | ||
layout: "azurerm" | ||
page_title: "Azure Resource Manager: azurerm_private_dns_zone" | ||
description: |- | ||
Gets information about an existing Private DNS Zone. | ||
--- | ||
|
||
# Data Source: azurerm_private_dns_zone | ||
|
||
Use this data source to access information about an existing Private DNS Zone. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "azurerm_private_dns_zone" "example" { | ||
name = "contoso.internal" | ||
resource_group_name = "contoso-dns" | ||
} | ||
output "private_dns_zone_id" { | ||
value = data.azurerm_private_dns_zone.example.id | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
* `name` - The name of the Private DNS Zone. | ||
* `resource_group_name` - (Optional) The Name of the Resource Group where the Private DNS Zone exists. | ||
If the Name of the Resource Group is not provided, the first Private DNS Zone from the list of Private | ||
DNS Zones in your subscription that matches `name` will be returned. | ||
|
||
## Attributes Reference | ||
|
||
* `id` - The ID of the Private DNS Zone. | ||
|
||
* `max_number_of_record_sets` - Maximum number of recordsets that can be created in this Private Zone. | ||
* `max_number_of_virtual_network_links` - Maximum number of Virtual Networks that can be linked to this Private Zone. | ||
* `max_number_of_virtual_network_links_with_registration` - Maximum number of Virtual Networks that can be linked to this Private Zone with registration enabled. | ||
* `number_of_record_sets` - The number of recordsets currently in the zone. | ||
* `tags` - A mapping of tags for the zone. | ||
|
||
## Timeouts | ||
|
||
The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/docs/configuration/resources.html#timeouts) for certain actions: | ||
|
||
* `read` - (Defaults to 5 minutes) Used when retrieving the Private DNS Zone. |