-
Notifications
You must be signed in to change notification settings - Fork 0
/
provider.go
65 lines (53 loc) · 1.99 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
// Package dode implements a DNS record management client compatible
// with the libdns interfaces for do.de. Unfortunately, the do.de API only
// supports creating and removing TXT records for domains starting with `_acme-challenge.`
package dode
import (
"context"
"fmt"
"strings"
"github.com/libdns/libdns"
)
// Provider facilitates DNS record manipulation with do.de.
type Provider struct {
// API token for do.de API
APIToken string `json:"api_token,omitempty"`
}
const notSupportedErrorMsg = "the do.de API only supports creating and removing TXT records for domains starting with '_acme-challenge.'"
const acmeChallenge = "_acme-challenge"
// AppendRecords adds records to the zone. It returns the records that were added.
//
// The do.de API only supports creating TXT records that start with `_acme-challenge.`.
func (p *Provider) AppendRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {
for _, rec := range records {
if rec.Type != "TXT" || !strings.HasPrefix(rec.Name, acmeChallenge) {
return nil, fmt.Errorf(notSupportedErrorMsg)
}
name := libdns.AbsoluteName(rec.Name, zone)
err := p.createACMERecord(ctx, strings.TrimSuffix(name, "."), rec.Value)
if err != nil {
return nil, err
}
}
return records, nil
}
// DeleteRecords deletes the records from the zone. It returns the records that were deleted.
//
// The do.de API only supports deleting TXT records that start with `_acme-challenge.`.
func (p *Provider) DeleteRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {
for _, rec := range records {
if rec.Type != "TXT" || !strings.HasPrefix(rec.Name, acmeChallenge) {
return nil, fmt.Errorf(notSupportedErrorMsg)
}
err := p.deleteACMERecord(ctx, strings.TrimSuffix(libdns.AbsoluteName(rec.Name, zone), "."))
if err != nil {
return nil, err
}
}
return records, nil
}
// Interface guards
var (
_ libdns.RecordAppender = (*Provider)(nil)
_ libdns.RecordDeleter = (*Provider)(nil)
)