-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
105 lines (78 loc) · 2.16 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
package arvancloud
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/libdns/libdns"
)
var (
baseURL = "https://napi.arvancloud.com/cdn/4.0"
)
func (p *Provider) getRecords(ctx context.Context, zone string) ([]libdns.Record, error) {
p.mutex.Lock()
defer p.mutex.Unlock()
url := fmt.Sprintf("%s/domains/%s/dns-records", baseURL, zone)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", p.APIToken)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return nil, fmt.Errorf("failed with status code %d", resp.StatusCode)
}
var recordList DnsRecordList
err = json.NewDecoder(resp.Body).Decode(&recordList)
if err != nil {
return nil, err
}
var records []libdns.Record
for _, item := range recordList.Data {
records = append(records, libdns.Record{
ID: item.ID,
Type: item.Type,
Name: item.Name,
Value: item.Value,
TTL: time.Duration(item.TTL) * time.Second,
})
}
return records, nil
}
func (p *Provider) appendRecord(ctx context.Context, zone string, record libdns.Record) error {
p.mutex.Lock()
defer p.mutex.Unlock()
url := fmt.Sprintf("%s/domains/%s/dns-records", baseURL, zone)
dnsRecord := fromLibdns(record)
buf := new(bytes.Buffer)
err := json.NewEncoder(buf).Encode(dnsRecord)
if err != nil {
return err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, buf)
if err != nil {
return err
}
req.Header.Set("Authorization", p.APIToken)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return fmt.Errorf("failed with status code %d", resp.StatusCode)
}
return nil
}
func (p *Provider) setRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {
return []libdns.Record{}, nil
}
func (p *Provider) deleteRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {
return []libdns.Record{}, nil
}