Skip to content

Commit 7b3c2e5

Browse files
committed
implemented libdns NS/TXT/CName/Address types
these are the types that do not require further parsing
1 parent e55e164 commit 7b3c2e5

File tree

1 file changed

+52
-21
lines changed

1 file changed

+52
-21
lines changed

client.go

Lines changed: 52 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"fmt"
77
"log"
8+
"net/netip"
89
"strings"
910
"time"
1011

@@ -110,13 +111,40 @@ func parseRecordSet(set types.ResourceRecordSet) []libdns.Record {
110111
row = strings.Join(parts, "")
111112
row = unquote(row)
112113
rows[i] = row
113-
records = append(records, libdns.RR{
114-
Name: *set.Name,
115-
Data: row,
116-
Type: rtype,
117-
TTL: time.Duration(ttl) * time.Second,
118-
})
114+
switch rtype {
115+
case "TXT":
116+
records = append(records, libdns.TXT{
117+
Name: *set.Name,
118+
Text: row,
119+
TTL: time.Duration(ttl) * time.Second,
120+
})
121+
case "SPF":
122+
records = append(records, libdns.RR{
123+
Name: *set.Name,
124+
Data: row,
125+
Type: rtype,
126+
TTL: time.Duration(ttl) * time.Second,
127+
})
128+
}
119129
}
130+
case "A", "AAAA":
131+
records = append(records, libdns.Address{
132+
Name: *set.Name,
133+
IP: netip.MustParseAddr(value),
134+
TTL: time.Duration(ttl) * time.Second,
135+
})
136+
case "CNAME":
137+
records = append(records, libdns.CNAME{
138+
Name: *set.Name,
139+
Target: value,
140+
TTL: time.Duration(ttl) * time.Second,
141+
})
142+
case "NS":
143+
records = append(records, libdns.NS{
144+
Name: *set.Name,
145+
Target: value,
146+
TTL: time.Duration(ttl) * time.Second,
147+
})
120148
default:
121149
records = append(records, libdns.RR{
122150
Name: *set.Name,
@@ -290,14 +318,15 @@ func (p *Provider) createRecord(ctx context.Context, zoneID string, record libdn
290318
func (p *Provider) updateRecord(ctx context.Context, zoneID string, record libdns.Record, zone string) (libdns.Record, error) {
291319
resourceRecords := make([]types.ResourceRecord, 0)
292320
// AWS Route53 TXT record value must be enclosed in quotation marks on update
293-
if record.RR().Type == "TXT" {
294-
txtRecords, err := p.getTxtRecordsFor(ctx, zoneID, zone, record.RR().Name)
321+
switch rr := record.(type) {
322+
case libdns.TXT:
323+
txtRecords, err := p.getTxtRecordsFor(ctx, zoneID, zone, rr.Name)
295324
if err != nil {
296325
return record, err
297326
}
298327
for _, r := range txtRecords {
299-
if record.RR().Data != r.Data {
300-
resourceRecords = append(resourceRecords, marshalRecord(r)...)
328+
if rr.Text != r.RR().Data {
329+
resourceRecords = append(resourceRecords, marshalRecord(r.RR())...)
301330
}
302331
}
303332
}
@@ -332,8 +361,9 @@ func (p *Provider) deleteRecord(ctx context.Context, zoneID string, record libdn
332361
action := types.ChangeActionDelete
333362
resourceRecords := make([]types.ResourceRecord, 0)
334363
// AWS Route53 TXT record value must be enclosed in quotation marks on update
335-
if record.RR().Type == "TXT" {
336-
txtRecords, err := p.getTxtRecordsFor(ctx, zoneID, zone, record.RR().Name)
364+
switch rr := record.(type) {
365+
case libdns.TXT:
366+
txtRecords, err := p.getTxtRecordsFor(ctx, zoneID, zone, rr.Name)
337367
if err != nil {
338368
return record, err
339369
}
@@ -347,8 +377,8 @@ func (p *Provider) deleteRecord(ctx context.Context, zoneID string, record libdn
347377
action = types.ChangeActionUpsert
348378
resourceRecords = make([]types.ResourceRecord, 0)
349379
for _, r := range txtRecords {
350-
if record.RR().Data != r.Data {
351-
resourceRecords = append(resourceRecords, marshalRecord(r)...)
380+
if record.RR().Data != r.RR().Data {
381+
resourceRecords = append(resourceRecords, marshalRecord(r.RR())...)
352382
}
353383
}
354384
}
@@ -406,28 +436,29 @@ func (p *Provider) applyChange(ctx context.Context, input *r53.ChangeResourceRec
406436
return nil
407437
}
408438

409-
func (p *Provider) getTxtRecords(ctx context.Context, zoneID string, zone string) ([]libdns.RR, error) {
410-
txtRecords := make([]libdns.RR, 0)
439+
func (p *Provider) getTxtRecords(ctx context.Context, zoneID string, zone string) ([]libdns.TXT, error) {
440+
txtRecords := make([]libdns.TXT, 0)
411441
records, err := p.getRecords(ctx, zoneID, zone)
412442
if err != nil {
413443
return nil, err
414444
}
415445
for _, r := range records {
416-
if r.RR().Type == "TXT" {
417-
txtRecords = append(txtRecords, r.RR())
446+
switch rr := r.(type) {
447+
case libdns.TXT:
448+
txtRecords = append(txtRecords, rr)
418449
}
419450
}
420451
return txtRecords, nil
421452
}
422453

423-
func (p *Provider) getTxtRecordsFor(ctx context.Context, zoneID string, zone string, name string) ([]libdns.RR, error) {
454+
func (p *Provider) getTxtRecordsFor(ctx context.Context, zoneID string, zone string, name string) ([]libdns.TXT, error) {
424455
txtRecords, err := p.getTxtRecords(ctx, zoneID, zone)
425456
if err != nil {
426457
return nil, err
427458
}
428-
records := make([]libdns.RR, 0)
459+
records := make([]libdns.TXT, 0)
429460
for _, r := range txtRecords {
430-
if libdns.AbsoluteName(name, zone) == r.Name {
461+
if libdns.AbsoluteName(name, zone) == r.RR().Name {
431462
records = append(records, r)
432463
}
433464
}

0 commit comments

Comments
 (0)