Skip to content

Commit

Permalink
Add DNS alias support (#2)
Browse files Browse the repository at this point in the history
* Update DNS SDK to 2018-05-01
* A and AAAA record support for aliases

* Add missing }

* Update modules.txt and fix imports for dns tests

* Fic incorrect function name

* AAAA testacc pass! + improve resource read code:

* A
* CNAME
* AAAA

* Add CNAME tests

* Change CNAME tests to Zone Record Set

* Update docs
  • Loading branch information
matt-FFFFFF authored Dec 11, 2019
1 parent c4cd97f commit ec7fb85
Show file tree
Hide file tree
Showing 32 changed files with 861 additions and 282 deletions.
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) {
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)

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 {
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

0 comments on commit ec7fb85

Please sign in to comment.