forked from libdns/gandi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovider.go
115 lines (95 loc) · 2.79 KB
/
provider.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package gandi
import (
"context"
"net/http"
"sync"
"time"
"github.com/libdns/libdns"
)
// Provider implements the libdns interfaces for Gandi.
type Provider struct {
APIToken string `json:"api_token,omitempty"`
BearerToken string `json:"bearer_token,omitempty"`
domains map[string]gandiDomain
mutex sync.Mutex
}
// GetRecords lists all the records in the zone.
func (p *Provider) GetRecords(ctx context.Context, zone string) ([]libdns.Record, error) {
domain, err := p.getDomain(ctx, zone)
if err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(ctx, "GET", domain.DomainRecordsHref, nil)
if err != nil {
return nil, err
}
var gandiRecords []gandiRecord
_, err = p.doRequest(req, &gandiRecords)
if err != nil {
return nil, err
}
var libRecords []libdns.Record
for _, rec := range gandiRecords {
for _, val := range rec.RRSetValues {
rec := libdns.Record{
Type: rec.RRSetType,
Name: rec.RRSetName,
TTL: time.Duration(rec.RRSetTTL) * time.Second,
Value: val,
}
libRecords = append(libRecords, rec)
}
}
return libRecords, nil
}
// AppendRecords adds records to the zone and returns the records that were created.
// Due to technical limitations of the LiveDNS API, it may affect the TTL of similar records
func (p *Provider) AppendRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {
domain, err := p.getDomain(ctx, zone)
if err != nil {
return nil, err
}
for _, rec := range records {
err := p.setRecord(ctx, zone, rec, domain)
if err != nil {
return nil, err
}
}
return records, nil
}
// DeleteRecords deletes records from the zone and returns the records that were deleted.
func (p *Provider) DeleteRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {
domain, err := p.getDomain(ctx, zone)
if err != nil {
return nil, err
}
for _, rec := range records {
err := p.deleteRecord(ctx, zone, rec, domain)
if err != nil {
return nil, err
}
}
return records, nil
}
// SetRecords sets the records in the zone, either by updating existing records or creating new ones, and returns the recordsthat were updated.
// Due to technical limitations of the LiveDNS API, it may affect the TTL of similar records.
func (p *Provider) SetRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {
domain, err := p.getDomain(ctx, zone)
if err != nil {
return nil, err
}
for _, rec := range records {
err := p.setRecord(ctx, zone, rec, domain)
if err != nil {
return nil, err
}
}
return records, nil
}
// Interface guards
var (
_ libdns.RecordGetter = (*Provider)(nil)
_ libdns.RecordAppender = (*Provider)(nil)
_ libdns.RecordSetter = (*Provider)(nil)
_ libdns.RecordDeleter = (*Provider)(nil)
)