Skip to content

Commit 619ec49

Browse files
committed
pdns: improve error messages
1 parent 2f10624 commit 619ec49

File tree

1 file changed

+34
-27
lines changed

1 file changed

+34
-27
lines changed

providers/dns/pdns/pdns.go

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -121,84 +121,86 @@ func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
121121

122122
// Present creates a TXT record to fulfill the dns-01 challenge.
123123
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
124+
ctx := context.Background()
125+
124126
info := dns01.GetChallengeInfo(domain, keyAuth)
125127

126128
authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
127129
if err != nil {
128130
return fmt.Errorf("pdns: could not find zone for domain %q: %w", domain, err)
129131
}
130132

131-
ctx := context.Background()
132-
133133
zone, err := d.client.GetHostedZone(ctx, authZone)
134134
if err != nil {
135-
return fmt.Errorf("pdns: %w", err)
135+
return fmt.Errorf("pdns: get hosted zone for %s: %w", authZone, err)
136136
}
137137

138+
// Look for existing records.
139+
existingRRSet := findTxtRecord(zone, info.EffectiveFQDN)
140+
138141
name := info.EffectiveFQDN
139142
if d.client.APIVersion() == 0 {
140143
// pre-v1 API wants non-fqdn
141144
name = dns01.UnFqdn(info.EffectiveFQDN)
142145
}
143146

144-
// Look for existing records.
145-
existingRRSet := findTxtRecord(zone, info.EffectiveFQDN)
146-
147-
// merge the existing and new records
148147
var records []internal.Record
149148
if existingRRSet != nil {
150149
records = existingRRSet.Records
151150
}
152151

153-
rec := internal.Record{
152+
records = append(records, internal.Record{
154153
Content: strconv.Quote(info.Value),
155154
Disabled: false,
156155

157156
// pre-v1 API
158157
Type: "TXT",
159158
Name: name,
160159
TTL: d.config.TTL,
161-
}
160+
})
162161

163162
rrSets := internal.RRSets{
164-
RRSets: []internal.RRSet{
165-
{
166-
Name: name,
167-
ChangeType: "REPLACE",
168-
Type: "TXT",
169-
Kind: "Master",
170-
TTL: d.config.TTL,
171-
Records: append(records, rec),
172-
},
173-
},
163+
RRSets: []internal.RRSet{{
164+
Name: name,
165+
ChangeType: "REPLACE",
166+
Type: "TXT",
167+
Kind: "Master",
168+
TTL: d.config.TTL,
169+
Records: records,
170+
}},
174171
}
175172

176173
err = d.client.UpdateRecords(ctx, zone, rrSets)
177174
if err != nil {
178-
return fmt.Errorf("pdns: %w", err)
175+
return fmt.Errorf("pdns: update records: %w", err)
176+
}
177+
178+
err = d.client.Notify(ctx, zone)
179+
if err != nil {
180+
return fmt.Errorf("pdns: notify: %w", err)
179181
}
180182

181-
return d.client.Notify(ctx, zone)
183+
return nil
182184
}
183185

184186
// CleanUp removes the TXT record matching the specified parameters.
185187
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
188+
ctx := context.Background()
189+
186190
info := dns01.GetChallengeInfo(domain, keyAuth)
187191

188192
authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
189193
if err != nil {
190194
return fmt.Errorf("pdns: could not find zone for domain %q: %w", domain, err)
191195
}
192196

193-
ctx := context.Background()
194-
195197
zone, err := d.client.GetHostedZone(ctx, authZone)
196198
if err != nil {
197-
return fmt.Errorf("pdns: %w", err)
199+
return fmt.Errorf("pdns: get hosted zone for %s: %w", authZone, err)
198200
}
199201

202+
// Look for existing records.
200203
set := findTxtRecord(zone, info.EffectiveFQDN)
201-
202204
if set == nil {
203205
return fmt.Errorf("pdns: no existing record found for %s", info.EffectiveFQDN)
204206
}
@@ -225,10 +227,15 @@ func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
225227

226228
err = d.client.UpdateRecords(ctx, zone, internal.RRSets{RRSets: []internal.RRSet{rrSet}})
227229
if err != nil {
228-
return fmt.Errorf("pdns: %w", err)
230+
return fmt.Errorf("pdns: update record:s %w", err)
231+
}
232+
233+
err = d.client.Notify(ctx, zone)
234+
if err != nil {
235+
return fmt.Errorf("pdns: notify: %w", err)
229236
}
230237

231-
return d.client.Notify(ctx, zone)
238+
return nil
232239
}
233240

234241
func findTxtRecord(zone *internal.HostedZone, fqdn string) *internal.RRSet {

0 commit comments

Comments
 (0)