Skip to content

Commit

Permalink
New Data Source: azurerm_private_dns_resolver_virtual_network_link (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
pchanvallon authored Jan 12, 2023
1 parent 1753a4a commit 26c00cd
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package privatednsresolver

import (
"context"
"fmt"
"time"

"github.com/hashicorp/go-azure-sdk/resource-manager/dnsresolver/2022-07-01/dnsforwardingrulesets"
"github.com/hashicorp/go-azure-sdk/resource-manager/dnsresolver/2022-07-01/virtualnetworklinks"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
)

type PrivateDNSResolverVirtualNetworkLinDataSourcekModel struct {
Name string `tfschema:"name"`
DnsForwardingRulesetId string `tfschema:"dns_forwarding_ruleset_id"`
Metadata map[string]string `tfschema:"metadata"`
VirtualNetworkId string `tfschema:"virtual_network_id"`
}

type PrivateDNSResolverVirtualNetworkLinkDataSource struct{}

var _ sdk.DataSource = PrivateDNSResolverVirtualNetworkLinkDataSource{}

func (r PrivateDNSResolverVirtualNetworkLinkDataSource) ResourceType() string {
return "azurerm_private_dns_resolver_virtual_network_link"
}

func (r PrivateDNSResolverVirtualNetworkLinkDataSource) ModelObject() interface{} {
return &PrivateDNSResolverVirtualNetworkLinDataSourcekModel{}
}

func (r PrivateDNSResolverVirtualNetworkLinkDataSource) IDValidationFunc() pluginsdk.SchemaValidateFunc {
return virtualnetworklinks.ValidateVirtualNetworkLinkID
}

func (r PrivateDNSResolverVirtualNetworkLinkDataSource) Arguments() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"dns_forwarding_ruleset_id": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: dnsforwardingrulesets.ValidateDnsForwardingRulesetID,
},
}
}

func (r PrivateDNSResolverVirtualNetworkLinkDataSource) Attributes() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"virtual_network_id": {
Type: pluginsdk.TypeString,
Computed: true,
},

"metadata": {
Type: pluginsdk.TypeMap,
Computed: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
}
}

func (r PrivateDNSResolverVirtualNetworkLinkDataSource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.PrivateDnsResolver.VirtualNetworkLinksClient

var state PrivateDNSResolverVirtualNetworkLinDataSourcekModel
if err := metadata.Decode(&state); err != nil {
return fmt.Errorf("decoding: %+v", err)
}

dnsForwardingRulesetId, err := virtualnetworklinks.ParseDnsForwardingRulesetID(state.DnsForwardingRulesetId)
if err != nil {
return err
}

id := virtualnetworklinks.NewVirtualNetworkLinkID(
dnsForwardingRulesetId.SubscriptionId,
dnsForwardingRulesetId.ResourceGroupName,
dnsForwardingRulesetId.DnsForwardingRulesetName,
state.Name)
resp, err := client.Get(ctx, id)
if err != nil {
return fmt.Errorf("retrieving %s: %+v", id, err)
}

model := resp.Model
if model == nil {
return fmt.Errorf("retrieving %s: model was nil", id)
}

state.DnsForwardingRulesetId = dnsforwardingrulesets.NewDnsForwardingRulesetID(id.SubscriptionId, id.ResourceGroupName, id.DnsForwardingRulesetName).ID()
state.VirtualNetworkId = model.Properties.VirtualNetwork.Id

if model.Properties.Metadata != nil {
state.Metadata = *model.Properties.Metadata
}

metadata.SetID(id)

return metadata.Encode(&state)
},
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package privatednsresolver_test

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
)

type PrivateDNSResolverVirtualNetworkLinkDataSource struct{}

func TestAccPrivateDNSResolverVirtualNetworkLinkDataSource_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_private_dns_resolver_virtual_network_link", "test")
d := PrivateDNSResolverVirtualNetworkLinkDataSource{}

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: d.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("name").Exists(),
check.That(data.ResourceName).Key("dns_forwarding_ruleset_id").Exists(),
check.That(data.ResourceName).Key("virtual_network_id").Exists(),
),
},
})
}

func (d PrivateDNSResolverVirtualNetworkLinkDataSource) basic(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
data "azurerm_private_dns_resolver_virtual_network_link" "test" {
name = azurerm_private_dns_resolver_virtual_network_link.test.name
dns_forwarding_ruleset_id = azurerm_private_dns_resolver_dns_forwarding_ruleset.test.id
}
`, PrivateDNSResolverVirtualNetworkLinkResource{}.basic(data))
}
1 change: 1 addition & 0 deletions internal/services/privatednsresolver/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func (r Registration) DataSources() []sdk.DataSource {
PrivateDNSResolverDnsResolverDataSource{},
PrivateDNSResolverInboundEndpointDataSource{},
PrivateDNSResolverOutboundEndpointDataSource{},
PrivateDNSResolverVirtualNetworkLinkDataSource{},
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
subcategory: "Private DNS Resolver"
layout: "azurerm"
page_title: "Azure Resource Manager: azurerm_private_dns_resolver_virtual_network_link"
description: |-
Gets information about an existing Private DNS Resolver Virtual Network Link.
---

# Data Source: azurerm_private_dns_resolver_virtual_network_link

Gets information about an existing Private DNS Resolver Virtual Network Link.

## Example Usage

```hcl
data "azurerm_private_dns_resolver_virtual_network_link" "example" {
name = "example-link"
dns_forwarding_ruleset_id = "example-dns-forwarding-ruleset-id"
}
```

## Arguments Reference

The following arguments are supported:

* `name` - (Required) Name of the Private DNS Resolver Virtual Network Link.

* `dns_forwarding_ruleset_id` - (Required) ID of the Private DNS Resolver DNS Forwarding Ruleset.

## Attributes Reference

In addition to the Arguments listed above - the following Attributes are exported:

* `id` - The ID of the Private DNS Resolver Virtual Network Link.

* `virtual_network_id` - The ID of the Virtual Network that is linked to the Private DNS Resolver Virtual Network Link.

* `metadata` - The metadata attached to the Private DNS Resolver Virtual Network Link.

## 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 Resolver Virtual Network Link.

0 comments on commit 26c00cd

Please sign in to comment.