Skip to content

Commit

Permalink
Add DS records
Browse files Browse the repository at this point in the history
  • Loading branch information
alexwilcox9 committed Dec 13, 2024
1 parent a13cb0a commit 57deeb6
Show file tree
Hide file tree
Showing 7 changed files with 974 additions and 2 deletions.
145 changes: 145 additions & 0 deletions internal/services/dns/dns_ds_record_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package dns

import (
"context"
"fmt"
"time"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-sdk/resource-manager/dns/2023-07-01-preview/recordsets"
"github.com/hashicorp/go-azure-sdk/resource-manager/dns/2023-07-01-preview/zones"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/dns/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
)

var _ sdk.DataSource = DnsDSRecordDataResource{}

type DnsDSRecordDataResource struct{}

func (DnsDSRecordDataResource) ModelObject() interface{} {
return &DnsDSRecordDataSourceModel{}
}

func (d DnsDSRecordDataResource) IDValidationFunc() pluginsdk.SchemaValidateFunc {
return validate.ValidateRecordTypeID(recordsets.RecordTypeDS)
}

func (DnsDSRecordDataResource) ResourceType() string {
return "azurerm_dns_ds_record"
}

type DnsDSRecordDataSourceModel struct {
Name string `tfschema:"name"`
ZoneId string `tfschema:"dns_zone_id"`
Ttl int64 `tfschema:"ttl"`
Record []DnsDSRecordResourceRecord `tfschema:"record"`
Tags map[string]string `tfschema:"tags"`
Fqdn string `tfschema:"fqdn"`
}

func (DnsDSRecordDataResource) Arguments() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
},

"dns_zone_id": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: zones.ValidateDnsZoneID,
},
}
}

func (DnsDSRecordDataResource) Attributes() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"record": {
Type: pluginsdk.TypeSet,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"algorithm": {
Type: pluginsdk.TypeInt,
Computed: true,
},

"key_tag": {
Type: pluginsdk.TypeInt,
Computed: true,
},

"digest_type": {
Type: pluginsdk.TypeInt,
Computed: true,
},

"digest_value": {
Type: pluginsdk.TypeString,
Computed: true,
},
},
},
},

"ttl": {
Type: pluginsdk.TypeInt,
Computed: true,
},

"fqdn": {
Type: pluginsdk.TypeString,
Computed: true,
},

"tags": commonschema.TagsDataSource(),
}
}

func (DnsDSRecordDataResource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Dns.RecordSets
subscriptionId := metadata.Client.Account.SubscriptionId

var state DnsDSRecordDataSourceModel
if err := metadata.Decode(&state); err != nil {
return fmt.Errorf("decoding: %+v", err)
}
zoneId, err := zones.ParseDnsZoneID(state.ZoneId)
if err != nil {
return fmt.Errorf("parsing dns_zone_id: %+v", err)
}
id := recordsets.NewRecordTypeID(subscriptionId, zoneId.ResourceGroupName, zoneId.DnsZoneName, recordsets.RecordTypeDS, state.Name)

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("retrieving %s: %+v", id, err)
}

if model := resp.Model; model != nil {
if props := model.Properties; props != nil {
state.Ttl = pointer.From(props.TTL)
state.Fqdn = pointer.From(props.Fqdn)

state.Record = flattenDnsDSRecords(props.DSRecords)

state.Tags = pointer.From(props.Metadata)
}
}
metadata.SetID(id)

return metadata.Encode(&state)
},
}
}
44 changes: 44 additions & 0 deletions internal/services/dns/dns_ds_record_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package dns_test

import (
"fmt"
"testing"

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

type DnsDSRecordDataSource struct{}

func TestAccDataSourceDnsDSRecord_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_dns_ds_record", "test")
r := DnsDSRecordDataSource{}

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("name").Exists(),
check.That(data.ResourceName).Key("dns_zone_id").Exists(),
check.That(data.ResourceName).Key("record.#").HasValue("2"),
check.That(data.ResourceName).Key("ttl").Exists(),
check.That(data.ResourceName).Key("fqdn").Exists(),
check.That(data.ResourceName).Key("tags.%").HasValue("0"),
),
},
})
}

func (DnsDSRecordDataSource) basic(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
data "azurerm_dns_ds_record" "test" {
name = azurerm_dns_ds_record.test.name
dns_zone_id = azurerm_dns_zone.test.id
}
`, DnsDSRecordResource{}.basic(data))
}
Loading

0 comments on commit 57deeb6

Please sign in to comment.