From 76c18636cd4c3a83491c6342ce8b6ac299178ce1 Mon Sep 17 00:00:00 2001 From: Alexandre Oliveira Date: Fri, 31 Mar 2023 16:15:31 +0200 Subject: [PATCH] Implement libdns ZoneLister interface for provider --- _example/main.go | 9 +++++++++ client.go | 32 ++++++++++++++++++++++++++++++++ provider.go | 11 +++++++++++ 3 files changed, 52 insertions(+) diff --git a/_example/main.go b/_example/main.go index 67239ec..6076a1d 100644 --- a/_example/main.go +++ b/_example/main.go @@ -25,6 +25,15 @@ func main() { provider := vultr.Provider{APIToken: token} + zones, err := provider.ListZones(context.TODO()) + if err != nil { + fmt.Printf("ERROR: %s\n", err.Error()) + } + + for _, zone := range zones { + fmt.Printf("%s\n", zone.Name) + } + records, err := provider.GetRecords(context.TODO(), zone) if err != nil { fmt.Printf("ERROR: %s\n", err.Error()) diff --git a/client.go b/client.go index 510ca28..c35b07b 100644 --- a/client.go +++ b/client.go @@ -121,3 +121,35 @@ func (p *Provider) updateDNSRecord(ctx context.Context, domain string, record li return record, nil } + +func (p *Provider) getDNSZones(ctx context.Context) ([]libdns.Zone, error) { + p.client.mutex.Lock() + defer p.client.mutex.Unlock() + + p.getClient() + + listOptions := &govultr.ListOptions{} + + var zones []libdns.Zone + for { + dns_zones, meta, _, err := p.client.vultr.Domain.List(ctx, listOptions) + if err != nil { + return zones, err + } + + for _, entry := range dns_zones { + zone := libdns.Zone{ + Name: entry.Domain, + } + zones = append(zones, zone) + } + + if meta.Links.Next == "" { + break + } + + listOptions.Cursor = meta.Links.Next + } + + return zones, nil +} diff --git a/provider.go b/provider.go index f4ae1ed..166f5da 100644 --- a/provider.go +++ b/provider.go @@ -83,10 +83,21 @@ func (p *Provider) SetRecords(ctx context.Context, zone string, records []libdns return setRecords, nil } +func (p *Provider) ListZones(ctx context.Context) ([]libdns.Zone, error) { + zones, err := p.getDNSZones(ctx) + + if err != nil { + return nil, err + } + + return zones, nil +} + // Interface guards var ( _ libdns.RecordGetter = (*Provider)(nil) _ libdns.RecordAppender = (*Provider)(nil) _ libdns.RecordSetter = (*Provider)(nil) _ libdns.RecordDeleter = (*Provider)(nil) + _ libdns.ZoneLister = (*Provider)(nil) )