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

INBOX-2456 - Add support for override_address_records for ALIAS records #332

Merged
merged 1 commit into from
Oct 10, 2024
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.4.4 (October 10, 2024)
BUGFIX
* Adds support for setting override_address_records for ALIAS records

## 2.4.3 (October 2, 2024)
* Modified target regex for redirects to align with server

Expand Down
2 changes: 1 addition & 1 deletion ns1/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
)

var (
clientVersion = "2.4.3"
clientVersion = "2.4.4"
providerUserAgent = "tf-ns1" + "/" + clientVersion
defaultRetryMax = 3
)
Expand Down
4 changes: 4 additions & 0 deletions ns1/data_source_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ func dataSourceRecord() *schema.Resource {
Type: schema.TypeInt,
Computed: true,
},
"override_address_records": {
Type: schema.TypeBool,
Computed: true,
},
"override_ttl": {
Type: schema.TypeBool,
Computed: true,
Expand Down
18 changes: 17 additions & 1 deletion ns1/resource_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ func recordResource() *schema.Resource {
Optional: true,
Computed: true,
},
"override_address_records": {
Type: schema.TypeBool,
Optional: true,
},
"override_ttl": {
Type: schema.TypeBool,
Optional: true,
Expand Down Expand Up @@ -242,10 +246,19 @@ func recordToResourceData(d *schema.ResourceData, r *dns.Record) error {
d.Set("blocked_tags", terraformBlockedTags)
}

if r.Type == "ALIAS" {
d.Set("override_address_records", false)
if r.OverrideAddressRecords != nil {
err := d.Set("override_address_records", *r.OverrideAddressRecords)
if err != nil {
return fmt.Errorf("[DEBUG] Error setting override_address_records for: %s, error: %#v", r.Domain, err)
}
}
}

d.Set("override_ttl", nil)
if r.Type == "ALIAS" && r.OverrideTTL != nil {
err := d.Set("override_ttl", *r.OverrideTTL)

if err != nil {
return fmt.Errorf("[DEBUG] Error setting override_ttl for: %s, error: %#v", r.Domain, err)
}
Expand Down Expand Up @@ -407,6 +420,9 @@ func resourceDataToRecord(r *dns.Record, d *schema.ResourceData) error {
}

if r.Type == "ALIAS" {
Override_AddressRecords := d.Get("override_address_records").(bool)
r.OverrideAddressRecords = &Override_AddressRecords

Override_TTL := d.Get("override_ttl").(bool)
r.OverrideTTL = &Override_TTL
}
Expand Down
176 changes: 176 additions & 0 deletions ns1/resource_record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,153 @@ func TestAccRecord_OverrideTTLTrueToFalse(t *testing.T) {
})
}

func TestAccRecord_OverrideAddressRecordsNilToTrue(t *testing.T) {
var record dns.Record
rString := acctest.RandStringFromCharSet(15, acctest.CharSetAlphaNum)

tfFileBasicAlias := testAccRecordBasicALIAS(rString)
tfFileOverrideAddressRecordsAliasTrue := testAccRecordBasicALIASOverrideAddressRecords(rString, true)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckRecordDestroy,
Steps: []resource.TestStep{
// Create an ALIAS record with override_address_records not set
{
Config: tfFileBasicAlias,
Check: resource.ComposeTestCheckFunc(
testAccCheckRecordExists("ns1_record.it", &record),
testAccCheckRecordOverrideAddressRecords(&record, false),
),
},
// Plan again to detect "loop" conditions
{
Config: tfFileBasicAlias,
PlanOnly: true,
ExpectNonEmptyPlan: false,
},
// Change override_address_records to true Plan and Apply
{
Config: tfFileOverrideAddressRecordsAliasTrue,
PlanOnly: true,
ExpectNonEmptyPlan: true,
},
{
Config: tfFileOverrideAddressRecordsAliasTrue,
Check: resource.ComposeTestCheckFunc(
testAccCheckRecordExists("ns1_record.it", &record),
testAccCheckRecordOverrideAddressRecords(&record, true),
),
},
// Plan again to detect "loop" conditions
{
Config: tfFileOverrideAddressRecordsAliasTrue,
PlanOnly: true,
ExpectNonEmptyPlan: false,
},
},
})
}

