@@ -23,6 +23,7 @@ import (
23
23
"os"
24
24
"slices"
25
25
"sort"
26
+ "strconv"
26
27
"strings"
27
28
"testing"
28
29
@@ -134,6 +135,12 @@ func getDNSRecordFromRecordParams(rp any) cloudflare.DNSRecord {
134
135
if params .Type == "MX" {
135
136
record .Priority = params .Priority
136
137
}
138
+ if params .Data != nil {
139
+ record .Data = params .Data
140
+ }
141
+ if params .Type == "SRV" && record .Data != nil {
142
+ record .Content = ""
143
+ }
137
144
return record
138
145
case cloudflare.UpdateDNSRecordParams :
139
146
record := cloudflare.DNSRecord {
@@ -147,6 +154,12 @@ func getDNSRecordFromRecordParams(rp any) cloudflare.DNSRecord {
147
154
if params .Type == "MX" {
148
155
record .Priority = params .Priority
149
156
}
157
+ if params .Data != nil {
158
+ record .Data = params .Data
159
+ }
160
+ if params .Type == "SRV" && record .Data != nil {
161
+ record .Content = ""
162
+ }
150
163
return record
151
164
default :
152
165
return cloudflare.DNSRecord {}
@@ -162,20 +175,45 @@ func (m *mockCloudFlareClient) CreateDNSRecord(ctx context.Context, rc *cloudfla
162
175
if recordData .ID == "" {
163
176
recordData .ID = generateDNSRecordID (recordData .Type , recordData .Name , recordData .Content )
164
177
}
165
- m .Actions = append (m .Actions , MockAction {
178
+
179
+ if recordData .Type == "SRV" {
180
+ if rp .Data != nil {
181
+ recordData .Data = rp .Data
182
+ recordData .Content = ""
183
+ } else if recordData .Data == nil && recordData .Content != "" {
184
+ parts := strings .Fields (recordData .Content )
185
+ if len (parts ) >= 4 {
186
+ priority , _ := strconv .Atoi (parts [0 ])
187
+ weight , _ := strconv .Atoi (parts [1 ])
188
+ port , _ := strconv .Atoi (parts [2 ])
189
+ target := strings .Join (parts [3 :], " " )
190
+ recordData .Data = map [string ]interface {}{
191
+ "priority" : priority ,
192
+ "weight" : weight ,
193
+ "port" : port ,
194
+ "target" : target ,
195
+ }
196
+ recordData .Content = ""
197
+ }
198
+ }
199
+ }
200
+
201
+ action := MockAction {
166
202
Name : "Create" ,
167
203
ZoneId : rc .Identifier ,
168
204
RecordId : recordData .ID ,
169
205
RecordData : recordData ,
170
- })
206
+ }
207
+ m .Actions = append (m .Actions , action )
208
+
171
209
if zone , ok := m .Records [rc .Identifier ]; ok {
172
210
zone [recordData .ID ] = recordData
173
211
}
174
212
175
213
if recordData .Name == "newerror.bar.com" {
176
214
return cloudflare.DNSRecord {}, fmt .Errorf ("failed to create record" )
177
215
}
178
- return cloudflare. DNSRecord {} , nil
216
+ return recordData , nil
179
217
}
180
218
181
219
func (m * mockCloudFlareClient ) ListDNSRecords (ctx context.Context , rc * cloudflare.ResourceContainer , rp cloudflare.ListDNSRecordsParams ) ([]cloudflare.DNSRecord , * cloudflare.ResultInfo , error ) {
@@ -780,6 +818,9 @@ func TestCloudflareSetProxied(t *testing.T) {
780
818
targets = endpoint.Targets {"10 mx.example.com" }
781
819
content = "mx.example.com"
782
820
priority = cloudflare .Uint16Ptr (10 )
821
+ } else if testCase .recordType == "SRV" {
822
+ targets = endpoint.Targets {"10 5 8080 example.com" }
823
+ content = "10 5 8080 example.com"
783
824
} else {
784
825
targets = endpoint.Targets {"127.0.0.1" }
785
826
content = "127.0.0.1"
@@ -798,17 +839,30 @@ func TestCloudflareSetProxied(t *testing.T) {
798
839
},
799
840
},
800
841
}
801
- expectedID := fmt .Sprintf ("%s-%s-%s" , testCase .domain , testCase .recordType , content )
842
+ // Generate the expected ID based on the record type and content
843
+ expectedID := generateDNSRecordID (testCase .recordType , testCase .domain , content )
844
+
802
845
recordData := cloudflare.DNSRecord {
803
846
ID : expectedID ,
804
847
Type : testCase .recordType ,
805
848
Name : testCase .domain ,
806
- Content : content ,
807
849
TTL : 1 ,
808
850
Proxied : testCase .proxiable ,
809
851
}
852
+
810
853
if testCase .recordType == "MX" {
854
+ recordData .Content = content
811
855
recordData .Priority = priority
856
+ } else if testCase .recordType == "SRV" {
857
+ recordData .Data = map [string ]interface {}{
858
+ "priority" : 10 ,
859
+ "weight" : 5 ,
860
+ "port" : 8080 ,
861
+ "target" : "example.com" ,
862
+ }
863
+ recordData .Content = ""
864
+ } else {
865
+ recordData .Content = content
812
866
}
813
867
AssertActions (t , & CloudFlareProvider {}, endpoints , []MockAction {
814
868
{
@@ -817,7 +871,7 @@ func TestCloudflareSetProxied(t *testing.T) {
817
871
RecordId : expectedID ,
818
872
RecordData : recordData ,
819
873
},
820
- }, []string {endpoint .RecordTypeA , endpoint .RecordTypeCNAME , endpoint .RecordTypeNS , endpoint .RecordTypeMX }, testCase .recordType + " record on " + testCase .domain )
874
+ }, []string {endpoint .RecordTypeA , endpoint .RecordTypeCNAME , endpoint .RecordTypeNS , endpoint .RecordTypeMX , endpoint . RecordTypeSRV }, testCase .recordType + " record on " + testCase .domain )
821
875
}
822
876
}
823
877
@@ -1226,11 +1280,6 @@ func TestCloudflareGetRecordID(t *testing.T) {
1226
1280
}
1227
1281
1228
1282
func TestCloudflareGroupByNameAndType (t * testing.T ) {
1229
- provider := & CloudFlareProvider {
1230
- Client : NewMockCloudFlareClient (),
1231
- domainFilter : endpoint .NewDomainFilter ([]string {"bar.com" }),
1232
- zoneIDFilter : provider .NewZoneIDFilter ([]string {"" }),
1233
- }
1234
1283
testCases := []struct {
1235
1284
Name string
1236
1285
Records []cloudflare.DNSRecord
@@ -1465,6 +1514,7 @@ func TestCloudflareGroupByNameAndType(t *testing.T) {
1465
1514
for _ , r := range tc .Records {
1466
1515
records [newDNSRecordIndex (r )] = r
1467
1516
}
1517
+ provider := & CloudFlareProvider {}
1468
1518
endpoints := provider .groupByNameAndTypeWithCustomHostnames (records , CustomHostnamesMap {})
1469
1519
// Targets order could be random with underlying map
1470
1520
for _ , ep := range endpoints {
0 commit comments