-
Notifications
You must be signed in to change notification settings - Fork 66
/
handler.go
152 lines (127 loc) · 4.02 KB
/
handler.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
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
/*
* Copyright 2020 SAP SE or an SAP affiliate company. All rights reserved. h file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
*
*/
package netlify
import (
"strings"
"github.com/gardener/controller-manager-library/pkg/logger"
"github.com/netlify/open-api/go/models"
"github.com/gardener/external-dns-management/pkg/dns"
"github.com/gardener/external-dns-management/pkg/dns/provider"
"github.com/gardener/external-dns-management/pkg/dns/provider/raw"
)
type Handler struct {
provider.DefaultDNSHandler
config provider.DNSHandlerConfig
cache provider.ZoneCache
access Access
}
var _ provider.DNSHandler = &Handler{}
func NewHandler(c *provider.DNSHandlerConfig) (provider.DNSHandler, error) {
var err error
h := &Handler{
DefaultDNSHandler: provider.NewDefaultDNSHandler(TYPE_CODE),
config: *c,
}
apiToken, err := c.GetRequiredProperty("NETLIFY_AUTH_TOKEN", "NETLIFY_API_TOKEN")
if err != nil {
return nil, err
}
access, err := NewAccess(apiToken, c.Metrics, c.RateLimiter)
if err != nil {
return nil, err
}
h.access = access
h.cache, err = provider.NewZoneCache(*c.CacheConfig.CopyWithDisabledZoneStateCache(), c.Metrics, nil, h.getZones, h.getZoneState)
if err != nil {
return nil, err
}
return h, nil
}
func (h *Handler) Release() {
h.cache.Release()
}
func (h *Handler) GetZones() (provider.DNSHostedZones, error) {
return h.cache.GetZones()
}
func (h *Handler) getZones(cache provider.ZoneCache) (provider.DNSHostedZones, error) {
rawZones := []models.DNSZone{}
{
f := func(zone models.DNSZone) (bool, error) {
rawZones = append(rawZones, zone)
return true, nil
}
err := h.access.ListZones(f)
if err != nil {
return nil, err
}
}
zones := provider.DNSHostedZones{}
for _, z := range rawZones {
forwarded := []string{}
f := func(r models.DNSRecord) (bool, error) {
if r.Type == dns.RS_NS {
name := r.Hostname
if name != z.Name {
forwarded = append(forwarded, name)
}
}
return true, nil
}
err := h.access.ListRecords(z.ID, f)
if err != nil {
if checkAccessForbidden(err) {
// It is possible to deny access to certain zones in the account
// As a result, z zone should not be appended to the hosted zones
continue
}
return nil, err
}
hostedZone := provider.NewDNSHostedZone(h.ProviderType(), z.ID, z.Name, z.ID, forwarded, false)
zones = append(zones, hostedZone)
}
return zones, nil
}
func (h *Handler) GetZoneState(zone provider.DNSHostedZone) (provider.DNSZoneState, error) {
return h.cache.GetZoneState(zone)
}
func (h *Handler) getZoneState(zone provider.DNSHostedZone, cache provider.ZoneCache) (provider.DNSZoneState, error) {
state := raw.NewState()
f := func(r models.DNSRecord) (bool, error) {
a := (*Record)(&r)
state.AddRecord(a)
return true, nil
}
err := h.access.ListRecords(zone.Key(), f)
if err != nil {
return nil, err
}
state.CalculateDNSSets()
return state, nil
}
func (h *Handler) ReportZoneStateConflict(zone provider.DNSHostedZone, err error) bool {
return h.cache.ReportZoneStateConflict(zone, err)
}
func (h *Handler) ExecuteRequests(logger logger.LogContext, zone provider.DNSHostedZone, state provider.DNSZoneState, reqs []*provider.ChangeRequest) error {
err := raw.ExecuteRequests(logger, &h.config, h.access, zone, state, reqs)
h.cache.ApplyRequests(logger, err, zone, reqs)
return err
}
func checkAccessForbidden(err error) bool {
if err != nil && strings.Contains(err.Error(), "403") {
return true
}
return false
}