-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathprovider.go
More file actions
170 lines (158 loc) · 5.85 KB
/
provider.go
File metadata and controls
170 lines (158 loc) · 5.85 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Package linode implements a DNS record management client compatible
// with the libdns interfaces for Linode.
package linode
import (
"context"
"errors"
"fmt"
"log/slog"
"net/http"
"os"
"sync"
"github.com/libdns/libdns"
"github.com/linode/linodego"
)
// Provider facilitates DNS record manipulation with Linode.
type Provider struct {
// APIToken is the Linode Personal Access Token, see https://cloud.linode.com/profile/tokens.
APIToken string `json:"api_token,omitempty"`
// APIURL is the Linode API hostname, i.e. "api.linode.com".
APIURL string `json:"api_url,omitempty"`
// APIVersion is the Linode API version, i.e. "v4".
APIVersion string `json:"api_version,omitempty"`
DebugLogsEnabled bool `json:"debug_logs_enabled,omitempty"`
client linodego.Client
once sync.Once
mutex sync.Mutex
}
func (p *Provider) init(_ context.Context) {
slog.Debug("Enter init", "hasToken", p.APIToken != "", "APIURL", p.APIURL, "APIVersion", p.APIVersion)
p.once.Do(func() {
// Configure global logger based on DebugLogsEnabled
level := slog.LevelInfo
if p.DebugLogsEnabled {
level = slog.LevelDebug
}
h := slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: level})
slog.SetDefault(slog.New(h))
p.client = linodego.NewClient(http.DefaultClient)
if p.APIToken != "" {
p.client.SetToken(p.APIToken)
}
if p.APIURL != "" {
p.client.SetBaseURL(p.APIURL)
}
if p.APIVersion != "" {
p.client.SetAPIVersion(p.APIVersion)
}
})
slog.Debug("Exit init")
}
// ListZones lists all the zones (domains).
func (p *Provider) ListZones(ctx context.Context) ([]libdns.Zone, error) {
p.mutex.Lock()
defer p.mutex.Unlock()
p.init(ctx)
slog.Debug("Enter ListZones")
domains, err := p.client.ListDomains(ctx, nil)
if err != nil {
return nil, fmt.Errorf("error listing domains: %w", err)
}
zones := make([]libdns.Zone, 0, len(domains))
for _, domain := range domains {
zones = append(zones, libdns.Zone{Name: domain.Domain})
}
slog.Debug("Exit ListZones", "lenZones", len(zones))
return zones, nil
}
// GetRecords lists all the records in the zone.
func (p *Provider) GetRecords(ctx context.Context, zone string) ([]libdns.Record, error) {
p.mutex.Lock()
defer p.mutex.Unlock()
p.init(ctx)
slog.Debug("Enter GetRecords", "zone", zone)
domainID, err := p.getDomainIDByZone(ctx, zone)
if err != nil {
return nil, fmt.Errorf("error getting domain ID for zone %s: %v", zone, err)
}
records, err := p.listDomainRecords(ctx, domainID)
if err != nil {
return nil, fmt.Errorf("error listing domain records: %w", err)
}
slog.Debug("Exit GetRecords", "zone", zone, "lenRecords", len(records))
return records, nil
}
// AppendRecords adds records to the zone. It returns the records that were added.
func (p *Provider) AppendRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {
p.mutex.Lock()
defer p.mutex.Unlock()
p.init(ctx)
slog.Debug("Enter AppendRecords", "zone", zone, "lenRecords", len(records))
domainID, err := p.getDomainIDByZone(ctx, zone)
if err != nil {
return nil, fmt.Errorf("error getting domain ID for zone %s: %v", zone, err)
}
addedRecords := make([]libdns.Record, 0)
for _, record := range records {
addedRecord, err := p.createDomainRecord(ctx, zone, domainID, record)
if err != nil {
if errors.Is(err, ErrUnsupportedType) {
// I would rather not fail silently; log at debug level as specified.
slog.Debug("skipping unsupported record type", "error", err)
continue
}
slog.Debug("skipping record due to error", "error", err)
continue
}
addedRecords = append(addedRecords, addedRecord)
}
slog.Debug("Exit AppendRecords", "zone", zone, "lenAddedRecords", len(addedRecords))
return addedRecords, nil
}
// SetRecords sets the records in the zone, either by updating existing records or creating new ones.
// It returns the updated records.
func (p *Provider) SetRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {
p.mutex.Lock()
defer p.mutex.Unlock()
p.init(ctx)
slog.Debug("Enter SetRecords", "zone", zone, "lenRecords", len(records))
domainID, err := p.getDomainIDByZone(ctx, zone)
if err != nil {
return nil, fmt.Errorf("could not find domain ID for zone: %s: %v", zone, err)
}
setRecords, err := p.createOrUpdateDomainRecords(ctx, zone, domainID, records)
if err != nil {
return nil, fmt.Errorf("could not create or update domain records: %w", err)
}
slog.Debug("Exit SetRecords", "zone", zone, "lenSetRecords", len(setRecords))
return setRecords, nil
}
// DeleteRecords deletes the records from the zone. It returns the records that were deleted.
// As per the libdns interface, any deleted records must match exactly the input record (Name, Type, TTL, Value).
// If any of (Type, TTL, Value) are "", 0, or "", respectively, deleteDomainRecord will delete any records that match
// the other fields, regardless of the value of the fields that were left empty.
// Note: this does not apply to the Name field.
func (p *Provider) DeleteRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {
p.mutex.Lock()
defer p.mutex.Unlock()
p.init(ctx)
slog.Debug("Enter DeleteRecords", "zone", zone, "lenRecords", len(records))
domainID, err := p.getDomainIDByZone(ctx, zone)
if err != nil {
return nil, fmt.Errorf("error getting domain ID for zone %s: %v", zone, err)
}
deletedRecords, err := p.deleteDomainRecords(ctx, domainID, records)
if err != nil {
return nil, fmt.Errorf("error deleting domain records: %w", err)
}
slog.Debug("Exit DeleteRecords", "zone", zone, "lenDeletedRecords", len(deletedRecords))
return deletedRecords, nil
}
// Interface guards
var (
_ libdns.RecordGetter = (*Provider)(nil)
_ libdns.RecordAppender = (*Provider)(nil)
_ libdns.RecordSetter = (*Provider)(nil)
_ libdns.RecordDeleter = (*Provider)(nil)
_ libdns.ZoneLister = (*Provider)(nil)
)