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: private_dns_zone_virtual_network_link #18045

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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))
}
19 changes: 10 additions & 9 deletions internal/services/privatedns/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.