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

dns - switch to go-azure-sdk #17986

Merged
merged 6 commits into from
Aug 17, 2022
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
3 changes: 2 additions & 1 deletion internal/clients/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/validation"
dns_v2018_05_01 "github.com/hashicorp/go-azure-sdk/resource-manager/dns/2018-05-01"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
"github.com/hashicorp/terraform-provider-azurerm/internal/features"
aadb2c "github.com/hashicorp/terraform-provider-azurerm/internal/services/aadb2c/client"
Expand Down Expand Up @@ -156,7 +157,7 @@ type Client struct {
DevTestLabs *devtestlabs.Client
DigitalTwins *digitaltwins.Client
Disks *disks.Client
Dns *dns.Client
Dns *dns_v2018_05_01.Client
DomainServices *domainservices.Client
Elastic *elastic.Client
EventGrid *eventgrid.Client
Expand Down
24 changes: 7 additions & 17 deletions internal/services/dns/client/client.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
package client

import (
"github.com/Azure/azure-sdk-for-go/services/dns/mgmt/2018-05-01/dns"
"github.com/Azure/go-autorest/autorest"
dns_v2018_05_01 "github.com/hashicorp/go-azure-sdk/resource-manager/dns/2018-05-01"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
)

type Client struct {
RecordSetsClient *dns.RecordSetsClient
ZonesClient *dns.ZonesClient
}

func NewClient(o *common.ClientOptions) *Client {
RecordSetsClient := dns.NewRecordSetsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&RecordSetsClient.Client, o.ResourceManagerAuthorizer)

ZonesClient := dns.NewZonesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&ZonesClient.Client, o.ResourceManagerAuthorizer)

return &Client{
RecordSetsClient: &RecordSetsClient,
ZonesClient: &ZonesClient,
}
func NewClient(o *common.ClientOptions) *dns_v2018_05_01.Client {
client := dns_v2018_05_01.NewClientWithBaseURI(o.ResourceManagerEndpoint, func(c *autorest.Client) {
c.Authorizer = o.ResourceManagerAuthorizer
})
return &client
}
58 changes: 30 additions & 28 deletions internal/services/dns/dns_a_record_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import (
"fmt"
"time"

"github.com/Azure/azure-sdk-for-go/services/dns/mgmt/2018-05-01/dns"
"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/2018-05-01/recordsets"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/dns/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tags"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

func dataSourceDnsARecord() *pluginsdk.Resource {
Expand Down Expand Up @@ -57,48 +56,51 @@ func dataSourceDnsARecord() *pluginsdk.Resource {
Computed: true,
},

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

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

name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)
zoneName := d.Get("zone_name").(string)
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, resourceGroup, zoneName, name, dns.A)
resp, err := recordSetsClient.Get(ctx, id)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Error: DNS A record %s: (zone %s) was not found", name, zoneName)
if response.WasNotFound(resp.HttpResponse) {
return fmt.Errorf("%s was not found", id)
}
return fmt.Errorf("reading DNS A record %s (zone %s): %+v", name, zoneName, err)
return fmt.Errorf("reading %s: %+v", id, err)
}

resourceId := parse.NewARecordID(subscriptionId, resourceGroup, zoneName, name)
d.SetId(resourceId.ID())
d.SetId(id.ID())

d.Set("name", name)
d.Set("resource_group_name", resourceGroup)
d.Set("zone_name", zoneName)
d.Set("name", id.RelativeRecordSetName)
d.Set("resource_group_name", id.ResourceGroupName)
d.Set("zone_name", id.ZoneName)

d.Set("ttl", resp.TTL)
d.Set("fqdn", resp.Fqdn)
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", flattenAzureRmDnsARecords(resp.ARecords)); err != nil {
return fmt.Errorf("setting `records`: %+v", err)
}
if err := d.Set("records", flattenAzureRmDnsARecords(props.ARecords)); err != nil {
return fmt.Errorf("setting `records`: %+v", err)
}

targetResourceId := ""
if props.TargetResource != nil && props.TargetResource.Id != nil {
targetResourceId = *props.TargetResource.Id
}
d.Set("target_resource_id", targetResourceId)

