libdns-gandi

Fork https://github.com/foxcpp/libdns-gandi

git clone git://git.lin.moe/go/libdns-gandi.git

  1package gandi
  2
  3import (
  4	"context"
  5	"net/http"
  6	"sync"
  7	"time"
  8
  9	"github.com/libdns/libdns"
 10)
 11
 12// 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"`
 17
 18	domains map[string]gandiDomain
 19	mutex   sync.Mutex
 20}
 21
 22// 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, err
 27	}
 28
 29	req, err := http.NewRequestWithContext(ctx, "GET", domain.DomainRecordsHref, nil)
 30	if err != nil {
 31		return nil, err
 32	}
 33
 34	var gandiRecords []gandiRecord
 35	_, err = p.doRequest(req, &gandiRecords)
 36	if err != nil {
 37		return nil, err
 38	}
 39
 40	var libRecords []libdns.Record
 41	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			}
 49
 50			libRecords = append(libRecords, rec)
 51		}
 52	}
 53
 54	return libRecords, nil
 55}
 56
 57// 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 records
 59func (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, err
 63	}
 64
 65	for _, rec := range records {
 66		err := p.setRecord(ctx, zone, rec.RR(), domain)
 67		if err != nil {
 68			return nil, err
 69		}
 70	}
 71
 72	return records, nil
 73}
 74
 75// 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, err
 80	}
 81
 82	for _, rec := range records {
 83		err := p.deleteRecord(ctx, zone, rec.RR(), domain)
 84		if err != nil {
 85			return nil, err
 86		}
 87	}
 88
 89	return records, nil
 90}
 91
 92// 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, err
 98	}
 99
100	for _, rec := range records {
101		err := p.setRecord(ctx, zone, rec.RR(), domain)
102		if err != nil {
103			return nil, err
104		}
105	}
106
107	return records, nil
108}
109
110// Interface guards
111var (
112	_ libdns.RecordGetter   = (*Provider)(nil)
113	_ libdns.RecordAppender = (*Provider)(nil)
114	_ libdns.RecordSetter   = (*Provider)(nil)
115	_ libdns.RecordDeleter  = (*Provider)(nil)
116)