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

r/dns_(a|aaaa|cname)_record: support for configuring target_resource_id #5218

Merged
merged 5 commits into from
Dec 19, 2019
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
2 changes: 1 addition & 1 deletion azurerm/data_source_dns_zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"time"

"github.com/Azure/azure-sdk-for-go/services/preview/dns/mgmt/2018-03-01-preview/dns"
"github.com/Azure/azure-sdk-for-go/services/dns/mgmt/2018-05-01/dns"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2018-05-01/resources"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
Expand Down
2 changes: 1 addition & 1 deletion azurerm/internal/services/dns/client/client.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package client

import (
"github.com/Azure/azure-sdk-for-go/services/preview/dns/mgmt/2018-03-01-preview/dns"
"github.com/Azure/azure-sdk-for-go/services/dns/mgmt/2018-05-01/dns"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/common"
)

Expand Down
83 changes: 58 additions & 25 deletions azurerm/resource_arm_dns_a_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"net/http"
"time"

"github.com/Azure/azure-sdk-for-go/services/preview/dns/mgmt/2018-03-01-preview/dns"
"github.com/Azure/azure-sdk-for-go/services/dns/mgmt/2018-05-01/dns"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
Expand Down Expand Up @@ -47,10 +47,11 @@ func resourceArmDnsARecord() *schema.Resource {
},

"records": {
Type: schema.TypeSet,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
ConflictsWith: []string{"target_resource_id"},
},

"ttl": {
Expand All @@ -63,6 +64,15 @@ func resourceArmDnsARecord() *schema.Resource {
Computed: true,
},

"target_resource_id": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: azure.ValidateResourceID,
ConflictsWith: []string{"records"},

// TODO: switch ConflictsWith for ExactlyOneOf when the Provider SDK's updated
},

"tags": tags.Schema(),
},
}
Expand Down Expand Up @@ -92,16 +102,28 @@ func resourceArmDnsARecordCreateUpdate(d *schema.ResourceData, meta interface{})

ttl := int64(d.Get("ttl").(int))
t := d.Get("tags").(map[string]interface{})
targetResourceId := d.Get("target_resource_id").(string)
recordsRaw := d.Get("records").(*schema.Set).List()

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

if targetResourceId != "" {
parameters.RecordSetProperties.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 {
Expand All @@ -114,7 +136,7 @@ func resourceArmDnsARecordCreateUpdate(d *schema.ResourceData, meta interface{})
}

if resp.ID == nil {
return fmt.Errorf("Cannot read DNS A Record %s (resource group %s) ID", name, resGroup)
return fmt.Errorf("Error retrieving DNS A Record %q (Zone %q / Resource Group %q): ID was nil", name, zoneName, resGroup)
}

d.SetId(*resp.ID)
Expand Down Expand Up @@ -148,12 +170,19 @@ func resourceArmDnsARecordRead(d *schema.ResourceData, meta interface{}) error {
d.Set("name", name)
d.Set("resource_group_name", resGroup)
d.Set("zone_name", zoneName)
d.Set("ttl", resp.TTL)
d.Set("fqdn", resp.Fqdn)
d.Set("ttl", resp.TTL)

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

Expand All @@ -179,28 +208,32 @@ func resourceArmDnsARecordDelete(d *schema.ResourceData, meta interface{}) error
return nil
}

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

if records != nil {
for _, record := range *records {
results = append(results, *record.Ipv4Address)
for i, v := range input {
ipv4 := v.(string)
records[i] = dns.ARecord{
Ipv4Address: &ipv4,
}
}

return results
return &records
}

func expandAzureRmDnsARecords(d *schema.ResourceData) *[]dns.ARecord {
recordStrings := d.Get("records").(*schema.Set).List()
records := make([]dns.ARecord, len(recordStrings))
func flattenAzureRmDnsARecords(records *[]dns.ARecord) []string {
if records == nil {
return []string{}
}

for i, v := range recordStrings {
ipv4 := v.(string)
records[i] = dns.ARecord{
Ipv4Address: &ipv4,
results := make([]string, 0)
for _, record := range *records {
if record.Ipv4Address == nil {
continue
}

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

return &records
return results
}
Loading