Skip to content

Commit 4fde9b4

Browse files
committed
feat: add test for TEM domain integration with domain records (issue #3407)
1 parent 261e5be commit 4fde9b4

File tree

3 files changed

+2225
-7
lines changed

3 files changed

+2225
-7
lines changed

internal/services/domain/record.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,10 @@ func ResourceRecord() *schema.Resource {
6666
ForceNew: true,
6767
Optional: true,
6868
StateFunc: func(val any) string {
69+
// This function normalizes the name before storing it in state
70+
// It converts FQDN (e.g., "_dmarc.example.com.") to relative format (e.g., "_dmarc")
6971
value := val.(string)
70-
if value == "@" {
72+
if value == "@" || value == "" {
7173
return ""
7274
}
7375

@@ -257,9 +259,11 @@ func resourceRecordCreate(ctx context.Context, d *schema.ResourceData, m any) di
257259
geoIP, okGeoIP := d.GetOk("geo_ip")
258260
recordType := domain.RecordType(d.Get("type").(string))
259261
recordData := d.Get("data").(string)
262+
// Normalize the record name to relative format (handles FQDN with trailing dot)
263+
recordName := normalizeRecordName(d.Get("name").(string), dnsZone)
260264
record := &domain.Record{
261265
Data: recordData,
262-
Name: d.Get("name").(string),
266+
Name: recordName,
263267
TTL: uint32(d.Get("ttl").(int)),
264268
Type: recordType,
265269
Priority: uint32(d.Get("priority").(int)),
@@ -297,7 +301,7 @@ func resourceRecordCreate(ctx context.Context, d *schema.ResourceData, m any) di
297301

298302
dnsZoneData, err := domainAPI.ListDNSZoneRecords(&domain.ListDNSZoneRecordsRequest{
299303
DNSZone: dnsZone,
300-
Name: d.Get("name").(string),
304+
Name: recordName,
301305
Type: recordType,
302306
}, scw.WithAllPages(), scw.WithContext(ctx))
303307
if err != nil {
@@ -369,10 +373,12 @@ func resourceDomainRecordRead(ctx context.Context, d *schema.ResourceData, m any
369373
}
370374

371375
idRecord := locality.ExpandID(d.Id())
376+
// Normalize the record name to relative format (handles FQDN with trailing dot)
377+
recordName := normalizeRecordName(d.Get("name").(string), dnsZone)
372378

373379
res, err := domainAPI.ListDNSZoneRecords(&domain.ListDNSZoneRecordsRequest{
374380
DNSZone: dnsZone,
375-
Name: d.Get("name").(string),
381+
Name: recordName,
376382
Type: recordType,
377383
ID: &idRecord,
378384
}, scw.WithAllPages(), scw.WithContext(ctx))
@@ -441,14 +447,17 @@ func resourceDomainRecordUpdate(ctx context.Context, d *schema.ResourceData, m a
441447

442448
domainAPI := NewDomainAPI(m)
443449

450+
dnsZone := d.Get("dns_zone").(string)
444451
req := &domain.UpdateDNSZoneRecordsRequest{
445-
DNSZone: d.Get("dns_zone").(string),
452+
DNSZone: dnsZone,
446453
ReturnAllRecords: scw.BoolPtr(false),
447454
}
448455

449456
geoIP, okGeoIP := d.GetOk("geo_ip")
457+
// Normalize the record name to relative format (handles FQDN with trailing dot)
458+
recordName := normalizeRecordName(d.Get("name").(string), dnsZone)
450459
record := &domain.Record{
451-
Name: d.Get("name").(string),
460+
Name: recordName,
452461
Data: d.Get("data").(string),
453462
Priority: uint32(d.Get("priority").(int)),
454463
TTL: uint32(d.Get("ttl").(int)),
@@ -473,7 +482,7 @@ func resourceDomainRecordUpdate(ctx context.Context, d *schema.ResourceData, m a
473482
return diag.FromErr(err)
474483
}
475484

476-
_, err = waitForDNSRecordExist(ctx, domainAPI, d.Get("dns_zone").(string), record.Name, record.Type, d.Timeout(schema.TimeoutUpdate))
485+
_, err = waitForDNSRecordExist(ctx, domainAPI, dnsZone, record.Name, record.Type, d.Timeout(schema.TimeoutUpdate))
477486
if err != nil {
478487
return diag.FromErr(err)
479488
}

internal/services/domain/record_test.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,95 @@ func TestAccDomainRecord_NameDiffSuppress(t *testing.T) {
924924
})
925925
}
926926

927+
func TestAccDomainRecord_TEMIntegration(t *testing.T) {
928+
tt := acctest.NewTestTools(t)
929+
defer tt.Cleanup()
930+
931+
testDNSZone := "test-tem-integration." + acctest.TestDomain
932+
logging.L.Debugf("TestAccDomainRecord_TEMIntegration: test dns zone: %s", testDNSZone)
933+
934+
resource.ParallelTest(t, resource.TestCase{
935+
PreCheck: func() { acctest.PreCheck(t) },
936+
ProtoV6ProviderFactories: tt.ProviderFactories,
937+
CheckDestroy: testAccCheckDomainRecordDestroy(tt),
938+
Steps: []resource.TestStep{
939+
{
940+
941+
Config: fmt.Sprintf(`
942+
resource "scaleway_domain_record" "dmarc" {
943+
dns_zone = "%s"
944+
name = "_dmarc.%s."
945+
type = "TXT"
946+
data = "v=DMARC1; p=none"
947+
}
948+
949+
resource "scaleway_domain_record" "dkim" {
950+
dns_zone = "%s"
951+
name = "scw1._domainkey.%s."
952+
type = "TXT"
953+
data = "v=DKIM1; k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQ"
954+
}
955+
`, testDNSZone, testDNSZone, testDNSZone, testDNSZone),
956+
Check: resource.ComposeTestCheckFunc(
957+
testAccCheckDomainRecordExists(tt, "scaleway_domain_record.dmarc"),
958+
testAccCheckDomainRecordExists(tt, "scaleway_domain_record.dkim"),
959+
resource.TestCheckResourceAttr("scaleway_domain_record.dmarc", "name", "_dmarc"),
960+
resource.TestCheckResourceAttr("scaleway_domain_record.dkim", "name", "scw1._domainkey"),
961+
resource.TestCheckResourceAttr("scaleway_domain_record.dmarc", "type", "TXT"),
962+
resource.TestCheckResourceAttr("scaleway_domain_record.dkim", "type", "TXT"),
963+
),
964+
},
965+
{
966+
Config: fmt.Sprintf(`
967+
resource "scaleway_domain_record" "dmarc" {
968+
dns_zone = "%s"
969+
name = "_dmarc.%s."
970+
type = "TXT"
971+
data = "v=DMARC1; p=none"
972+
}
973+
974+
resource "scaleway_domain_record" "dkim" {
975+
dns_zone = "%s"
976+
name = "scw1._domainkey.%s."
977+
type = "TXT"
978+
data = "v=DKIM1; k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQ"
979+
}
980+
`, testDNSZone, testDNSZone, testDNSZone, testDNSZone),
981+
Check: resource.ComposeTestCheckFunc(
982+
testAccCheckDomainRecordExists(tt, "scaleway_domain_record.dmarc"),
983+
testAccCheckDomainRecordExists(tt, "scaleway_domain_record.dkim"),
984+
resource.TestCheckResourceAttr("scaleway_domain_record.dmarc", "name", "_dmarc"),
985+
resource.TestCheckResourceAttr("scaleway_domain_record.dkim", "name", "scw1._domainkey"),
986+
),
987+
},
988+
{
989+
Config: fmt.Sprintf(`
990+
resource "scaleway_domain_record" "dmarc" {
991+
dns_zone = "%s"
992+
name = "_dmarc"
993+
type = "TXT"
994+
data = "v=DMARC1; p=quarantine"
995+
}
996+
997+
resource "scaleway_domain_record" "dkim" {
998+
dns_zone = "%s"
999+
name = "scw1._domainkey.%s."
1000+
type = "TXT"
1001+
data = "v=DKIM1; k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQ"
1002+
}
1003+
`, testDNSZone, testDNSZone, testDNSZone),
1004+
Check: resource.ComposeTestCheckFunc(
1005+
testAccCheckDomainRecordExists(tt, "scaleway_domain_record.dmarc"),
1006+
testAccCheckDomainRecordExists(tt, "scaleway_domain_record.dkim"),
1007+
resource.TestCheckResourceAttr("scaleway_domain_record.dmarc", "name", "_dmarc"),
1008+
resource.TestCheckResourceAttr("scaleway_domain_record.dmarc", "data", "v=DMARC1; p=quarantine"),
1009+
resource.TestCheckResourceAttr("scaleway_domain_record.dkim", "name", "scw1._domainkey"),
1010+
),
1011+
},
1012+
},
1013+
})
1014+
}
1015+
9271016
func testAccCheckDomainRecordDestroy(tt *acctest.TestTools) resource.TestCheckFunc {
9281017
return func(state *terraform.State) error {
9291018
for _, rs := range state.RootModule().Resources {

0 commit comments

Comments
 (0)