Skip to content

Commit

Permalink
Add TLSA records
Browse files Browse the repository at this point in the history
  • Loading branch information
alexwilcox9 committed Dec 10, 2024
1 parent 3127786 commit 0b73626
Show file tree
Hide file tree
Showing 7 changed files with 968 additions and 0 deletions.
119 changes: 119 additions & 0 deletions internal/services/dns/dns_tlsa_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 dataSourceDnsTLSARecord() *pluginsdk.Resource {
return &pluginsdk.Resource{
Read: dataSourceDnsTLSARecordRead,

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{
"matching_type": {
Type: pluginsdk.TypeInt,
Computed: true,
},

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

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

"cert_association_data": {
Type: pluginsdk.TypeString,
Computed: true,
},
},
},
Set: resourceDnsTLSARecordHash,
},

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

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

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

func dataSourceDnsTLSARecordRead(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.RecordTypeTLSA, 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", flattenAzureRmDnsTLSARecords(props.TLSARecords)); err != nil {
return err
}

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

return nil
}
46 changes: 46 additions & 0 deletions internal/services/dns/dns_tlsa_record_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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 DnsTLSARecordDataSource struct{}

func TestAccDataSourceDnsTLSARecord_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_dns_tlsa_record", "test")
r := DnsTLSARecordDataSource{}

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("name").Exists(),
check.That(data.ResourceName).Key("resource_group_name").Exists(),
check.That(data.ResourceName).Key("zone_name").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 (DnsTLSARecordDataSource) basic(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
data "azurerm_dns_tlsa_record" "test" {
name = azurerm_dns_tlsa_record.test.name
resource_group_name = azurerm_resource_group.test.name
zone_name = azurerm_dns_zone.test.name
}
`, DnsTLSARecordResource{}.basic(data))
}
Loading

0 comments on commit 0b73626

Please sign in to comment.