targetResourceId := ""
if resp.TargetResource != nil && resp.TargetResource.ID != nil {
targetResourceId = *resp.TargetResource.ID
return tags.FlattenAndSet(d, props.Metadata)
}
}
d.Set("target_resource_id", targetResourceId)

return tags.FlattenAndSet(d, resp.Metadata)
return nil
}
120 changes: 66 additions & 54 deletions internal/services/dns/dns_a_record_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package dns

import (
"fmt"
"net/http"
"time"

"github.com/Azure/azure-sdk-for-go/services/dns/mgmt/2018-05-01/dns"
"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/2018-05-01/recordsets"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/dns/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tags"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
"github.com/hashicorp/terraform-provider-azurerm/utils"
Expand All @@ -30,8 +30,14 @@ func resourceDnsARecord() *pluginsdk.Resource {
Delete: pluginsdk.DefaultTimeout(30 * time.Minute),
},
Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := parse.ARecordID(id)
return err
parsed, err := recordsets.ParseRecordTypeID(id)
if err != nil {
return err
}
if parsed.RecordType != recordsets.RecordTypeA {
return fmt.Errorf("this resource only supports 'A' records")
}
return nil
}),

Schema: map[string]*pluginsdk.Schema{
Expand All @@ -41,11 +47,12 @@ func resourceDnsARecord() *pluginsdk.Resource {
ForceNew: true,
},

"resource_group_name": azure.SchemaResourceGroupName(),
"resource_group_name": commonschema.ResourceGroupName(),

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

"records": {
Expand Down Expand Up @@ -75,13 +82,13 @@ func resourceDnsARecord() *pluginsdk.Resource {
// TODO: switch ConflictsWith for ExactlyOneOf when the Provider SDK's updated
},

"tags": tags.Schema(),
"tags": commonschema.Tags(),
},
}
}

func resourceDnsARecordCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Dns.RecordSetsClient
client := meta.(*clients.Client).Dns.RecordSets
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
defer cancel()
Expand All @@ -90,18 +97,17 @@ func resourceDnsARecordCreateUpdate(d *pluginsdk.ResourceData, meta interface{})
resGroup := d.Get("resource_group_name").(string)
zoneName := d.Get("zone_name").(string)

resourceId := parse.NewARecordID(subscriptionId, resGroup, zoneName, name)

id := recordsets.NewRecordTypeID(subscriptionId, resGroup, zoneName, recordsets.RecordTypeA, name)
if d.IsNewResource() {
existing, err := client.Get(ctx, resGroup, zoneName, name, dns.A)
existing, err := client.Get(ctx, id)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for presence of existing DNS A Record %q (Zone %q / Resource Group %q): %s", name, zoneName, resGroup, err)
if !response.WasNotFound(existing.HttpResponse) {
return fmt.Errorf("checking for presence of existing %s: %+v", id, err)
}
}

if !utils.ResponseWasNotFound(existing.Response) {
return tf.ImportAsExistsError("azurerm_dns_a_record", resourceId.ID())
if !response.WasNotFound(existing.HttpResponse) {
return tf.ImportAsExistsError("azurerm_dns_a_record", id.ID())
}
}

Expand All @@ -110,118 +116,124 @@ func resourceDnsARecordCreateUpdate(d *pluginsdk.ResourceData, meta interface{})
targetResourceId := d.Get("target_resource_id").(string)
recordsRaw := d.Get("records").(*pluginsdk.Set).List()

