Skip to content

Commit de70a1b

Browse files
committed
feat: add support for libdns ZoneLister
1 parent 3cac5c9 commit de70a1b

2 files changed

Lines changed: 85 additions & 0 deletions

File tree

provider.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,23 @@ func (p *Provider) DeleteRecords(ctx context.Context, zone string, records []lib
292292
return ret, nil
293293
}
294294

295+
// ListZones lists all the zones on an account.
296+
func (p *Provider) ListZones(ctx context.Context) ([]libdns.Zone, error) {
297+
// https://desec.readthedocs.io/en/latest/dns/domains.html#listing-domains
298+
desecZones, err := p.listZones(ctx)
299+
if err != nil {
300+
return nil, err
301+
}
302+
303+
zones := make([]libdns.Zone, len(desecZones))
304+
for i, zone := range desecZones {
305+
zones[i] = libdns.Zone{
306+
Name: zone.Name,
307+
}
308+
}
309+
return zones, nil
310+
}
311+
295312
// https://desec.readthedocs.io/en/latest/dns/rrsets.html#rrset-field-reference
296313
type rrSet struct {
297314
Subname string `json:"subname"`
@@ -538,6 +555,25 @@ func (p *Provider) listRRSets(ctx context.Context, zone string) ([]rrSet, error)
538555
return out, nil
539556
}
540557

558+
// https://desec.readthedocs.io/en/latest/dns/domains.html#domain-field-reference
559+
type domain struct {
560+
Name string `json:"name"`
561+
}
562+
563+
func (p *Provider) listZones(ctx context.Context) ([]domain, error) {
564+
// https://desec.readthedocs.io/en/latest/dns/domains.html#listing-domains
565+
buf, err := p.httpDo(ctx, "GET", "https://desec.io/api/v1/domains/", nil)
566+
if err != nil {
567+
return nil, err
568+
}
569+
570+
var out []domain
571+
if err := json.Unmarshal(buf, &out); err != nil {
572+
return nil, fmt.Errorf("decoding json: %v", err)
573+
}
574+
return out, nil
575+
}
576+
541577
func (p *Provider) putRRSets(ctx context.Context, zone string, rrs []rrSet) error {
542578
if len(rrs) == 0 {
543579
return nil
@@ -567,4 +603,5 @@ var (
567603
_ libdns.RecordAppender = (*Provider)(nil)
568604
_ libdns.RecordSetter = (*Provider)(nil)
569605
_ libdns.RecordDeleter = (*Provider)(nil)
606+
_ libdns.ZoneLister = (*Provider)(nil)
570607
)

provider_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,3 +693,51 @@ func TestDeleteRecords(t *testing.T) {
693693
t.Fatalf("p.GetRecords() unexpected diff [-want +got]: %s", diff)
694694
}
695695
}
696+
697+
func TestListZones(t *testing.T) {
698+
if *token == "" || *domain == "" {
699+
t.Skip("skipping integration test; both -token and -domain must be set")
700+
}
701+
702+
ctx := context.Background()
703+
if deadline, ok := t.Deadline(); ok {
704+
var cancel context.CancelFunc
705+
ctx, cancel = context.WithDeadline(ctx, deadline)
706+
t.Cleanup(cancel)
707+
}
708+
709+
p := &desec.Provider{
710+
Token: *token,
711+
}
712+
713+
testDomains := []string{
714+
*domain,
715+
"test1-" + *domain,
716+
"test2-" + *domain,
717+
}
718+
719+
for _, testDomain := range testDomains {
720+
if domainExists(ctx, t, testDomain) {
721+
t.Fatalf("domain %q exists, but it should not", testDomain)
722+
}
723+
createDomain(ctx, t, testDomain)
724+
domain := testDomain
725+
t.Cleanup(func() { deleteDomain(ctx, t, domain) })
726+
}
727+
728+
zones, err := p.ListZones(ctx)
729+
if err != nil {
730+
t.Fatal(err)
731+
}
732+
733+
zoneMap := make(map[string]bool)
734+
for _, zone := range zones {
735+
zoneMap[zone.Name] = true
736+
}
737+
738+
for _, testDomain := range testDomains {
739+
if !zoneMap[testDomain] {
740+
t.Errorf("Failed to find %s in ListZones call", testDomain)
741+
}
742+
}
743+
}

0 commit comments

Comments
 (0)