Skip to content

Commit 2ef2362

Browse files
authored
HETZNER: adopt ZoneCache (#3372)
1 parent ab00797 commit 2ef2362

File tree

3 files changed

+25
-54
lines changed

3 files changed

+25
-54
lines changed

providers/hetzner/api.go

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"time"
1212

1313
"github.com/StackExchange/dnscontrol/v4/pkg/printer"
14+
"github.com/StackExchange/dnscontrol/v4/pkg/zoneCache"
1415
)
1516

1617
const (
@@ -19,8 +20,7 @@ const (
1920

2021
type hetznerProvider struct {
2122
apiKey string
22-
mu sync.Mutex
23-
cachedZones map[string]zone
23+
zoneCache zoneCache.ZoneCache[zone]
2424
requestRateLimiter requestRateLimiter
2525
}
2626

@@ -62,7 +62,13 @@ func (api *hetznerProvider) createZone(name string) error {
6262
request := createZoneRequest{
6363
Name: name,
6464
}
65-
return api.request("/zones", "POST", request, nil, nil)
65+
response := createZoneResponse{}
66+
err := api.request("/zones", "POST", request, &response, nil)
67+
if err != nil {
68+
return err
69+
}
70+
api.zoneCache.SetZone(name, response.Zone)
71+
return nil
6672
}
6773

6874
func (api *hetznerProvider) deleteRecord(record *record) error {
@@ -71,7 +77,7 @@ func (api *hetznerProvider) deleteRecord(record *record) error {
7177
}
7278

7379
func (api *hetznerProvider) getAllRecords(domain string) ([]record, error) {
74-
z, err := api.getZone(domain)
80+
z, err := api.zoneCache.GetZone(domain)
7581
if err != nil {
7682
return nil, err
7783
}
@@ -105,18 +111,7 @@ func (api *hetznerProvider) getAllRecords(domain string) ([]record, error) {
105111
return records, nil
106112
}
107113

108-
func (api *hetznerProvider) resetZoneCache() {
109-
api.mu.Lock()
110-
defer api.mu.Unlock()
111-
api.cachedZones = nil
112-
}
113-
114-
func (api *hetznerProvider) getAllZones() (map[string]zone, error) {
115-
api.mu.Lock()
116-
defer api.mu.Unlock()
117-
if api.cachedZones != nil {
118-
return api.cachedZones, nil
119-
}
114+
func (api *hetznerProvider) fetchAllZones() (map[string]zone, error) {
120115
var zones map[string]zone
121116
page := 1
122117
statusOK := func(code int) bool {
@@ -148,22 +143,9 @@ func (api *hetznerProvider) getAllZones() (map[string]zone, error) {
148143
}
149144
page++
150145
}
151-
api.cachedZones = zones
152146
return zones, nil
153147
}
154148

155-
func (api *hetznerProvider) getZone(name string) (*zone, error) {
156-
zones, err := api.getAllZones()
157-
if err != nil {
158-
return nil, err
159-
}
160-
z, ok := zones[name]
161-
if !ok {
162-
return nil, fmt.Errorf("%q is not a zone in this HETZNER account", name)
163-
}
164-
return &z, nil
165-
}
166-
167149
func (api *hetznerProvider) request(endpoint string, method string, request interface{}, target interface{}, statusOK func(code int) bool) error {
168150
if statusOK == nil {
169151
statusOK = func(code int) bool {

providers/hetzner/hetznerProvider.go

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/StackExchange/dnscontrol/v4/models"
99
"github.com/StackExchange/dnscontrol/v4/pkg/diff"
10+
"github.com/StackExchange/dnscontrol/v4/pkg/zoneCache"
1011
"github.com/StackExchange/dnscontrol/v4/providers"
1112
)
1213

@@ -50,28 +51,20 @@ func New(settings map[string]string, _ json.RawMessage) (providers.DNSServicePro
5051
return nil, errors.New("missing HETZNER api_key")
5152
}
5253

53-
return &hetznerProvider{
54-
apiKey: apiKey,
55-
}, nil
54+
api := &hetznerProvider{apiKey: apiKey}
55+
api.zoneCache = zoneCache.New(api.fetchAllZones)
56+
return api, nil
5657
}
5758

5859
// EnsureZoneExists creates a zone if it does not exist
5960
func (api *hetznerProvider) EnsureZoneExists(domain string) error {
60-
domains, err := api.ListZones()
61-
if err != nil {
61+
if ok, err := api.zoneCache.HasZone(domain); err != nil || ok {
6262
return err
6363
}
6464

65-
for _, d := range domains {
66-
if d == domain {
67-
return nil
68-
}
69-
}
70-
71-
if err = api.createZone(domain); err != nil {
65+
if err := api.createZone(domain); err != nil {
7266
return err
7367
}
74-
api.resetZoneCache()
7568
return nil
7669
}
7770

@@ -86,7 +79,7 @@ func (api *hetznerProvider) GetZoneRecordsCorrections(dc *models.DomainConfig, e
8679
// Start corrections with the reports
8780
corrections := diff.GenerateMessageCorrections(toReport)
8881

89-
z, err := api.getZone(domain)
82+
z, err := api.zoneCache.GetZone(domain)
9083
if err != nil {
9184
return nil, 0, err
9285
}
@@ -143,7 +136,7 @@ func (api *hetznerProvider) GetZoneRecordsCorrections(dc *models.DomainConfig, e
143136

144137
// GetNameservers returns the nameservers for a domain.
145138
func (api *hetznerProvider) GetNameservers(domain string) ([]*models.Nameserver, error) {
146-
z, err := api.getZone(domain)
139+
z, err := api.zoneCache.GetZone(domain)
147140
if err != nil {
148141
return nil, err
149142
}
@@ -169,13 +162,5 @@ func (api *hetznerProvider) GetZoneRecords(domain string, meta map[string]string
169162

170163
// ListZones lists the zones on this account.
171164
func (api *hetznerProvider) ListZones() ([]string, error) {
172-
zones, err := api.getAllZones()
173-
if err != nil {
174-
return nil, err
175-
}
176-
domains := make([]string, 0, len(zones))
177-
for domain := range zones {
178-
domains = append(domains, domain)
179-
}
180-
return domains, nil
165+
return api.zoneCache.GetZoneNames()
181166
}

providers/hetzner/types.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ type createZoneRequest struct {
1717
Name string `json:"name"`
1818
}
1919

20+
type createZoneResponse struct {
21+
Zone zone `json:"zone"`
22+
}
23+
2024
type getAllRecordsResponse struct {
2125
Records []record `json:"records"`
2226
Meta struct {
@@ -53,7 +57,7 @@ type zone struct {
5357
TTL uint32 `json:"ttl"`
5458
}
5559

56-
func fromRecordConfig(in *models.RecordConfig, zone *zone) record {
60+
func fromRecordConfig(in *models.RecordConfig, zone zone) record {
5761
r := record{
5862
Name: in.GetLabel(),
5963
Type: in.Type,

0 commit comments

Comments
 (0)