parameters := dns.RecordSet{
parameters := recordsets.RecordSet{
Name: &name,
RecordSetProperties: &dns.RecordSetProperties{
Properties: &recordsets.RecordSetProperties{
Metadata: tags.Expand(t),
TTL: &ttl,
ARecords: expandAzureRmDnsARecords(recordsRaw),
TargetResource: &dns.SubResource{},
TargetResource: &recordsets.SubResource{},
},
}

if targetResourceId != "" {
parameters.RecordSetProperties.TargetResource.ID = utils.String(targetResourceId)
parameters.Properties.TargetResource.Id = utils.String(targetResourceId)
}

// TODO: this can be removed when the provider SDK is upgraded
if targetResourceId == "" && len(recordsRaw) == 0 {
return fmt.Errorf("One of either `records` or `target_resource_id` must be specified")
}

eTag := ""
ifNoneMatch := "" // set to empty to allow updates to records after creation
if _, err := client.CreateOrUpdate(ctx, resGroup, zoneName, name, dns.A, parameters, eTag, ifNoneMatch); err != nil {
if _, err := client.CreateOrUpdate(ctx, id, parameters, recordsets.DefaultCreateOrUpdateOperationOptions()); err != nil {
return fmt.Errorf("creating/updating DNS A Record %q (Zone %q / Resource Group %q): %s", name, zoneName, resGroup, err)
}

d.SetId(resourceId.ID())
d.SetId(id.ID())

return resourceDnsARecordRead(d, meta)
}

func resourceDnsARecordRead(d *pluginsdk.ResourceData, meta interface{}) error {
dnsClient := meta.(*clients.Client).Dns.RecordSetsClient
client := meta.(*clients.Client).Dns.RecordSets
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.ARecordID(d.Id())
id, err := recordsets.ParseRecordTypeID(d.Id())
if err != nil {
return err
}

resp, err := dnsClient.Get(ctx, id.ResourceGroup, id.DnszoneName, id.AName, dns.A)
resp, err := client.Get(ctx, *id)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
if response.WasNotFound(resp.HttpResponse) {
d.SetId("")
return nil
}
return fmt.Errorf("reading DNS A record %s: %+v", id.AName, err)
return fmt.Errorf("retrieving %s: %+v", *id, err)
}

d.Set("name", id.AName)
d.Set("resource_group_name", id.ResourceGroup)
d.Set("zone_name", id.DnszoneName)
d.Set("name", id.RelativeRecordSetName)
d.Set("resource_group_name", id.ResourceGroupName)
d.Set("zone_name", id.ZoneName)

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

if err := d.Set("records", flattenAzureRmDnsARecords(resp.ARecords)); err != nil {
return fmt.Errorf("setting `records`: %+v", err)
}
if err := d.Set("records", flattenAzureRmDnsARecords(props.ARecords)); err != nil {
return fmt.Errorf("setting `records`: %+v", err)
}

targetResourceId := ""
if props.TargetResource != nil && props.TargetResource.Id != nil {
targetResourceId = *props.TargetResource.Id
}
d.Set("target_resource_id", targetResourceId)

if err := tags.FlattenAndSet(d, props.Metadata); err != nil {
return err
}
}

targetResourceId := ""
if resp.TargetResource != nil && resp.TargetResource.ID != nil {
targetResourceId = *resp.TargetResource.ID
}
d.Set("target_resource_id", targetResourceId)

return tags.FlattenAndSet(d, resp.Metadata)
return nil
}

func resourceDnsARecordDelete(d *pluginsdk.ResourceData, meta interface{}) error {
dnsClient := meta.(*clients.Client).Dns.RecordSetsClient
client := meta.(*clients.Client).Dns.RecordSets
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.ARecordID(d.Id())
id, err := recordsets.ParseRecordTypeID(d.Id())
if err != nil {
return err
}

resp, err := dnsClient.Delete(ctx, id.ResourceGroup, id.DnszoneName, id.AName, dns.A, "")
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("deleting DNS A Record %s: %+v", id.AName, err)
if _, err := client.Delete(ctx, *id, recordsets.DefaultDeleteOperationOptions()); err != nil {
return fmt.Errorf("deleting %s: %+v", *id, err)
}

return nil
}

func expandAzureRmDnsARecords(input []interface{}) *[]dns.ARecord {
records := make([]dns.ARecord, len(input))
func expandAzureRmDnsARecords(input []interface{}) *[]recordsets.ARecord {
records := make([]recordsets.ARecord, len(input))

for i, v := range input {
ipv4 := v.(string)
records[i] = dns.ARecord{
Ipv4Address: &ipv4,
records[i] = recordsets.ARecord{
IPv4Address: &ipv4,
}
}

return &records
}

func flattenAzureRmDnsARecords(records *[]dns.ARecord) []string {
func flattenAzureRmDnsARecords(records *[]recordsets.ARecord) []string {
if records == nil {
return []string{}
}

results := make([]string, 0)
for _, record := range *records {
if record.Ipv4Address == nil {
if record.IPv4Address == nil {
continue
}

results = append(results, *record.Ipv4Address)
results = append(results, *record.IPv4Address)
}

return results
Expand Down
Loading