Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 34 additions & 15 deletions provider/cloudflare/cloudflare.go
Original file line number Diff line number Diff line change
Expand Up @@ -780,29 +780,48 @@ func (p *CloudFlareProvider) newCloudFlareChange(action changeAction, ep *endpoi
}

priority := (*uint16)(nil)
var data map[string]interface{}

if ep.RecordType == "MX" {
mxRecord, err := endpoint.NewMXRecord(target)
if err != nil {
return &cloudFlareChange{}, fmt.Errorf("failed to parse MX record target %q: %w", target, err)
} else {
priority = mxRecord.GetPriority()
target = *mxRecord.GetHost()
}
priority = mxRecord.GetPriority()
target = *mxRecord.GetHost()
} else if ep.RecordType == "SRV" {
parts := strings.Fields(target)
if len(parts) >= 4 {
priorityVal, _ := strconv.Atoi(parts[0])
weight, _ := strconv.Atoi(parts[1])
port, _ := strconv.Atoi(parts[2])
targetHost := strings.Join(parts[3:], " ")
data = map[string]interface{}{
"priority": priorityVal,
"weight": weight,
"port": port,
"target": targetHost,
}
}
}

record := cloudflare.DNSRecord{
Name: ep.DNSName,
TTL: ttl,
Proxied: &proxied,
Type: ep.RecordType,
Content: target,
Comment: comment,
Priority: priority,
}

if data != nil {
record.Data = data
}

