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 DNS alias support #5133

Closed
wants to merge 1 commit into from
Closed
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
44 changes: 34 additions & 10 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,13 @@ func resourceArmDnsARecord() *schema.Resource {
Computed: true,
},

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

"tags": tags.Schema(),
},
}
Expand Down Expand Up @@ -92,13 +100,24 @@ 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)

if bool(targetResourceId == "") && bool(len(d.Get("records").(*schema.Set).List()) == 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't need to be specified here since this logic is defined in the schema with:

ConflictsWith: []string{"target_resource_id"},

Also, do you need to specify at least one of these for this resource? If so, you can swap ConflictsWith with ExactlyOneOf which says one and only one of these keys must be specified. It'll look like

ExactlyOneOf: []string{"records", "target_resource_id"}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was confused slightly here, then I realised I had to update the plugin-sdk to 1.4.0.

return fmt.Errorf("Neither 'records' nor 'target_resource_id' is defined")
}

var targetResource dns.SubResource
if targetResourceId != "" {
targetResource.ID = utils.String(targetResourceId)
}

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(d),
TargetResource: &targetResource,
},
}

Expand Down Expand Up @@ -150,10 +169,15 @@ func resourceArmDnsARecordRead(d *schema.ResourceData, meta interface{}) error {
d.Set("zone_name", zoneName)
d.Set("ttl", resp.TTL)
d.Set("fqdn", resp.Fqdn)
d.Set("target_resource_id", (resp.TargetResource).ID)
tombuildsstuff marked this conversation as resolved.
Show resolved Hide resolved

if err := d.Set("records", flattenAzureRmDnsARecords(resp.ARecords)); err != nil {
return err
// Only flatten DNS records if they are present in the resource, e.g. not for alias records
if resp.ARecords != nil {
tombuildsstuff marked this conversation as resolved.
Show resolved Hide resolved
if err := d.Set("records", flattenAzureRmDnsARecords(resp.ARecords)); err != nil {
return err
}
}

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

Expand Down
99 changes: 98 additions & 1 deletion azurerm/resource_arm_dns_a_record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"net/http"
"testing"

"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/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
Expand Down Expand Up @@ -132,6 +132,43 @@ func TestAccAzureRMDnsARecord_withTags(t *testing.T) {
})
}

func TestAccAzureRMDnsARecord_withAlias(t *testing.T) {
resourceName := "azurerm_dns_a_record.test"
targetResourceName := "azurerm_public_ip.test"
targetResourceName2 := "azurerm_public_ip.test2"
ri := tf.AccRandTimeInt()
location := testLocation()
preConfig := testAccAzureRMDnsARecord_withAlias(ri, location)
postConfig := testAccAzureRMDnsARecord_withAliasUpdate(ri, location)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMDnsARecordDestroy,
Steps: []resource.TestStep{
{
Config: preConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMDnsARecordExists(resourceName),
resource.TestCheckResourceAttrPair(resourceName, "target_resource_id", targetResourceName, "id"),
),
},
{
Config: postConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMDnsARecordExists(resourceName),
resource.TestCheckResourceAttrPair(resourceName, "target_resource_id", targetResourceName2, "id"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testCheckAzureRMDnsARecordExists(resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
// Ensure we have enough information in state to look up in API
Expand Down Expand Up @@ -302,3 +339,63 @@ resource "azurerm_dns_a_record" "test" {
}
`, rInt, location, rInt, rInt)
}

func testAccAzureRMDnsARecord_withAlias(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}

resource "azurerm_dns_zone" "test" {
name = "acctestzone%d.com"
resource_group_name = "${azurerm_resource_group.test.name}"
}

resource "azurerm_public_ip" "test" {
name = "mypublicip%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
allocation_method = "Dynamic"
ip_version = "IPv4"
}

resource "azurerm_dns_a_record" "test" {
name = "myarecord%d"
resource_group_name = "${azurerm_resource_group.test.name}"
zone_name = "${azurerm_dns_zone.test.name}"
ttl = 300
target_resource_id = "${azurerm_public_ip.test.id}"
}
`, rInt, location, rInt, rInt, rInt)
}

func testAccAzureRMDnsARecord_withAliasUpdate(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}

resource "azurerm_dns_zone" "test" {
name = "acctestzone%d.com"
resource_group_name = "${azurerm_resource_group.test.name}"
}

resource "azurerm_public_ip" "test2" {
name = "mypublicip%d2"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
allocation_method = "Dynamic"
ip_version = "IPv4"
}

resource "azurerm_dns_a_record" "test" {
name = "myarecord%d"
resource_group_name = "${azurerm_resource_group.test.name}"
zone_name = "${azurerm_dns_zone.test.name}"
ttl = 300
target_resource_id = "${azurerm_public_ip.test2.id}"
}
`, rInt, location, rInt, rInt, rInt)
}
43 changes: 33 additions & 10 deletions azurerm/resource_arm_dns_aaaa_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 resourceArmDnsAAAARecord() *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 @@ -64,6 +65,13 @@ func resourceArmDnsAAAARecord() *schema.Resource {
},

"tags": tags.Schema(),

"target_resource_id": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: azure.ValidateResourceID,
ConflictsWith: []string{"records"},
},
},
}
}
Expand Down Expand Up @@ -92,13 +100,24 @@ func resourceArmDnsAaaaRecordCreateUpdate(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)

if bool(targetResourceId == "") && bool(len(d.Get("records").(*schema.Set).List()) == 0) {
return fmt.Errorf("Neither 'records' nor 'target_resource_id' is defined")
}

var targetResource dns.SubResource
if targetResourceId != "" {
targetResource.ID = utils.String(targetResourceId)
}

parameters := dns.RecordSet{
Name: &name,
RecordSetProperties: &dns.RecordSetProperties{
Metadata: tags.Expand(t),
TTL: &ttl,
AaaaRecords: expandAzureRmDnsAaaaRecords(d),
Metadata: tags.Expand(t),
TTL: &ttl,
AaaaRecords: expandAzureRmDnsAaaaRecords(d),
TargetResource: &targetResource,
},
}

Expand Down Expand Up @@ -150,9 +169,13 @@ func resourceArmDnsAaaaRecordRead(d *schema.ResourceData, meta interface{}) erro
d.Set("zone_name", zoneName)
d.Set("ttl", resp.TTL)
d.Set("fqdn", resp.Fqdn)
d.Set("target_resource_id", (resp.TargetResource).ID)

if err := d.Set("records", flattenAzureRmDnsAaaaRecords(resp.AaaaRecords)); err != nil {
return err
// Only flatten DNS records if they are present in the resource, e.g. not for alias records
if resp.AaaaRecords != nil {
if err := d.Set("records", flattenAzureRmDnsAaaaRecords(resp.AaaaRecords)); err != nil {
return err
}
}
return tags.FlattenAndSet(d, resp.Metadata)
}
Expand Down
Loading