1package gandi23import (4 "context"5 "net/http"6 "sync"7 "time"89 "github.com/libdns/libdns"10)1112// Provider implements the libdns interfaces for Gandi.13type Provider struct {14 // DEPRECATED: Use BearerToken (personal access token) instead.15 APIToken string `json:"api_token,omitempty"`16 BearerToken string `json:"bearer_token,omitempty"`1718 domains map[string]gandiDomain19 mutex sync.Mutex20}2122// GetRecords lists all the records in the zone.23func (p *Provider) GetRecords(ctx context.Context, zone string) ([]libdns.Record, error) {24 domain, err := p.getDomain(ctx, zone)25 if err != nil {26 return nil, err27 }2829 req, err := http.NewRequestWithContext(ctx, "GET", domain.DomainRecordsHref, nil)30 if err != nil {31 return nil, err32 }3334 var gandiRecords []gandiRecord35 _, err = p.doRequest(req, &gandiRecords)36 if err != nil {37 return nil, err38 }3940 var libRecords []libdns.Record41 for _, rec := range gandiRecords {42 for _, val := range rec.RRSetValues {43 rec := libdns.RR{44 Type: rec.RRSetType,45 Name: rec.RRSetName,46 TTL: time.Duration(rec.RRSetTTL) * time.Second,47 Data: val,48 }4950 libRecords = append(libRecords, rec)51 }52 }5354 return libRecords, nil55}5657// AppendRecords adds records to the zone and returns the records that were created.58// Due to technical limitations of the LiveDNS API, it may affect the TTL of similar records59func (p *Provider) AppendRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {60 domain, err := p.getDomain(ctx, zone)61 if err != nil {62 return nil, err63 }6465 for _, rec := range records {66 err := p.setRecord(ctx, zone, rec.RR(), domain)67 if err != nil {68 return nil, err69 }70 }7172 return records, nil73}7475// DeleteRecords deletes records from the zone and returns the records that were deleted.76func (p *Provider) DeleteRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {77 domain, err := p.getDomain(ctx, zone)78 if err != nil {79 return nil, err80 }8182 for _, rec := range records {83 err := p.deleteRecord(ctx, zone, rec.RR(), domain)84 if err != nil {85 return nil, err86 }87 }8889 return records, nil90}9192// SetRecords sets the records in the zone, either by updating existing records or creating new ones, and returns the recordsthat were updated.93// Due to technical limitations of the LiveDNS API, it may affect the TTL of similar records.94func (p *Provider) SetRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {95 domain, err := p.getDomain(ctx, zone)96 if err != nil {97 return nil, err98 }99100 for _, rec := range records {101 err := p.setRecord(ctx, zone, rec.RR(), domain)102 if err != nil {103 return nil, err104 }105 }106107 return records, nil108}109110// Interface guards111var (112 _ libdns.RecordGetter = (*Provider)(nil)113 _ libdns.RecordAppender = (*Provider)(nil)114 _ libdns.RecordSetter = (*Provider)(nil)115 _ libdns.RecordDeleter = (*Provider)(nil)116)