-
Notifications
You must be signed in to change notification settings - Fork 1
/
provider.go
172 lines (154 loc) · 4.4 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package huaweicloud
import (
"context"
"slices"
"time"
"github.com/devhaozi/huaweicloud-sdk-go-v3/services/dns/v2/model"
"github.com/libdns/libdns"
)
// Provider facilitates DNS record manipulation with Huawei Cloud
type Provider struct {
// AccessKeyId is required by the Huawei Cloud API for authentication.
AccessKeyId string `json:"access_key_id,omitempty"`
// SecretAccessKey is required by the Huawei Cloud API for authentication.
SecretAccessKey string `json:"secret_access_key,omitempty"`
// RegionId is optional and defaults to "cn-south-1".
RegionId string `json:"region_id,omitempty"`
}
// GetRecords lists all the records in the zone.
func (p *Provider) GetRecords(ctx context.Context, zone string) ([]libdns.Record, error) {
zoneId, err := p.getZoneIdByName(zone)
if err != nil {
return nil, err
}
client, err := p.getClient()
if err != nil {
return nil, err
}
request := &model.ListRecordSetsByZoneRequest{
ZoneId: zoneId,
}
response, err := client.ListRecordSetsByZone(request)
if err != nil {
return nil, err
}
var list []libdns.Record
for record := range slices.Values(*response.Recordsets) {
for value := range slices.Values(*record.Records) {
list = append(list, libdns.Record{
ID: *record.Id,
Type: *record.Type,
Name: libdns.RelativeName(*record.Name, zone),
Value: value,
TTL: time.Duration(*record.Ttl) * time.Second,
})
}
}
return list, 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) {
zoneId, err := p.getZoneIdByName(zone)
if err != nil {
return nil, err
}
client, err := p.getClient()
if err != nil {
return nil, err
}
for i, record := range records {
ttl := int32(record.TTL.Seconds())
request := &model.CreateRecordSetRequest{
ZoneId: zoneId,
Body: &model.CreateRecordSetRequestBody{
Name: libdns.AbsoluteName(record.Name, zone),
Type: record.Type,
Ttl: &ttl,
Records: []string{record.Value},
},
}
response, err := client.CreateRecordSet(request)
if err != nil {
return nil, err
}
records[i].ID = *response.Id
}
return records, 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) {
zoneId, err := p.getZoneIdByName(zone)
if err != nil {
return nil, err
}
client, err := p.getClient()
if err != nil {
return nil, err
}
for i, record := range records {
// If the record id is empty try to get it by name and type
if record.ID == "" {
id, err := p.getRecordIdByNameAndType(ctx, zone, record.Name, record.Type)
if err == nil {
record.ID = id
}
}
// If the record id is still empty, it means it is a new record
if record.ID == "" {
newRecord, err := p.AppendRecords(ctx, zone, []libdns.Record{record})
if err != nil {
return nil, err
}
records[i].ID = newRecord[0].ID
} else {
name := libdns.AbsoluteName(record.Name, zone)
ttl := int32(record.TTL.Seconds())
value := []string{record.Value}
request := &model.UpdateRecordSetRequest{
ZoneId: zoneId,
RecordsetId: record.ID,
Body: &model.UpdateRecordSetReq{
Name: &name,
Type: &record.Type,
Ttl: &ttl,
Records: &value,
},
}
response, err := client.UpdateRecordSet(request)
if err != nil {
return nil, err
}
records[i].ID = *response.Id
}
}
return records, 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) {
zoneId, err := p.getZoneIdByName(zone)
if err != nil {
return nil, err
}
client, err := p.getClient()
if err != nil {
return nil, err
}
for record := range slices.Values(records) {
request := &model.DeleteRecordSetRequest{
ZoneId: zoneId,
RecordsetId: record.ID,
}
if _, err = client.DeleteRecordSet(request); err != nil {
return nil, err
}
}
return records, nil
}
// Interface guards
var (
_ libdns.RecordGetter = (*Provider)(nil)
_ libdns.RecordAppender = (*Provider)(nil)
_ libdns.RecordSetter = (*Provider)(nil)
_ libdns.RecordDeleter = (*Provider)(nil)
)