Skip to content

Commit 9d3ab3c

Browse files
committed
Better SRV support and errors (fix #7)
1 parent e5327b9 commit 9d3ab3c

File tree

5 files changed

+64
-21
lines changed

5 files changed

+64
-21
lines changed

client.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ import (
1212
)
1313

1414
func (p *Provider) createRecord(ctx context.Context, zoneInfo cfZone, record libdns.Record) (cfDNSRecord, error) {
15-
jsonBytes, err := json.Marshal(cloudflareRecord(record))
15+
cfRec, err := cloudflareRecord(record)
16+
if err != nil {
17+
return cfDNSRecord{}, err
18+
}
19+
jsonBytes, err := json.Marshal(cfRec)
1620
if err != nil {
1721
return cfDNSRecord{}, err
1822
}

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module github.com/libdns/cloudflare
22

3-
go 1.14
3+
go 1.18
44

5-
require github.com/libdns/libdns v0.2.0
5+
require github.com/libdns/libdns v0.2.2-0.20221006221142-3ef90aee33fd

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
github.com/libdns/libdns v0.2.0 h1:ewg3ByWrdUrxrje8ChPVMBNcotg7H9LQYg+u5De2RzI=
2-
github.com/libdns/libdns v0.2.0/go.mod h1:yQCXzk1lEZmmCPa857bnk4TsOiqYasqpyOEeSObbb40=
1+
github.com/libdns/libdns v0.2.2-0.20221006221142-3ef90aee33fd h1:SyZBFgMczGjPf5VIKgj3OqpvWPd4qsx6VTX5Bpe3GkU=
2+
github.com/libdns/libdns v0.2.2-0.20221006221142-3ef90aee33fd/go.mod h1:4Bj9+5CQiNMVGf87wjX4CY3HQJypUHRuLvlsfsZqLWQ=

models.go

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cloudflare
22

33
import (
44
"encoding/json"
5+
"strings"
56
"time"
67

78
"github.com/libdns/libdns"
@@ -61,7 +62,7 @@ type cfDNSRecord struct {
6162
ZoneName string `json:"zone_name,omitempty"`
6263
CreatedOn time.Time `json:"created_on,omitempty"`
6364
ModifiedOn time.Time `json:"modified_on,omitempty"`
64-
Data *struct {
65+
Data struct {
6566
// LOC
6667
LatDegrees int `json:"lat_degrees,omitempty"`
6768
LatMinutes int `json:"lat_minutes,omitempty"`
@@ -80,9 +81,9 @@ type cfDNSRecord struct {
8081
Service string `json:"service,omitempty"`
8182
Proto string `json:"proto,omitempty"`
8283
Name string `json:"name,omitempty"`
83-
Priority int `json:"priority,omitempty"`
84-
Weight int `json:"weight,omitempty"`
85-
Port int `json:"port,omitempty"`
84+
Priority uint `json:"priority,omitempty"`
85+
Weight uint `json:"weight,omitempty"`
86+
Port uint `json:"port,omitempty"`
8687
Target string `json:"target,omitempty"`
8788

8889
// DNSKEY
@@ -109,6 +110,18 @@ type cfDNSRecord struct {
109110
}
110111

111112
func (r cfDNSRecord) libdnsRecord(zone string) libdns.Record {
113+
if r.Type == "SRV" {
114+
srv := libdns.SRV{
115+
Service: strings.TrimPrefix(r.Data.Service, "_"),
116+
Proto: strings.TrimPrefix(r.Data.Proto, "_"),
117+
Name: r.Data.Name,
118+
Priority: r.Data.Priority,
119+
Weight: r.Data.Weight,
120+
Port: r.Data.Port,
121+
Target: r.Data.Target,
122+
}
123+
return srv.ToRecord()
124+
}
112125
return libdns.Record{
113126
Type: r.Type,
114127
Name: libdns.RelativeName(r.Name, zone),
@@ -118,25 +131,44 @@ func (r cfDNSRecord) libdnsRecord(zone string) libdns.Record {
118131
}
119132
}
120133

121-
func cloudflareRecord(r libdns.Record) cfDNSRecord {
122-
return cfDNSRecord{
123-
ID: r.ID,
124-
Type: r.Type,
125-
Name: r.Name,
126-
Content: r.Value,
127-
TTL: int(r.TTL.Seconds()),
134+
func cloudflareRecord(r libdns.Record) (cfDNSRecord, error) {
135+
rec := cfDNSRecord{
136+
ID: r.ID,
137+
Type: r.Type,
138+
TTL: int(r.TTL.Seconds()),
139+
}
140+
if r.Type == "SRV" {
141+
srv, err := r.ToSRV()
142+
if err != nil {
143+
return cfDNSRecord{}, err
144+
}
145+
rec.Data.Service = "_" + srv.Service
146+
rec.Data.Priority = srv.Priority
147+
rec.Data.Weight = srv.Weight
148+
rec.Data.Proto = "_" + srv.Proto
149+
rec.Data.Name = srv.Name
150+
rec.Data.Port = srv.Port
151+
rec.Data.Target = srv.Target
152+
} else {
153+
rec.Name = r.Name
154+
rec.Content = r.Value
128155
}
156+
return rec, nil
129157
}
130158

131159
// All API responses have this structure.
132160
type cfResponse struct {
133161
Result json.RawMessage `json:"result,omitempty"`
134162
Success bool `json:"success"`
135163
Errors []struct {
136-
Code int `json:"code"`
137-
Message string `json:"message"`
164+
Code int `json:"code"`
165+
Message string `json:"message"`
166+
ErrorChain []struct {
167+
Code int `json:"code"`
168+
Message string `json:"message"`
169+
} `json:"error_chain,omitempty"`
138170
} `json:"errors,omitempty"`
139-
Messages []interface{} `json:"messages,omitempty"`
171+
Messages []any `json:"messages,omitempty"`
140172
ResultInfo *cfResultInfo `json:"result_info,omitempty"`
141173
}
142174

provider.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,10 @@ func (p *Provider) SetRecords(ctx context.Context, zone string, records []libdns
131131

132132
var results []libdns.Record
133133
for _, rec := range records {
134-
oldRec := cloudflareRecord(rec)
134+
oldRec, err := cloudflareRecord(rec)
135+
if err != nil {
136+
return nil, err
137+
}
135138
oldRec.ZoneID = zoneInfo.ID
136139
if rec.ID == "" {
137140
// the record might already exist, even if we don't know the ID yet
@@ -155,7 +158,11 @@ func (p *Provider) SetRecords(ctx context.Context, zone string, records []libdns
155158
oldRec.ID = matches[0].ID
156159
}
157160
// record exists; update it
158-
result, err := p.updateRecord(ctx, oldRec, cloudflareRecord(rec))
161+
cfRec, err := cloudflareRecord(rec)
162+
if err != nil {
163+
return nil, err
164+
}
165+
result, err := p.updateRecord(ctx, oldRec, cfRec)
159166
if err != nil {
160167
return nil, err
161168
}

0 commit comments

Comments
 (0)