func TestAccRecord_OverrideAddressRecordsTrueToNil(t *testing.T) {
var record dns.Record
rString := acctest.RandStringFromCharSet(15, acctest.CharSetAlphaNum)

tfFileBasicAlias := testAccRecordBasicALIAS(rString)
tfFileOverrideAddressRecordsAliasTrue := testAccRecordBasicALIASOverrideAddressRecords(rString, true)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckRecordDestroy,
Steps: []resource.TestStep{
// Create an ALIAS record with override_address_records true
{
Config: tfFileOverrideAddressRecordsAliasTrue,
Check: resource.ComposeTestCheckFunc(
testAccCheckRecordExists("ns1_record.it", &record),
testAccCheckRecordOverrideAddressRecords(&record, true),
),
},
// Plan again to detect "loop" conditions
{
Config: tfFileOverrideAddressRecordsAliasTrue,
PlanOnly: true,
ExpectNonEmptyPlan: false,
},
{
Config: tfFileBasicAlias,
PlanOnly: true,
ExpectNonEmptyPlan: true,
},
// Change override_address_records from true setting to "null" results in false
{
Config: tfFileBasicAlias,
Check: resource.ComposeTestCheckFunc(
testAccCheckRecordExists("ns1_record.it", &record),
testAccCheckRecordOverrideAddressRecords(&record, false),
),
},
// Plan again to detect "loop" conditions
{
Config: tfFileBasicAlias,
PlanOnly: true,
ExpectNonEmptyPlan: false,
},
},
})
}

func TestAccRecord_OverrideAddressRecordsTrueToFalse(t *testing.T) {
var record dns.Record
rString := acctest.RandStringFromCharSet(15, acctest.CharSetAlphaNum)

tfFileOverrideAddressRecordsAliasTrue := testAccRecordBasicALIASOverrideAddressRecords(rString, true)
tfFileDoNotOverrideAddressRecordsAliasFalse := testAccRecordBasicALIASOverrideAddressRecords(rString, false)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckRecordDestroy,
Steps: []resource.TestStep{
// Create an ALIAS record with override_address_records true
{
Config: tfFileOverrideAddressRecordsAliasTrue,
Check: resource.ComposeTestCheckFunc(
testAccCheckRecordExists("ns1_record.it", &record),
testAccCheckRecordOverrideAddressRecords(&record, true),
),
},
// Plan again to detect "loop" conditions
{
Config: tfFileOverrideAddressRecordsAliasTrue,
PlanOnly: true,
ExpectNonEmptyPlan: false,
},
// Change override_address_records to false setting to false Plan and Apply
{
Config: tfFileDoNotOverrideAddressRecordsAliasFalse,
ExpectNonEmptyPlan: true,
PlanOnly: true,
},
{
Config: tfFileDoNotOverrideAddressRecordsAliasFalse,
Check: resource.ComposeTestCheckFunc(
testAccCheckRecordExists("ns1_record.it", &record),
testAccCheckRecordOverrideAddressRecords(&record, false),
),
},
// Plan again to detect "loop" conditions
{
Config: tfFileDoNotOverrideAddressRecordsAliasFalse,
PlanOnly: true,
ExpectNonEmptyPlan: false,
},
},
})
}

func TestAccRecord_Link(t *testing.T) {
var record1 dns.Record
var record2 dns.Record
Expand Down Expand Up @@ -1049,6 +1196,16 @@ func ExpectOverrideTTLNotNil() bool {
return false
}

func testAccCheckRecordOverrideAddressRecords(r *dns.Record, expected bool) resource.TestCheckFunc {
return func(s *terraform.State) error {
if *r.OverrideAddressRecords != expected {
return fmt.Errorf("Override Address Records: got: %#v want: %#v", *r.OverrideAddressRecords, expected)
}

return nil
}
}

func testAccCheckRecordOverrideTTL(r *dns.Record, expectedNil bool) resource.TestCheckFunc {
return func(s *terraform.State) error {
if expectedNil {
Expand Down Expand Up @@ -1273,6 +1430,25 @@ resource "ns1_zone" "test" {
`, domain, zone)
}

func testAccRecordBasicALIASOverrideAddressRecords(rString string, overrideAddressRecords bool) string {
return fmt.Sprintf(`
resource "ns1_record" "it" {
zone = "${ns1_zone.test.zone}"
domain = "${ns1_zone.test.zone}"
type = "ALIAS"
ttl = 60
override_address_records = %v
answers {
answer = "test.${ns1_zone.test.zone}"
}
}

resource "ns1_zone" "test" {
zone = "terraform-test-%s.io"
}
`, overrideAddressRecords, rString)
}

func testAccRecordBasicALIASOverrideTTL(rString string, overridettl bool) string {
return fmt.Sprintf(`
resource "ns1_record" "it" {
Expand Down
Loading