Implement libdns ZoneLister interface for provider

This commit is contained in:
Alexandre Oliveira 2023-03-31 16:15:31 +02:00
parent 4be2894734
commit 76c18636cd
3 changed files with 52 additions and 0 deletions

View File

@ -25,6 +25,15 @@ func main() {
provider := vultr.Provider{APIToken: token} 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) records, err := provider.GetRecords(context.TODO(), zone)
if err != nil { if err != nil {
fmt.Printf("ERROR: %s\n", err.Error()) fmt.Printf("ERROR: %s\n", err.Error())

View File

@ -121,3 +121,35 @@ func (p *Provider) updateDNSRecord(ctx context.Context, domain string, record li
return record, nil 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
}

View File

@ -83,10 +83,21 @@ func (p *Provider) SetRecords(ctx context.Context, zone string, records []libdns
return setRecords, nil 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 // Interface guards
var ( var (
_ libdns.RecordGetter = (*Provider)(nil) _ libdns.RecordGetter = (*Provider)(nil)
_ libdns.RecordAppender = (*Provider)(nil) _ libdns.RecordAppender = (*Provider)(nil)
_ libdns.RecordSetter = (*Provider)(nil) _ libdns.RecordSetter = (*Provider)(nil)
_ libdns.RecordDeleter = (*Provider)(nil) _ libdns.RecordDeleter = (*Provider)(nil)
_ libdns.ZoneLister = (*Provider)(nil)
) )