diff --git a/internal/services/privatedns/private_dns_zone_virtual_network_link_data_source.go b/internal/services/privatedns/private_dns_zone_virtual_network_link_data_source.go new file mode 100644 index 000000000000..41e4789b3b1b --- /dev/null +++ b/internal/services/privatedns/private_dns_zone_virtual_network_link_data_source.go @@ -0,0 +1,87 @@ +package privatedns + +import ( + "fmt" + "time" + + "github.com/hashicorp/go-azure-helpers/lang/response" + "github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema" + "github.com/hashicorp/go-azure-helpers/resourcemanager/tags" + "github.com/hashicorp/go-azure-sdk/resource-manager/privatedns/2018-09-01/virtualnetworklinks" + "github.com/hashicorp/terraform-provider-azurerm/internal/clients" + "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" + "github.com/hashicorp/terraform-provider-azurerm/internal/timeouts" +) + +func dataSourcePrivateDnsZoneVirtualNetworkLink() *pluginsdk.Resource { + return &pluginsdk.Resource{ + Read: dataSourcePrivateDnsZoneVirtualNetworkLinkRead, + + Timeouts: &pluginsdk.ResourceTimeout{ + Read: pluginsdk.DefaultTimeout(5 * time.Minute), + }, + + Schema: map[string]*pluginsdk.Schema{ + "name": { + Type: pluginsdk.TypeString, + Required: true, + }, + + // TODO: in 4.0 switch this to `private_dns_zone_id` + "private_dns_zone_name": { + Type: pluginsdk.TypeString, + Required: true, + }, + + "resource_group_name": commonschema.ResourceGroupNameForDataSource(), + + "virtual_network_id": { + Type: pluginsdk.TypeString, + Computed: true, + }, + + "registration_enabled": { + Type: pluginsdk.TypeBool, + Computed: true, + }, + + "tags": commonschema.TagsDataSource(), + }, + } +} + +func dataSourcePrivateDnsZoneVirtualNetworkLinkRead(d *pluginsdk.ResourceData, meta interface{}) error { + client := meta.(*clients.Client).PrivateDns.VirtualNetworkLinksClient + ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) + defer cancel() + + subscriptionId := meta.(*clients.Client).Account.SubscriptionId + id := virtualnetworklinks.NewVirtualNetworkLinkID(subscriptionId, d.Get("resource_group_name").(string), d.Get("private_dns_zone_name").(string), d.Get("name").(string)) + + resp, err := client.Get(ctx, id) + if err != nil { + if response.WasNotFound(resp.HttpResponse) { + return fmt.Errorf("%s was not found", id) + } + return fmt.Errorf("reading %s: %+v", id, err) + } + + d.SetId(id.ID()) + + d.Set("name", id.VirtualNetworkLinkName) + d.Set("private_dns_zone_name", id.PrivateZoneName) + d.Set("resource_group_name", id.ResourceGroupName) + + if model := resp.Model; model != nil { + if props := model.Properties; props != nil { + d.Set("registration_enabled", props.RegistrationEnabled) + + if network := props.VirtualNetwork; network != nil { + d.Set("virtual_network_id", network.Id) + } + } + return tags.FlattenAndSet(d, model.Tags) + } + + return nil +} diff --git a/internal/services/privatedns/private_dns_zone_virtual_network_link_data_source_test.go b/internal/services/privatedns/private_dns_zone_virtual_network_link_data_source_test.go new file mode 100644 index 000000000000..7af8fd561f53 --- /dev/null +++ b/internal/services/privatedns/private_dns_zone_virtual_network_link_data_source_test.go @@ -0,0 +1,47 @@ +package privatedns_test + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-provider-azurerm/internal/acceptance" + "github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check" +) + +type PrivateDnsZoneVirtualNetworkLinkDataSource struct{} + +func TestAccDataSourcePrivateDnsZoneVirtualNetworkLink_basic(t *testing.T) { + data := acceptance.BuildTestData(t, "data.azurerm_private_dns_zone_virtual_network_link", "test") + r := PrivateDnsZoneVirtualNetworkLinkDataSource{} + + resourceName := "azurerm_private_dns_zone_virtual_network_link.test" + zoneName := "azurerm_private_dns_zone.test" + vnetName := "azurerm_virtual_network.test" + + data.DataSourceTest(t, []acceptance.TestStep{ + { + Config: r.basic(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).Key("id").MatchesOtherKey(check.That(resourceName).Key("id")), + check.That(data.ResourceName).Key("name").MatchesOtherKey(check.That(resourceName).Key("name")), + check.That(data.ResourceName).Key("resource_group_name").MatchesOtherKey(check.That(resourceName).Key("resource_group_name")), + check.That(data.ResourceName).Key("virtual_network_id").MatchesOtherKey(check.That(vnetName).Key("id")), + check.That(data.ResourceName).Key("private_dns_zone_name").MatchesOtherKey(check.That(zoneName).Key("name")), + check.That(data.ResourceName).Key("registration_enabled").HasValue("false"), + check.That(data.ResourceName).Key("tags.%").HasValue("0"), + ), + }, + }) +} + +func (PrivateDnsZoneVirtualNetworkLinkDataSource) basic(data acceptance.TestData) string { + return fmt.Sprintf(` +%s + +data "azurerm_private_dns_zone_virtual_network_link" "test" { + name = azurerm_private_dns_zone_virtual_network_link.test.name + resource_group_name = azurerm_resource_group.test.name + private_dns_zone_name = azurerm_private_dns_zone.test.name +} +`, PrivateDnsZoneVirtualNetworkLinkResource{}.basic(data)) +} diff --git a/internal/services/privatedns/registration.go b/internal/services/privatedns/registration.go index 40b0b0fd4217..965a033b4f9e 100644 --- a/internal/services/privatedns/registration.go +++ b/internal/services/privatedns/registration.go @@ -25,15 +25,16 @@ func (r Registration) WebsiteCategories() []string { // SupportedDataSources returns the supported Data Sources supported by this Service func (r Registration) SupportedDataSources() map[string]*pluginsdk.Resource { return map[string]*pluginsdk.Resource{ - "azurerm_private_dns_zone": dataSourcePrivateDnsZone(), - "azurerm_private_dns_a_record": dataSourcePrivateDnsARecord(), - "azurerm_private_dns_aaaa_record": dataSourcePrivateDnsAaaaRecord(), - "azurerm_private_dns_cname_record": dataSourcePrivateDnsCNameRecord(), - "azurerm_private_dns_mx_record": dataSourcePrivateDnsMxRecord(), - "azurerm_private_dns_ptr_record": dataSourcePrivateDnsPtrRecord(), - "azurerm_private_dns_soa_record": dataSourcePrivateDnsSoaRecord(), - "azurerm_private_dns_srv_record": dataSourcePrivateDnsSrvRecord(), - "azurerm_private_dns_txt_record": dataSourcePrivateDnsTxtRecord(), + "azurerm_private_dns_zone": dataSourcePrivateDnsZone(), + "azurerm_private_dns_a_record": dataSourcePrivateDnsARecord(), + "azurerm_private_dns_aaaa_record": dataSourcePrivateDnsAaaaRecord(), + "azurerm_private_dns_cname_record": dataSourcePrivateDnsCNameRecord(), + "azurerm_private_dns_mx_record": dataSourcePrivateDnsMxRecord(), + "azurerm_private_dns_ptr_record": dataSourcePrivateDnsPtrRecord(), + "azurerm_private_dns_soa_record": dataSourcePrivateDnsSoaRecord(), + "azurerm_private_dns_srv_record": dataSourcePrivateDnsSrvRecord(), + "azurerm_private_dns_txt_record": dataSourcePrivateDnsTxtRecord(), + "azurerm_private_dns_zone_virtual_network_link": dataSourcePrivateDnsZoneVirtualNetworkLink(), } } diff --git a/website/docs/d/private_dns_zone_virtual_network_link.html.markdown b/website/docs/d/private_dns_zone_virtual_network_link.html.markdown new file mode 100644 index 000000000000..b1fa72f99c8b --- /dev/null +++ b/website/docs/d/private_dns_zone_virtual_network_link.html.markdown @@ -0,0 +1,49 @@ +--- +subcategory: "Private DNS" +layout: "azurerm" +page_title: "Azure Resource Manager: azurerm_private_dns_zone_virtual_network_link" +description: |- + Gets information about an existing Private DNS Zone Virtual Network Link. +--- + +# Data Source: azurerm_private_dns_zone_virtual_network_link + +Use this data source to access information about an existing Private DNS zone Virtual Network Link. These Links enable DNS resolution and registration inside Azure Virtual Networks using Azure Private DNS. + +## Example Usage + +```hcl +data "azurerm_private_dns_zone_virtual_network_link" "example" { + name = "test" + resource_group_name = "test-rg" + private_dns_zone_name = "test-zone" +} + +output "private_dns_a_record_id" { + value = data.azurerm_private_dns_zone_virtual_network_link.example.id +} +``` + +## Argument Reference + +* `name` - The name of the Private DNS Zone Virtual Network Link. + +* `private_dns_zone_name` - The name of the Private DNS zone (without a terminating dot). + +* `resource_group_name` - Specifies the resource group where the Private DNS Zone exists. + +## Attributes Reference + +* `id` - The ID of the Private DNS Zone Virtual Network Link. + +* `virtual_network_id` - The ID of the Virtual Network that is linked to the DNS Zone. + +* `registration_enabled` - Whether the auto-registration of virtual machine records in the virtual network in the Private DNS zone is enabled or not. + +* `tags` - A mapping of tags to assign to the resource. + +## Timeouts + +The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/language/resources/syntax#operation-timeouts) for certain actions: + +* `read` - (Defaults to 5 minutes) Used when retrieving the Private DNS Zone Virtual Network Link.