-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathclient.go
More file actions
192 lines (156 loc) · 3.89 KB
/
client.go
File metadata and controls
192 lines (156 loc) · 3.89 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package designate
import (
"errors"
"fmt"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack"
"github.com/gophercloud/gophercloud/openstack/dns/v2/recordsets"
"github.com/gophercloud/gophercloud/openstack/dns/v2/zones"
"github.com/libdns/libdns"
"time"
)
func (p *Provider) getRecords(recordSets []recordsets.RecordSet) ([]libdns.Record, error) {
var records []libdns.Record
for _, j := range recordSets {
tmp := libdns.Record{
ID: j.ID,
Type: j.Type,
Name: j.Name,
TTL: time.Duration(j.TTL) * time.Second,
}
records = append(records, tmp)
}
return records, nil
}
func (p *Provider) getRecordID(recordName string, zone string) (string, error) {
recordName = recordName + "."
listOpts := recordsets.ListOpts{
Type: "TXT",
}
allPages, err := recordsets.ListByZone(p.dnsClient, p.zoneID, listOpts).AllPages()
if err != nil {
return "", err
}
allRecordSets, err := recordsets.ExtractRecordSets(allPages)
if err != nil {
return "", err
}
for _, rr := range allRecordSets {
if recordName == rr.Name {
return rr.ID, nil
}
}
return "", nil
}
func (p *Provider) createRecord(record libdns.Record, zone string) error {
createOpts := recordsets.CreateOpts{
Name: record.Name + ".",
Type: record.Type,
TTL: int(record.TTL / time.Second),
Records: []string{record.Value},
}
exist, err := p.getRecordID(record.Name, zone)
if err != nil {
return fmt.Errorf("cannot get recordID: %v", err)
}
if exist != "" {
return errors.New("DNS record already exist")
}
_, err = recordsets.Create(p.dnsClient, p.zoneID, createOpts).Extract()
if err != nil {
return fmt.Errorf("cannot create DNS record: %v", err)
}
return nil
}
func (p *Provider) updateRecord(record libdns.Record, recordID string) error {
updateOpts := recordsets.UpdateOpts{
TTL: intToPointer(int(record.TTL / time.Second)),
Records: []string{record.Value},
}
// Update updates a recordset in a given zone
_, err := recordsets.Update(p.dnsClient, p.zoneID, recordID, updateOpts).Extract()
if err != nil {
return err
}
return nil
}
func (p *Provider) deleteRecord(recordID string) error {
err := recordsets.Delete(p.dnsClient, p.zoneID, recordID).ExtractErr()
if err != nil {
return err
}
return nil
}
func (p *Provider) isAuth() (bool, error) {
if p.dnsClient != nil {
_, err := p.dnsClient.GetAuthResult().ExtractTokenID()
if err != nil {
return true, err
}
}
return false, nil
}
func (p *Provider) auth() error {
p.mu.Lock()
defer p.mu.Unlock()
authStatus, err := p.isAuth()
if err != nil {
return err
}
if authStatus {
return nil
}
opts := gophercloud.AuthOptions{
IdentityEndpoint: p.AuthOpenStack.AuthURL,
Username: p.AuthOpenStack.Username,
Password: p.AuthOpenStack.Password,
TenantID: p.AuthOpenStack.TenantID,
}
provider, err := openstack.AuthenticatedClient(opts)
if err != nil {
return err
}
dnsClient, err := openstack.NewDNSV2(provider, gophercloud.EndpointOpts{
Region: p.AuthOpenStack.RegionName,
})
if err != nil {
return err
}
p.dnsClient = dnsClient
return nil
}
func (p *Provider) setZone(zone string) error {
dot := zone[len(zone)-1:]
if dot != "." {
zone = zone + "."
}
zoneID, err := p.setZoneID(zone)
if err != nil {
return err
}
p.zoneID = zoneID
if p.zoneID == "" {
return errors.New("zoneID does not exist")
}
return nil
}
func (p *Provider) setZoneID(zoneName string) (string, error) {
listOpts := zones.ListOpts{}
allPages, err := zones.List(p.dnsClient, listOpts).AllPages()
if err != nil {
return "", fmt.Errorf("trying to get zones list: %v", err)
}
allZones, err := zones.ExtractZones(allPages)
if err != nil {
return "", fmt.Errorf("trying to extract zones: %v", err)
}
for _, zone := range allZones {
if zoneName == zone.Name {
return zone.ID, nil
}
}
return "", nil
}
func intToPointer(x int) *int {
return &x
}