diff --git a/azurerm/internal/services/privatedns/data_source_private_dns_zone.go b/azurerm/internal/services/privatedns/data_source_private_dns_zone.go new file mode 100644 index 000000000000..4310d3de0001 --- /dev/null +++ b/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) +} diff --git a/azurerm/internal/services/privatedns/registration.go b/azurerm/internal/services/privatedns/registration.go index 2cc589a51100..a51d8b99d167 100644 --- a/azurerm/internal/services/privatedns/registration.go +++ b/azurerm/internal/services/privatedns/registration.go @@ -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 diff --git a/azurerm/internal/services/privatedns/tests/data_source_private_dns_zone_test.go b/azurerm/internal/services/privatedns/tests/data_source_private_dns_zone_test.go new file mode 100644 index 000000000000..94589ba11a7b --- /dev/null +++ b/azurerm/internal/services/privatedns/tests/data_source_private_dns_zone_test.go @@ -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) +} diff --git a/website/azurerm.erb b/website/azurerm.erb index 23ac113f0ef8..43200d078419 100644 --- a/website/azurerm.erb +++ b/website/azurerm.erb @@ -422,6 +422,10 @@ azurerm_proximity_placement_group +
  • + azurerm_private_dns_zone +
  • +
  • azurerm_private_endpoint_connection
  • diff --git a/website/docs/d/private_dns_zone.html.markdown b/website/docs/d/private_dns_zone.html.markdown new file mode 100644 index 000000000000..6b9133634045 --- /dev/null +++ b/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.