Skip to content

Commit 06a0eac

Browse files
committed
feat: add DiffSuppressFunc to scaleway_domain_record name attribute to accept FQDN and relative names
1 parent 3f5b763 commit 06a0eac

File tree

4 files changed

+821
-0
lines changed

4 files changed

+821
-0
lines changed

internal/services/domain/helpers.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,3 +643,24 @@ func BuildZoneName(subdomain, domain string) string {
643643

644644
return fmt.Sprintf("%s.%s", subdomain, domain)
645645
}
646+
647+
648+
func normalizeRecordName(name, dnsZone string) string {
649+
if name == "" || name == "@" {
650+
return ""
651+
}
652+
653+
name = strings.TrimSuffix(name, ".")
654+
dnsZone = strings.TrimSuffix(dnsZone, ".")
655+
656+
if name == dnsZone {
657+
return ""
658+
}
659+
660+
suffix := "." + dnsZone
661+
if strings.HasSuffix(name, suffix) {
662+
return strings.TrimSuffix(name, suffix)
663+
}
664+
665+
return name
666+
}

internal/services/domain/record.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ func ResourceRecord() *schema.Resource {
7373

7474
return value
7575
},
76+
DiffSuppressFunc: func(k, oldValue, newValue string, d *schema.ResourceData) bool {
77+
dnsZone := d.Get("dns_zone").(string)
78+
79+
normalizedOld := normalizeRecordName(oldValue, dnsZone)
80+
normalizedNew := normalizeRecordName(newValue, dnsZone)
81+
82+
return normalizedOld == normalizedNew
83+
},
7684
},
7785
"type": {
7886
Type: schema.TypeString,

internal/services/domain/record_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,54 @@ func TestAccDomainRecord_CNAME(t *testing.T) {
876876
})
877877
}
878878

879+
func TestAccDomainRecord_NameDiffSuppress(t *testing.T) {
880+
tt := acctest.NewTestTools(t)
881+
defer tt.Cleanup()
882+
883+
testDNSZone := "test-name-diff." + acctest.TestDomain
884+
logging.L.Debugf("TestAccDomainRecord_NameDiffSuppress: test dns zone: %s", testDNSZone)
885+
886+
resource.ParallelTest(t, resource.TestCase{
887+
PreCheck: func() { acctest.PreCheck(t) },
888+
ProtoV6ProviderFactories: tt.ProviderFactories,
889+
CheckDestroy: testAccCheckDomainRecordDestroy(tt),
890+
Steps: []resource.TestStep{
891+
{
892+
Config: fmt.Sprintf(`
893+
resource "scaleway_domain_record" "dmarc" {
894+
dns_zone = "%s"
895+
name = "_dmarc"
896+
type = "TXT"
897+
data = "v=DMARC1; p=none"
898+
}
899+
`, testDNSZone),
900+
Check: resource.ComposeTestCheckFunc(
901+
testAccCheckDomainRecordExists(tt, "scaleway_domain_record.dmarc"),
902+
resource.TestCheckResourceAttr("scaleway_domain_record.dmarc", "name", "_dmarc"),
903+
resource.TestCheckResourceAttr("scaleway_domain_record.dmarc", "type", "TXT"),
904+
resource.TestCheckResourceAttr("scaleway_domain_record.dmarc", "data", "v=DMARC1; p=none"),
905+
),
906+
},
907+
{
908+
// Use FQDN format - should not cause replacement
909+
Config: fmt.Sprintf(`
910+
resource "scaleway_domain_record" "dmarc" {
911+
dns_zone = "%s"
912+
name = "_dmarc.%s."
913+
type = "TXT"
914+
data = "v=DMARC1; p=none"
915+
}
916+
`, testDNSZone, testDNSZone),
917+
Check: resource.ComposeTestCheckFunc(
918+
testAccCheckDomainRecordExists(tt, "scaleway_domain_record.dmarc"),
919+
resource.TestCheckResourceAttr("scaleway_domain_record.dmarc", "name", "_dmarc"),
920+
resource.TestCheckResourceAttr("scaleway_domain_record.dmarc", "type", "TXT"),
921+
),
922+
},
923+
},
924+
})
925+
}
926+
879927
func testAccCheckDomainRecordDestroy(tt *acctest.TestTools) resource.TestCheckFunc {
880928
return func(state *terraform.State) error {
881929
for _, rs := range state.RootModule().Resources {
@@ -910,3 +958,5 @@ func testAccCheckDomainRecordDestroy(tt *acctest.TestTools) resource.TestCheckFu
910958
return nil
911959
}
912960
}
961+
962+

0 commit comments

Comments
 (0)