Skip to content

Commit 4fd6475

Browse files
committed
fix(domain): fix SRV domain duplication bug
1 parent d888258 commit 4fd6475

File tree

6 files changed

+4572
-11
lines changed

6 files changed

+4572
-11
lines changed

internal/services/domain/helpers.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,22 @@ func getRecordFromTypeAndData(dnsType domain.RecordType, data string, records []
3535
flattedData := flattenDomainData(strings.ToLower(r.Data), r.Type).(string)
3636
flattenCurrentData := flattenDomainData(strings.ToLower(data), r.Type).(string)
3737

38-
if strings.HasPrefix(flattedData, flattenCurrentData) && r.Type == dnsType {
39-
if currentRecord != nil {
40-
return nil, errors.New("multiple records found with same type and data")
38+
if dnsType == domain.RecordTypeSRV {
39+
if flattedData == flattenCurrentData {
40+
if currentRecord != nil {
41+
return nil, errors.New("multiple records found with same type and data")
42+
}
43+
currentRecord = r
44+
break
45+
}
46+
} else {
47+
if strings.HasPrefix(flattedData, flattenCurrentData) && r.Type == dnsType {
48+
if currentRecord != nil {
49+
return nil, errors.New("multiple records found with same type and data")
50+
}
51+
currentRecord = r
52+
break
4153
}
42-
43-
currentRecord = r
44-
45-
break
4654
}
4755
}
4856

internal/services/domain/record_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,72 @@ func TestAccDomainRecord_SRVZone(t *testing.T) {
692692
})
693693
}
694694

695+
func TestAccDomainRecord_SRVWithDomainDuplication(t *testing.T) {
696+
tt := acctest.NewTestTools(t)
697+
defer tt.Cleanup()
698+
699+
testDNSZone := "test-srv-duplication." + acctest.TestDomain
700+
logging.L.Debugf("TestAccDomainRecord_SRVWithDomainDuplication: test dns zone: %s", testDNSZone)
701+
702+
name := "_test_srv_bug"
703+
recordType := "SRV"
704+
data := "0 0 1234 foo.example.com"
705+
ttl := 60
706+
priority := 0
707+
708+
resource.ParallelTest(t, resource.TestCase{
709+
PreCheck: func() { acctest.PreCheck(t) },
710+
ProviderFactories: tt.ProviderFactories,
711+
CheckDestroy: testAccCheckDomainRecordDestroy(tt),
712+
Steps: []resource.TestStep{
713+
{
714+
Config: fmt.Sprintf(`
715+
resource "scaleway_domain_record" "srv_test" {
716+
dns_zone = "%[1]s"
717+
name = "%[2]s"
718+
type = "%[3]s"
719+
data = "%[4]s"
720+
priority = %[5]d
721+
ttl = %[6]d
722+
}
723+
`, testDNSZone, name, recordType, data, priority, ttl),
724+
Check: resource.ComposeTestCheckFunc(
725+
testAccCheckDomainRecordExists(tt, "scaleway_domain_record.srv_test"),
726+
resource.TestCheckResourceAttr("scaleway_domain_record.srv_test", "dns_zone", testDNSZone),
727+
resource.TestCheckResourceAttr("scaleway_domain_record.srv_test", "name", name),
728+
resource.TestCheckResourceAttr("scaleway_domain_record.srv_test", "type", recordType),
729+
resource.TestCheckResourceAttr("scaleway_domain_record.srv_test", "data", "0 0 1234 foo.example.com"),
730+
resource.TestCheckResourceAttr("scaleway_domain_record.srv_test", "ttl", strconv.Itoa(ttl)),
731+
resource.TestCheckResourceAttr("scaleway_domain_record.srv_test", "priority", strconv.Itoa(priority)),
732+
acctest.CheckResourceAttrUUID("scaleway_domain_record.srv_test", "id"),
733+
),
734+
},
735+
{
736+
Config: fmt.Sprintf(`
737+
resource "scaleway_domain_record" "srv_test" {
738+
dns_zone = "%[1]s"
739+
name = "%[2]s"
740+
type = "%[3]s"
741+
data = "10 0 5678 bar.example.com"
742+
priority = 10
743+
ttl = 300
744+
}
745+
`, testDNSZone, name, recordType),
746+
Check: resource.ComposeTestCheckFunc(
747+
testAccCheckDomainRecordExists(tt, "scaleway_domain_record.srv_test"),
748+
resource.TestCheckResourceAttr("scaleway_domain_record.srv_test", "dns_zone", testDNSZone),
749+
resource.TestCheckResourceAttr("scaleway_domain_record.srv_test", "name", name),
750+
resource.TestCheckResourceAttr("scaleway_domain_record.srv_test", "type", recordType),
751+
resource.TestCheckResourceAttr("scaleway_domain_record.srv_test", "data", "10 0 5678 bar.example.com"),
752+
resource.TestCheckResourceAttr("scaleway_domain_record.srv_test", "ttl", "300"),
753+
resource.TestCheckResourceAttr("scaleway_domain_record.srv_test", "priority", "10"),
754+
acctest.CheckResourceAttrUUID("scaleway_domain_record.srv_test", "id"),
755+
),
756+
},
757+
},
758+
})
759+
}
760+
695761
func testAccCheckDomainRecordExists(tt *acctest.TestTools, n string) resource.TestCheckFunc {
696762
return func(s *terraform.State) error {
697763
rs, ok := s.RootModule().Resources[n]

0 commit comments

Comments
 (0)