return &cloudFlareChange{
Action: action,
ResourceRecord: cloudflare.DNSRecord{
Name: ep.DNSName,
TTL: ttl,
// We have to use pointers to bools now, as the upstream cloudflare-go library requires them
// see: https://github.com/cloudflare/cloudflare-go/pull/595
Proxied: &proxied,
Type: ep.RecordType,
Content: target,
Comment: comment,
Priority: priority,
},
Action: action,
ResourceRecord: record,
RegionalHostname: p.regionalHostname(ep),
CustomHostnamesPrev: prevCustomHostnames,
CustomHostnames: newCustomHostnames,
Expand Down
2 changes: 0 additions & 2 deletions provider/cloudflare/cloudflare_regional_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,6 @@ func TestRecordsWithListRegionalHostnameFaillure(t *testing.T) {
}

func TestApplyChangesWithRegionalHostnamesFaillures(t *testing.T) {
t.Parallel()
type fields struct {
Records map[string]cloudflare.DNSRecord
RegionalHostnames []cloudflare.RegionalHostname
Expand Down Expand Up @@ -1031,7 +1030,6 @@ func TestApplyChangesWithRegionalHostnamesFaillures(t *testing.T) {
}

func TestApplyChangesWithRegionalHostnamesDryRun(t *testing.T) {
t.Parallel()
type fields struct {
Records map[string]cloudflare.DNSRecord
RegionalHostnames []cloudflare.RegionalHostname
Expand Down
72 changes: 61 additions & 11 deletions provider/cloudflare/cloudflare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"os"
"slices"
"sort"
"strconv"
"strings"
"testing"

Expand Down Expand Up @@ -134,6 +135,12 @@ func getDNSRecordFromRecordParams(rp any) cloudflare.DNSRecord {
if params.Type == "MX" {
record.Priority = params.Priority
}
if params.Data != nil {
record.Data = params.Data
}
if params.Type == "SRV" && record.Data != nil {
record.Content = ""
}
return record
case cloudflare.UpdateDNSRecordParams:
record := cloudflare.DNSRecord{
Expand All @@ -147,6 +154,12 @@ func getDNSRecordFromRecordParams(rp any) cloudflare.DNSRecord {
if params.Type == "MX" {
record.Priority = params.Priority
}
if params.Data != nil {
record.Data = params.Data
}
if params.Type == "SRV" && record.Data != nil {
record.Content = ""
}
return record
default:
return cloudflare.DNSRecord{}
Expand All @@ -162,20 +175,45 @@ func (m *mockCloudFlareClient) CreateDNSRecord(ctx context.Context, rc *cloudfla
if recordData.ID == "" {
recordData.ID = generateDNSRecordID(recordData.Type, recordData.Name, recordData.Content)
}
m.Actions = append(m.Actions, MockAction{

if recordData.Type == "SRV" {
if rp.Data != nil {
recordData.Data = rp.Data
recordData.Content = ""
} else if recordData.Data == nil && recordData.Content != "" {
parts := strings.Fields(recordData.Content)
if len(parts) >= 4 {
priority, _ := strconv.Atoi(parts[0])
weight, _ := strconv.Atoi(parts[1])
port, _ := strconv.Atoi(parts[2])
target := strings.Join(parts[3:], " ")
recordData.Data = map[string]interface{}{
"priority": priority,
"weight": weight,
"port": port,
"target": target,
}
recordData.Content = ""
}
Comment on lines +184 to +197
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wdyt about moving this part into a dedicated func ?
That would ease maintenance and readability.

}
}

action := MockAction{
Name: "Create",
ZoneId: rc.Identifier,
RecordId: recordData.ID,
RecordData: recordData,
})
}
m.Actions = append(m.Actions, action)

if zone, ok := m.Records[rc.Identifier]; ok {
zone[recordData.ID] = recordData
}

if recordData.Name == "newerror.bar.com" {
return cloudflare.DNSRecord{}, fmt.Errorf("failed to create record")
}
return cloudflare.DNSRecord{}, nil
return recordData, nil
}

func (m *mockCloudFlareClient) ListDNSRecords(ctx context.Context, rc *cloudflare.ResourceContainer, rp cloudflare.ListDNSRecordsParams) ([]cloudflare.DNSRecord, *cloudflare.ResultInfo, error) {
Expand Down Expand Up @@ -780,6 +818,9 @@ func TestCloudflareSetProxied(t *testing.T) {
targets = endpoint.Targets{"10 mx.example.com"}
content = "mx.example.com"
priority = cloudflare.Uint16Ptr(10)
} else if testCase.recordType == "SRV" {
targets = endpoint.Targets{"10 5 8080 example.com"}
content = "10 5 8080 example.com"
} else {
targets = endpoint.Targets{"127.0.0.1"}
content = "127.0.0.1"
Expand All @@ -798,17 +839,30 @@ func TestCloudflareSetProxied(t *testing.T) {
},
},
}
expectedID := fmt.Sprintf("%s-%s-%s", testCase.domain, testCase.recordType, content)
// Generate the expected ID based on the record type and content
expectedID := generateDNSRecordID(testCase.recordType, testCase.domain, content)

recordData := cloudflare.DNSRecord{
ID: expectedID,
Type: testCase.recordType,
Name: testCase.domain,
Content: content,
TTL: 1,
Proxied: testCase.proxiable,
}

if testCase.recordType == "MX" {
recordData.Content = content
recordData.Priority = priority
} else if testCase.recordType == "SRV" {
recordData.Data = map[string]interface{}{
"priority": 10,
"weight": 5,
"port": 8080,
"target": "example.com",
}
recordData.Content = ""
} else {
recordData.Content = content
}
AssertActions(t, &CloudFlareProvider{}, endpoints, []MockAction{
{
Expand All @@ -817,7 +871,7 @@ func TestCloudflareSetProxied(t *testing.T) {
RecordId: expectedID,
RecordData: recordData,
},
}, []string{endpoint.RecordTypeA, endpoint.RecordTypeCNAME, endpoint.RecordTypeNS, endpoint.RecordTypeMX}, testCase.recordType+" record on "+testCase.domain)
}, []string{endpoint.RecordTypeA, endpoint.RecordTypeCNAME, endpoint.RecordTypeNS, endpoint.RecordTypeMX, endpoint.RecordTypeSRV}, testCase.recordType+" record on "+testCase.domain)
}
}

Expand Down Expand Up @@ -1226,11 +1280,6 @@ func TestCloudflareGetRecordID(t *testing.T) {
}

func TestCloudflareGroupByNameAndType(t *testing.T) {
provider := &CloudFlareProvider{
Client: NewMockCloudFlareClient(),
domainFilter: endpoint.NewDomainFilter([]string{"bar.com"}),
zoneIDFilter: provider.NewZoneIDFilter([]string{""}),
}
testCases := []struct {
Name string
Records []cloudflare.DNSRecord
Expand Down Expand Up @@ -1465,6 +1514,7 @@ func TestCloudflareGroupByNameAndType(t *testing.T) {
for _, r := range tc.Records {
records[newDNSRecordIndex(r)] = r
}
provider := &CloudFlareProvider{}
endpoints := provider.groupByNameAndTypeWithCustomHostnames(records, CustomHostnamesMap{})
// Targets order could be random with underlying map
for _, ep := range endpoints {
Expand Down
Loading