Skip to content

Commit 404151e

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

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

internal/services/domain/helpers.go

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

644644
return fmt.Sprintf("%s.%s", subdomain, domain)
645645
}
646+
647+
// normalizeRecordName extracts relative name from FQDN or returns the name as-is
648+
// Example: "_dmarc.example.com." with dnsZone "example.com" returns "_dmarc"
649+
// Example: "_dmarc" with dnsZone "example.com" returns "_dmarc"
650+
func normalizeRecordName(name, dnsZone string) string {
651+
if name == "" || name == "@" {
652+
return ""
653+
}
654+
655+
// Remove trailing dot
656+
name = strings.TrimSuffix(name, ".")
657+
dnsZone = strings.TrimSuffix(dnsZone, ".")
658+
659+
// If name equals dns zone, it's root zone
660+
if name == dnsZone {
661+
return ""
662+
}
663+
664+
// Remove domain suffix if present
665+
suffix := "." + dnsZone
666+
if strings.HasSuffix(name, suffix) {
667+
return strings.TrimSuffix(name, suffix)
668+
}
669+
670+
return name
671+
}

internal/services/domain/record.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,15 @@ 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+
// Normalize both values by removing trailing dots and domain suffix
80+
normalizedOld := normalizeRecordName(oldValue, dnsZone)
81+
normalizedNew := normalizeRecordName(newValue, dnsZone)
82+
83+
return normalizedOld == normalizedNew
84+
},
7685
},
7786
"type": {
7887
Type: schema.TypeString,

internal/services/domain/record_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -910,3 +910,51 @@ func testAccCheckDomainRecordDestroy(tt *acctest.TestTools) resource.TestCheckFu
910910
return nil
911911
}
912912
}
913+
914+
func TestAccDomainRecord_NameDiffSuppress(t *testing.T) {
915+
tt := acctest.NewTestTools(t)
916+
defer tt.Cleanup()
917+
918+
testDNSZone := "test-name-diff." + acctest.TestDomain
919+
logging.L.Debugf("TestAccDomainRecord_NameDiffSuppress: test dns zone: %s", testDNSZone)
920+
921+
resource.ParallelTest(t, resource.TestCase{
922+
PreCheck: func() { acctest.PreCheck(t) },
923+
ProtoV6ProviderFactories: tt.ProviderFactories,
924+
CheckDestroy: testAccCheckDomainRecordDestroy(tt),
925+
Steps: []resource.TestStep{
926+
{
927+
Config: fmt.Sprintf(`
928+
resource "scaleway_domain_record" "dmarc" {
929+
dns_zone = "%s"
930+
name = "_dmarc"
931+
type = "TXT"
932+
data = "v=DMARC1; p=none"
933+
}
934+
`, testDNSZone),
935+
Check: resource.ComposeTestCheckFunc(
936+
testAccCheckDomainRecordExists(tt, "scaleway_domain_record.dmarc"),
937+
resource.TestCheckResourceAttr("scaleway_domain_record.dmarc", "name", "_dmarc"),
938+
resource.TestCheckResourceAttr("scaleway_domain_record.dmarc", "type", "TXT"),
939+
resource.TestCheckResourceAttr("scaleway_domain_record.dmarc", "data", "v=DMARC1; p=none"),
940+
),
941+
},
942+
{
943+
// Use FQDN format - should not cause replacement
944+
Config: fmt.Sprintf(`
945+
resource "scaleway_domain_record" "dmarc" {
946+
dns_zone = "%s"
947+
name = "_dmarc.%s."
948+
type = "TXT"
949+
data = "v=DMARC1; p=none"
950+
}
951+
`, testDNSZone, testDNSZone),
952+
Check: resource.ComposeTestCheckFunc(
953+
testAccCheckDomainRecordExists(tt, "scaleway_domain_record.dmarc"),
954+
resource.TestCheckResourceAttr("scaleway_domain_record.dmarc", "name", "_dmarc"),
955+
resource.TestCheckResourceAttr("scaleway_domain_record.dmarc", "type", "TXT"),
956+
),
957+
},
958+
},
959+
})
960+
}

0 commit comments

Comments
 (0)