Skip to content

Commit

Permalink
Replace DNS NS record objects with records array
Browse files Browse the repository at this point in the history
- Replace and deprecate the `azurerm_dns_ns_record.record` objects with
a single `records` array to simplify working with DNS zone resources
- Duplicate tests to have coverage for the deprecated attribute
- Add TODOs to remove code once the deprecated attribute is removed
- Update docs for the new attribute
  • Loading branch information
phekmat committed Jun 1, 2018
1 parent 23af7a2 commit 1e7773d
Show file tree
Hide file tree
Showing 3 changed files with 261 additions and 30 deletions.
79 changes: 63 additions & 16 deletions azurerm/resource_arm_dns_ns_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ func resourceArmDnsNsRecord() *schema.Resource {
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
// MigrateState: resourceArmDNSNsMigrateState,
// SchemaVersion: 1,

Schema: map[string]*schema.Schema{
"name": {
Expand All @@ -33,9 +35,21 @@ func resourceArmDnsNsRecord() *schema.Resource {
Required: true,
},

"records": {
Type: schema.TypeList,
//TODO: add `Required: true` once we remove the `record` attribute
Optional: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
ConflictsWith: []string{"record"},
},

"record": {
Type: schema.TypeSet,
Required: true,
Type: schema.TypeSet,
Optional: true,
Computed: true,
Deprecated: "This field has been replaced by `records`",
ConflictsWith: []string{"records"},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"nsdname": {
Expand Down Expand Up @@ -122,8 +136,13 @@ func resourceArmDnsNsRecordRead(d *schema.ResourceData, meta interface{}) error
d.Set("zone_name", zoneName)
d.Set("ttl", resp.TTL)

if err := d.Set("record", flattenAzureRmDnsNsRecords(resp.NsRecords)); err != nil {
return err
if err := d.Set("records", flattenAzureRmDnsNsRecords(resp.NsRecords)); err != nil {
return fmt.Errorf("Error settings `records`: %+v", err)
}

//TODO: remove this once we remove the `record` attribute
if err := d.Set("record", flattenAzureRmDnsNsRecordsSet(resp.NsRecords)); err != nil {
return fmt.Errorf("Error settings `record`: %+v", err)
}

flattenAndSetTags(d, resp.Metadata)
Expand Down Expand Up @@ -152,35 +171,63 @@ func resourceArmDnsNsRecordDelete(d *schema.ResourceData, meta interface{}) erro
return nil
}

func flattenAzureRmDnsNsRecords(records *[]dns.NsRecord) []map[string]interface{} {
//TODO: remove this once we remove the `record` attribute
func flattenAzureRmDnsNsRecordsSet(records *[]dns.NsRecord) []map[string]interface{} {
results := make([]map[string]interface{}, 0, len(*records))

if records != nil {
for _, record := range *records {
nsRecord := make(map[string]interface{})
nsRecord["nsdname"] = *record.Nsdname

results = append(results, nsRecord)
}
}

return results
}

func flattenAzureRmDnsNsRecords(records *[]dns.NsRecord) []string {
results := make([]string, 0, len(*records))

if records != nil {
for _, record := range *records {
results = append(results, *record.Nsdname)
}
}

return results
}

func expandAzureRmDnsNsRecords(d *schema.ResourceData) ([]dns.NsRecord, error) {
recordStrings := d.Get("record").(*schema.Set).List()
records := make([]dns.NsRecord, len(recordStrings))
var records []dns.NsRecord

for i, v := range recordStrings {
record := v.(map[string]interface{})
nsdName := record["nsdname"].(string)
//TODO: remove this once we remove the `record` attribute
if d.HasChange("records") {
recordStrings := d.Get("records").([]interface{})
records = make([]dns.NsRecord, len(recordStrings))
for i, v := range recordStrings {
record := v.(string)

nsRecord := dns.NsRecord{
Nsdname: &nsdName,
}
nsRecord := dns.NsRecord{
Nsdname: &record,
}

records[i] = nsRecord
records[i] = nsRecord
}
} else {
recordList := d.Get("record").(*schema.Set).List()
if len(recordList) != 0 {
records = make([]dns.NsRecord, len(recordList))
for i, v := range recordList {
record := v.(map[string]interface{})
nsdname := record["nsdname"].(string)
nsRecord := dns.NsRecord{
Nsdname: &nsdname,
}

records[i] = nsRecord
}
}
}

return records, nil
}
200 changes: 194 additions & 6 deletions azurerm/resource_arm_dns_ns_record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,27 @@ import (
"github.com/hashicorp/terraform/terraform"
)

//TODO: remove this once we remove the `record` attribute
func TestAccAzureRMDnsNsRecord_deprecatedBasic(t *testing.T) {
resourceName := "azurerm_dns_ns_record.test"
ri := acctest.RandInt()
config := testAccAzureRMDnsNsRecord_deprecatedBasic(ri, testLocation())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMDnsNsRecordDestroy,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMDnsNsRecordExists(resourceName),
),
},
},
})
}

func TestAccAzureRMDnsNsRecord_basic(t *testing.T) {
resourceName := "azurerm_dns_ns_record.test"
ri := acctest.RandInt()
Expand All @@ -31,6 +52,37 @@ func TestAccAzureRMDnsNsRecord_basic(t *testing.T) {
})
}

//TODO: remove this once we remove the `record` attribute
func TestAccAzureRMDnsNsRecord_deprecatedUpdateRecords(t *testing.T) {
resourceName := "azurerm_dns_ns_record.test"
ri := acctest.RandInt()
location := testLocation()
preConfig := testAccAzureRMDnsNsRecord_deprecatedBasic(ri, location)
postConfig := testAccAzureRMDnsNsRecord_deprecatedUpdateRecords(ri, location)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMDnsNsRecordDestroy,
Steps: []resource.TestStep{
{
Config: preConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMDnsNsRecordExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "record.#", "2"),
),
},
{
Config: postConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMDnsNsRecordExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "record.#", "3"),
),
},
},
})
}

