This repository has been archived by the owner on Feb 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
143 lines (124 loc) · 2.94 KB
/
client.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
package civo
import (
"context"
"github.com/libdns/libdns"
"sync"
"time"
"github.com/civo/civogo"
)
type Client struct {
client *civogo.Client
mutex sync.Mutex
}
func (p *Provider) getClient() error {
if p.client == nil {
var err error
// DNS is region independent, we can use any region
p.client, err = civogo.NewClient(p.APIToken, "LON1")
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
}
var records []libdns.Record
domain, err := p.client.GetDNSDomain(zone)
if err != nil {
return nil, err
}
dnsRecords, err := p.client.ListDNSRecords(domain.ID)
if err != nil {
return nil, err
}
for _, entry := range dnsRecords {
record := libdns.Record{
Name: libdns.RelativeName(entry.Name, zone),
Value: entry.Value,
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
}
domain, err := p.client.GetDNSDomain(zone)
if err != nil {
return record, err
}
dnsRecord, err := p.client.CreateDNSRecord(domain.ID, &civogo.DNSRecordConfig{
Name: libdns.AbsoluteName(record.Name, zone),
Value: record.Value,
Type: civogo.DNSRecordType(record.Type),
TTL: int(record.TTL.Seconds()),
})
if err != nil {
return libdns.Record{}, err
}
if err != nil {
return record, err
}
record.ID = dnsRecord.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
}
domain, err := p.client.GetDNSDomain(zone)
if err != nil {
return record, err
}
dnsRecord, err := p.client.GetDNSRecord(domain.ID, record.ID)
if err != nil {
return record, err
}
_, err = p.client.DeleteDNSRecord(dnsRecord)
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
}
domain, err := p.client.GetDNSDomain(zone)
if err != nil {
return record, err
}
dnsRecord, err := p.client.GetDNSRecord(domain.ID, record.ID)
if err != nil {
return record, err
}
_, err = p.client.UpdateDNSRecord(dnsRecord, &civogo.DNSRecordConfig{
Name: libdns.AbsoluteName(record.Name, zone),
Value: record.Value,
Type: civogo.DNSRecordType(record.Type),
TTL: int(record.TTL.Seconds()),
})
if err != nil {
return record, err
}
return record, nil
}