generated from libdns/template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprovider.go
96 lines (88 loc) · 3.2 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
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
package cloudns
import (
"context"
"errors"
"github.com/libdns/libdns"
"strings"
)
// ClouDNS API docs: https://www.cloudns.net/wiki/article/41/
// Provider facilitates DNS record manipulation with ClouDNS.
type Provider struct {
AuthId string `json:"auth_id"`
SubAuthId string `json:"sub_auth_id,omitempty"`
AuthPassword string `json:"auth_password"`
}
// GetRecords lists all the records in the zone.
func (p *Provider) GetRecords(ctx context.Context, zone string) ([]libdns.Record, error) {
if strings.HasSuffix(zone, ".") {
zone = strings.TrimSuffix(zone, ".")
}
records, err := UseClient(p.AuthId, p.SubAuthId, p.AuthPassword).GetRecords(ctx, zone)
if err != nil {
return nil, err
}
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) {
if strings.HasSuffix(zone, ".") {
zone = strings.TrimSuffix(zone, ".")
}
var createdRecords []libdns.Record
for _, record := range records {
r, err := UseClient(p.AuthId, p.SubAuthId, p.AuthPassword).AddRecord(ctx, zone, record.Type, record.Name, record.Value, record.TTL)
if err != nil {
return nil, errors.New("failed to add record: " + err.Error())
}
createdRecords = append(createdRecords, *r)
}
return createdRecords, 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) {
if strings.HasSuffix(zone, ".") {
zone = strings.TrimSuffix(zone, ".")
}
var updatedRecords []libdns.Record
for _, record := range records {
if len(record.ID) == 0 {
// create
r, err := UseClient(p.AuthId, p.SubAuthId, p.AuthPassword).AddRecord(ctx, zone, record.Type, record.Name, record.Value, record.TTL)
if err != nil {
return nil, errors.New("failed to add record: " + err.Error())
}
updatedRecords = append(updatedRecords, *r)
} else {
//update
r, err := UseClient(p.AuthId, p.SubAuthId, p.AuthPassword).UpdateRecord(ctx, zone, record.ID, record.Name, record.Value, record.TTL)
if err != nil {
return nil, errors.New("failed to update record: " + err.Error())
}
updatedRecords = append(updatedRecords, *r)
}
}
return updatedRecords, nil
}
// DeleteRecords deletes the records from the zone. It returns the records that were deleted.
func (p *Provider) DeleteRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {
if strings.HasSuffix(zone, ".") {
zone = strings.TrimSuffix(zone, ".")
}
var deletedRecords []libdns.Record
for _, record := range records {
r, err := UseClient(p.AuthId, p.SubAuthId, p.AuthPassword).DeleteRecord(ctx, zone, record.ID)
if err != nil {
return nil, errors.New("failed to delete record: " + err.Error())
}
deletedRecords = append(deletedRecords, *r)
}
return deletedRecords, nil
}
// Interface guards
var (
_ libdns.RecordGetter = (*Provider)(nil)
_ libdns.RecordAppender = (*Provider)(nil)
_ libdns.RecordSetter = (*Provider)(nil)
_ libdns.RecordDeleter = (*Provider)(nil)
)