func TestAccAzureRMDnsNsRecord_updateRecords(t *testing.T) {
resourceName := "azurerm_dns_ns_record.test"
ri := acctest.RandInt()
Expand All @@ -47,14 +99,45 @@ func TestAccAzureRMDnsNsRecord_updateRecords(t *testing.T) {
Config: preConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMDnsNsRecordExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "record.#", "2"),
resource.TestCheckResourceAttr(resourceName, "records.#", "2"),
),
},
{
Config: postConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMDnsNsRecordExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "record.#", "3"),
resource.TestCheckResourceAttr(resourceName, "records.#", "3"),
),
},
},
})
}

//TODO: remove this once we remove the `record` attribute
func TestAccAzureRMDnsNsRecord_deprecatedWithTags(t *testing.T) {
resourceName := "azurerm_dns_ns_record.test"
ri := acctest.RandInt()
location := testLocation()
preConfig := testAccAzureRMDnsNsRecord_deprecatedWithTags(ri, location)
postConfig := testAccAzureRMDnsNsRecord_deprecatedWithTagsUpdate(ri, location)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMDnsNsRecordDestroy,
Steps: []resource.TestStep{
{
Config: preConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMDnsNsRecordExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "2"),
),
},
{
Config: postConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMDnsNsRecordExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
),
},
},
Expand Down Expand Up @@ -161,18 +244,42 @@ resource "azurerm_dns_zone" "test" {
resource_group_name = "${azurerm_resource_group.test.name}"
}
resource "azurerm_dns_ns_record" "test" {
name = "mynsrecord%d"
resource_group_name = "${azurerm_resource_group.test.name}"
zone_name = "${azurerm_dns_zone.test.name}"
ttl = 300
records = ["ns1.contoso.com", "ns2.contoso.com"]
}
`, rInt, location, rInt, rInt)
}

//TODO: remove this once we remove the `record` attribute
func testAccAzureRMDnsNsRecord_deprecatedBasic(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_dns_ns_record" "test" {
name = "mynsrecord%d"
resource_group_name = "${azurerm_resource_group.test.name}"
zone_name = "${azurerm_dns_zone.test.name}"
ttl = 300
record {
nsdname = "ns1.contoso.com"
nsdname = "ns1.contoso.com"
}
record {
nsdname = "ns2.contoso.com"
nsdname = "ns2.contoso.com"
}
}
`, rInt, location, rInt, rInt)
Expand All @@ -190,6 +297,30 @@ resource "azurerm_dns_zone" "test" {
resource_group_name = "${azurerm_resource_group.test.name}"
}
resource "azurerm_dns_ns_record" "test" {
name = "mynsrecord%d"
resource_group_name = "${azurerm_resource_group.test.name}"
zone_name = "${azurerm_dns_zone.test.name}"
ttl = 300
records = ["ns1.contoso.com", "ns2.contoso.com", "ns3.contoso.com"]
}
`, rInt, location, rInt, rInt)
}

//TODO: remove this once we remove the `record` attribute
func testAccAzureRMDnsNsRecord_deprecatedUpdateRecords(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_dns_ns_record" "test" {
name = "mynsrecord%d"
resource_group_name = "${azurerm_resource_group.test.name}"
Expand Down Expand Up @@ -229,9 +360,38 @@ resource "azurerm_dns_ns_record" "test" {
zone_name = "${azurerm_dns_zone.test.name}"
ttl = 300
record {
nsdname = "ns1.contoso.com"
records = ["ns1.contoso.com", "ns2.contoso.com"]
tags {
environment = "Production"
cost_center = "MSFT"
}
}
`, rInt, location, rInt, rInt)
}

//TODO: remove this once we remove the `record` attribute
func testAccAzureRMDnsNsRecord_deprecatedWithTags(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_dns_ns_record" "test" {
name = "mynsrecord%d"
resource_group_name = "${azurerm_resource_group.test.name}"
zone_name = "${azurerm_dns_zone.test.name}"
ttl = 300
record {
nsdname = "ns1.contoso.com"
}
record {
nsdname = "ns2.contoso.com"
Expand All @@ -257,6 +417,34 @@ resource "azurerm_dns_zone" "test" {
resource_group_name = "${azurerm_resource_group.test.name}"
}
resource "azurerm_dns_ns_record" "test" {
name = "mynsrecord%d"
resource_group_name = "${azurerm_resource_group.test.name}"
zone_name = "${azurerm_dns_zone.test.name}"
ttl = 300
records = ["ns1.contoso.com", "ns2.contoso.com"]
tags {
environment = "staging"
}
}
`, rInt, location, rInt, rInt)
}

//TODO: remove this once we remove the `record` attribute
func testAccAzureRMDnsNsRecord_deprecatedWithTagsUpdate(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_dns_ns_record" "test" {
name = "mynsrecord%d"
resource_group_name = "${azurerm_resource_group.test.name}"
Expand Down
Loading

0 comments on commit 1e7773d

Please sign in to comment.