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_private_dns_zone #6512

Merged
merged 3 commits into from Apr 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
157 changes: 157 additions & 0 deletions azurerm/internal/services/privatedns/data_source_private_dns_zone.go
@@ -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)
}
4 changes: 3 additions & 1 deletion azurerm/internal/services/privatedns/registration.go
Expand Up @@ -20,7 +20,9 @@ func (r Registration) WebsiteCategories() []string {

// SupportedDataSources returns the supported Data Sources supported by this Service
func (r Registration) SupportedDataSources() map[string]*schema.Resource {
return map[string]*schema.Resource{}
return map[string]*schema.Resource{
"azurerm_private_dns_zone": dataSourceArmPrivateDnsZone(),
}
}

// SupportedResources returns the supported Resources supported by this Service
Expand Down
@@ -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)
}
4 changes: 4 additions & 0 deletions website/azurerm.erb
Expand Up @@ -422,6 +422,10 @@
<a href="/docs/providers/azurerm/d/proximity_placement_group.html">azurerm_proximity_placement_group</a>
</li>

<li>
<a href="/docs/providers/azurerm/d/private_dns_zone.html">azurerm_private_dns_zone</a>
</li>

<li>
<a href="/docs/providers/azurerm/d/private_endpoint_connection.html">azurerm_private_endpoint_connection</a>
</li>
Expand Down
48 changes: 48 additions & 0 deletions website/docs/d/private_dns_zone.html.markdown
@@ -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.