forked from vultr/govultr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dns_records.go
150 lines (113 loc) · 3.27 KB
/
dns_records.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
package govultr
import (
"context"
"net/http"
"net/url"
"strconv"
)
// DNSRecordService is the interface to interact with the DNS Records endpoints on the Vultr API
// Link: https://www.vultr.com/api/#dns
type DNSRecordService interface {
Create(ctx context.Context, domain, recordType, name, data string, ttl, priority int) error
Delete(ctx context.Context, domain, recordID string) error
List(ctx context.Context, domain string) ([]DNSRecord, error)
Update(ctx context.Context, domain string, dnsRecord *DNSRecord) error
}
// DNSRecordsServiceHandler handles interaction with the DNS Records methods for the Vultr API
type DNSRecordsServiceHandler struct {
client *Client
}
// DNSRecord represents a DNS record on Vultr
type DNSRecord struct {
RecordID int `json:"RECORDID"`
Type string `json:"type"`
Name string `json:"name"`
Data string `json:"data"`
Priority *int `json:"priority"`
TTL int `json:"ttl"`
}
// Create will add a DNS record.
func (d *DNSRecordsServiceHandler) Create(ctx context.Context, domain, recordType, name, data string, ttl, priority int) error {
uri := "/v1/dns/create_record"
values := url.Values{
"domain": {domain},
"name": {name},
"type": {recordType},
"data": {data},
"ttl": {strconv.Itoa(ttl)},
"priority": {strconv.Itoa(priority)},
}
req, err := d.client.NewRequest(ctx, http.MethodPost, uri, values)
if err != nil {
return err
}
err = d.client.DoWithContext(ctx, req, nil)
if err != nil {
return err
}
return nil
}
// Delete will delete a domain name and all associated records.
func (d *DNSRecordsServiceHandler) Delete(ctx context.Context, domain, recordID string) error {
uri := "/v1/dns/delete_record"
values := url.Values{
"domain": {domain},
"RECORDID": {recordID},
}
req, err := d.client.NewRequest(ctx, http.MethodPost, uri, values)
if err != nil {
return err
}
err = d.client.DoWithContext(ctx, req, nil)
if err != nil {
return err
}
return nil
}
// List will list all the records associated with a particular domain on Vultr
func (d *DNSRecordsServiceHandler) List(ctx context.Context, domain string) ([]DNSRecord, error) {
uri := "/v1/dns/records"
req, err := d.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, err
}
q := req.URL.Query()
q.Add("domain", domain)
req.URL.RawQuery = q.Encode()
var dnsRecord []DNSRecord
err = d.client.DoWithContext(ctx, req, &dnsRecord)
if err != nil {
return nil, err
}
return dnsRecord, nil
}
// Update will update a DNS record
func (d *DNSRecordsServiceHandler) Update(ctx context.Context, domain string, dnsRecord *DNSRecord) error {
uri := "/v1/dns/update_record"
values := url.Values{
"domain": {domain},
"RECORDID": {strconv.Itoa(dnsRecord.RecordID)},
}
// Optional
if dnsRecord.Name != "" {
values.Add("name", dnsRecord.Name)
}
if dnsRecord.Data != "" {
values.Add("data", dnsRecord.Data)
}
if dnsRecord.TTL != 0 {
values.Add("ttl", strconv.Itoa(dnsRecord.TTL))
}
if dnsRecord.Priority != nil {
values.Add("priority", strconv.Itoa(*dnsRecord.Priority))
}
req, err := d.client.NewRequest(ctx, http.MethodPost, uri, values)
if err != nil {
return err
}
err = d.client.DoWithContext(ctx, req, nil)
if err != nil {
return err
}
return nil
}