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

Add data sources for every type of private DNS record #18036

Merged
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,95 @@
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/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 dataSourcePrivateDnsARecord() *pluginsdk.Resource {
return &pluginsdk.Resource{
Read: dataSourcePrivateDnsARecordRead,

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,
},

"records": {
Type: pluginsdk.TypeSet,
Computed: true,
Elem: &pluginsdk.Schema{Type: pluginsdk.TypeString},
Set: pluginsdk.HashString,
},

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

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

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

func dataSourcePrivateDnsARecordRead(d *pluginsdk.ResourceData, meta interface{}) error {
recordSetsClient := meta.(*clients.Client).PrivateDns.RecordSetsClient
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.RecordTypeA, 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.PrivateZoneName)

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("records", flattenAzureRmPrivateDnsARecords(props.ARecords)); err != nil {
return fmt.Errorf("setting `records`: %+v", err)
}

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

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package privatedns_test

import (
"fmt"
"testing"

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

type PrivateDnsARecordDataSource struct{}

func TestAccDataSourcePrivateDnsARecord_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_private_dns_a_record", "test")
r := PrivateDnsARecordDataSource{}

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("records.#").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 (PrivateDnsARecordDataSource) basic(data acceptance.TestData) string {
return fmt.Sprintf(`
%s

data "azurerm_private_dns_a_record" "test" {
name = azurerm_private_dns_a_record.test.name
resource_group_name = azurerm_resource_group.test.name
zone_name = azurerm_private_dns_zone.test.name
}
`, PrivateDnsARecordResource{}.basic(data))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
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/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/tf/set"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
)

func dataSourcePrivateDnsAaaaRecord() *pluginsdk.Resource {
return &pluginsdk.Resource{
Read: dataSourcePrivateDnsAAAARecordRead,

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,
},

"records": {
Type: pluginsdk.TypeSet,
Computed: true,
Elem: &pluginsdk.Schema{Type: pluginsdk.TypeString},
Set: set.HashIPv6Address,
},

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

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

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

func dataSourcePrivateDnsAAAARecordRead(d *pluginsdk.ResourceData, meta interface{}) error {
recordSetsClient := meta.(*clients.Client).PrivateDns.RecordSetsClient
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.RecordTypeAAAA, 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.PrivateZoneName)

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("records", flattenAzureRmPrivateDnsAaaaRecords(props.AaaaRecords)); err != nil {
return fmt.Errorf("setting `records`: %+v", err)
}

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

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package privatedns_test

import (
"fmt"
"testing"

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

type PrivateDnsAAAARecordDataSource struct{}

func TestAccDataSourcePrivateDnsAAAARecord_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_private_dns_aaaa_record", "test")
r := PrivateDnsAAAARecordDataSource{}

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("records.#").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 (PrivateDnsAAAARecordDataSource) basic(data acceptance.TestData) string {
return fmt.Sprintf(`
%s

data "azurerm_private_dns_aaaa_record" "test" {
name = azurerm_private_dns_aaaa_record.test.name
resource_group_name = azurerm_resource_group.test.name
zone_name = azurerm_private_dns_zone.test.name
}
`, PrivateDnsAAAARecordResource{}.basic(data))
}
101 changes: 101 additions & 0 deletions internal/services/privatedns/private_dns_cname_record_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
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/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 dataSourcePrivateDnsCNameRecord() *pluginsdk.Resource {
return &pluginsdk.Resource{
Read: dataSourcePrivateDnsCNameRecordRead,

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.TypeString,
Computed: true,
},

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

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

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

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

func dataSourcePrivateDnsCNameRecordRead(d *pluginsdk.ResourceData, meta interface{}) error {
recordSetsClient := meta.(*clients.Client).PrivateDns.RecordSetsClient
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.RecordTypeCNAME, 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.PrivateZoneName)

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

cname := ""
if props.CnameRecord != nil && props.CnameRecord.Cname != nil {
cname = *props.CnameRecord.Cname
}
d.Set("record", cname)

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

return nil
}
Loading