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 10, 2024
1 parent d5d20ec commit fef53ae
Show file tree
Hide file tree
Showing 5 changed files with 861 additions and 0 deletions.
119 changes: 119 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,119 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package dns

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/dns/2023-07-01-preview/recordsets"
"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 dataSourceDnsDSRecord() *pluginsdk.Resource {
return &pluginsdk.Resource{
Read: dataSourceDnsDSRecordRead,

Timeouts: &pluginsdk.ResourceTimeout{
Read: pluginsdk.DefaultTimeout(5 * time.Minute),
},

Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
},

"resource_group_name": commonschema.ResourceGroupNameForDataSource(),

"zone_name": {
Type: pluginsdk.TypeString,
Required: true,
},

"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,
},
},
},
Set: resourceDnsDSRecordHash,
},

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

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

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

func dataSourceDnsDSRecordRead(d *pluginsdk.ResourceData, meta interface{}) error {
recordSetsClient := meta.(*clients.Client).Dns.RecordSets
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()
subscriptionId := meta.(*clients.Client).Account.SubscriptionId

id := recordsets.NewRecordTypeID(subscriptionId, d.Get("resource_group_name").(string), d.Get("zone_name").(string), recordsets.RecordTypeDS, d.Get("name").(string))
resp, err := recordSetsClient.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.RelativeRecordSetName)
d.Set("resource_group_name", id.ResourceGroupName)
d.Set("zone_name", id.DnsZoneName)

if model := resp.Model; model != nil {
if props := model.Properties; props != nil {
d.Set("ttl", props.TTL)
d.Set("fqdn", props.Fqdn)

if err := d.Set("record", flattenAzureRmDnsDSRecords(props.DSRecords)); err != nil {
return err
}

return tags.FlattenAndSet(d, props.Metadata)
}
}

return nil
}
Loading

0 comments on commit fef53ae

Please sign in to comment.