forked from libdns/scaleway
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
161 lines (145 loc) · 3.39 KB
/
client.go
File metadata and controls
161 lines (145 loc) · 3.39 KB
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
package scaleway
import (
"context"
"sync"
"time"
"github.com/libdns/libdns"
domain "github.com/scaleway/scaleway-sdk-go/api/domain/v2beta1"
"github.com/scaleway/scaleway-sdk-go/scw"
)
type Client struct {
client *scw.Client
mutex sync.Mutex
}
func (p *Provider) getClient() error {
if p.client == nil {
var err error
p.client, err = scw.NewClient(
scw.WithAuth("SCWXXXXXXXXXXXXXXXXX", p.SecretKey),
scw.WithDefaultOrganizationID(p.OrganizationID),
)
if err != nil {
return err
}
}
return nil
}
func (p *Provider) getDNSEntries(ctx context.Context, zone string) ([]libdns.Record, error) {
p.mutex.Lock()
defer p.mutex.Unlock()
err := p.getClient()
if err != nil {
return nil, err
}
domainAPI := domain.NewAPI(p.client)
var records []libdns.Record
zoneRecords, err := domainAPI.ListDNSZoneRecords(&domain.ListDNSZoneRecordsRequest{
DNSZone: zone,
})
if err != nil {
return records, err
}
for _, entry := range zoneRecords.Records {
record := libdns.Record{
Name: libdns.RelativeName(entry.Name, zone),
Value: entry.Data,
Type: string(entry.Type),
TTL: time.Duration(entry.TTL) * time.Second,
ID: entry.ID,
}
records = append(records, record)
}
return records, nil
}
func (p *Provider) addDNSEntry(ctx context.Context, zone string, record libdns.Record) (libdns.Record, error) {
p.mutex.Lock()
defer p.mutex.Unlock()
err := p.getClient()
if err != nil {
return record, err
}
domainAPI := domain.NewAPI(p.client)
records, err := domainAPI.UpdateDNSZoneRecords(&domain.UpdateDNSZoneRecordsRequest{
DNSZone: zone,
Changes: []*domain.RecordChange{
{
Add: &domain.RecordChangeAdd{
Records: []*domain.Record{
{
Name: libdns.AbsoluteName(record.Name, zone),
Data: record.Value,
Type: domain.RecordType(record.Type),
TTL: func() uint32 {
if record.TTL.Seconds() == 0 {
return 60
}
return uint32(record.TTL.Seconds())
}(),
},
},
},
},
},
})
if err != nil {
return record, err
}
if len(records.Records) > 0 {
record.ID = records.Records[0].ID
}
return record, nil
}
func (p *Provider) removeDNSEntry(ctx context.Context, zone string, record libdns.Record) (libdns.Record, error) {
p.mutex.Lock()
defer p.mutex.Unlock()
err := p.getClient()
if err != nil {
return record, err
}
domainAPI := domain.NewAPI(p.client)
_, err = domainAPI.UpdateDNSZoneRecords(&domain.UpdateDNSZoneRecordsRequest{
DNSZone: zone,
Changes: []*domain.RecordChange{
{
Delete: &domain.RecordChangeDelete{
ID: &record.ID,
},
},
},
})
if err != nil {
return record, err
}
return record, nil
}
func (p *Provider) updateDNSEntry(ctx context.Context, zone string, record libdns.Record) (libdns.Record, error) {
p.mutex.Lock()
defer p.mutex.Unlock()
err := p.getClient()
if err != nil {
return record, err
}
domainAPI := domain.NewAPI(p.client)
_, err = domainAPI.UpdateDNSZoneRecords(&domain.UpdateDNSZoneRecordsRequest{
DNSZone: zone,
Changes: []*domain.RecordChange{
{
Set: &domain.RecordChangeSet{
ID: &record.ID,
Records: []*domain.Record{
{
Name: libdns.AbsoluteName(record.Name, zone),
Data: record.Value,
Type: domain.RecordType(record.Type),
TTL: uint32(record.TTL.Seconds()),
},
},
},
},
},
})
if err != nil {
return record, err
}
return record, nil
}