-
Notifications
You must be signed in to change notification settings - Fork 56
/
domains.go
190 lines (154 loc) · 5.47 KB
/
domains.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
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
package govultr
import (
"context"
"fmt"
"net/http"
"github.com/google/go-querystring/query"
)
const domainPath = "/v2/domains"
// DomainService is the interface to interact with the DNS endpoints on the Vultr API
// https://www.vultr.com/api/#tag/dns
type DomainService interface {
Create(ctx context.Context, domainReq *DomainReq) (*Domain, *http.Response, error)
Get(ctx context.Context, domain string) (*Domain, *http.Response, error)
Update(ctx context.Context, domain, dnsSec string) error
Delete(ctx context.Context, domain string) error
List(ctx context.Context, options *ListOptions) ([]Domain, *Meta, *http.Response, error)
GetSoa(ctx context.Context, domain string) (*Soa, *http.Response, error)
UpdateSoa(ctx context.Context, domain string, soaReq *Soa) error
GetDNSSec(ctx context.Context, domain string) ([]string, *http.Response, error)
}
// DomainServiceHandler handles interaction with the DNS methods for the Vultr API
type DomainServiceHandler struct {
client *Client
}
// Domain represents a Domain entry on Vultr
type Domain struct {
Domain string `json:"domain,omitempty"`
DateCreated string `json:"date_created,omitempty"`
DNSSec string `json:"dns_sec,omitempty"`
}
// DomainReq is the struct to create a domain
// If IP is omitted then an empty DNS entry will be created. If supplied the domain will be pre populated with entries
type DomainReq struct {
Domain string `json:"domain,omitempty"`
IP string `json:"ip,omitempty"`
DNSSec string `json:"dns_sec,omitempty"`
}
type domainsBase struct {
Domains []Domain `json:"domains"`
Meta *Meta `json:"meta"`
}
type domainBase struct {
Domain *Domain `json:"domain"`
}
// Soa information for the Domain
type Soa struct {
NSPrimary string `json:"nsprimary,omitempty"`
Email string `json:"email,omitempty"`
}
type soaBase struct {
DNSSoa *Soa `json:"dns_soa,omitempty"`
}
type dnsSecBase struct {
DNSSec []string `json:"dns_sec,omitempty"`
}
// Create a domain entry
func (d *DomainServiceHandler) Create(ctx context.Context, domainReq *DomainReq) (*Domain, *http.Response, error) {
req, err := d.client.NewRequest(ctx, http.MethodPost, domainPath, domainReq)
if err != nil {
return nil, nil, err
}
domain := new(domainBase)
resp, err := d.client.DoWithContext(ctx, req, domain)
if err != nil {
return nil, resp, err
}
return domain.Domain, resp, nil
}
// Get a domain from your Vultr account.
func (d *DomainServiceHandler) Get(ctx context.Context, domain string) (*Domain, *http.Response, error) {
req, err := d.client.NewRequest(ctx, http.MethodGet, fmt.Sprintf("%s/%s", domainPath, domain), nil)
if err != nil {
return nil, nil, err
}
dBase := new(domainBase)
resp, err := d.client.DoWithContext(ctx, req, dBase)
if err != nil {
return nil, resp, err
}
return dBase.Domain, resp, nil
}
// Update allows you to enable or disable DNS Sec on the domain.
// The two valid options for dnsSec are "enabled" or "disabled"
func (d *DomainServiceHandler) Update(ctx context.Context, domain, dnsSec string) error {
body := &RequestBody{"dns_sec": dnsSec}
req, err := d.client.NewRequest(ctx, http.MethodPut, fmt.Sprintf("%s/%s", domainPath, domain), body)
if err != nil {
return err
}
_, err = d.client.DoWithContext(ctx, req, nil)
return err
}
// Delete a domain with all associated records.
func (d *DomainServiceHandler) Delete(ctx context.Context, domain string) error {
req, err := d.client.NewRequest(ctx, http.MethodDelete, fmt.Sprintf("%s/%s", domainPath, domain), nil)
if err != nil {
return err
}
_, err = d.client.DoWithContext(ctx, req, nil)
return err
}
// List gets all domains associated with the current Vultr account.
func (d *DomainServiceHandler) List(ctx context.Context, options *ListOptions) ([]Domain, *Meta, *http.Response, error) { //nolint:dupl
req, err := d.client.NewRequest(ctx, http.MethodGet, domainPath, nil)
if err != nil {
return nil, nil, nil, err
}
newValues, err := query.Values(options)
if err != nil {
return nil, nil, nil, err
}
req.URL.RawQuery = newValues.Encode()
domains := new(domainsBase)
resp, err := d.client.DoWithContext(ctx, req, domains)
if err != nil {
return nil, nil, resp, err
}
return domains.Domains, domains.Meta, resp, nil
}
// GetSoa gets the SOA record information for a domain
func (d *DomainServiceHandler) GetSoa(ctx context.Context, domain string) (*Soa, *http.Response, error) {
req, err := d.client.NewRequest(ctx, http.MethodGet, fmt.Sprintf("%s/%s/soa", domainPath, domain), nil)
if err != nil {
return nil, nil, err
}
soa := new(soaBase)
resp, err := d.client.DoWithContext(ctx, req, soa)
if err != nil {
return nil, resp, err
}
return soa.DNSSoa, resp, nil
}
// UpdateSoa will update the SOA record information for a domain.
func (d *DomainServiceHandler) UpdateSoa(ctx context.Context, domain string, soaReq *Soa) error {
req, err := d.client.NewRequest(ctx, http.MethodPatch, fmt.Sprintf("%s/%s/soa", domainPath, domain), soaReq)
if err != nil {
return err
}
_, err = d.client.DoWithContext(ctx, req, nil)
return err
}
// GetDNSSec gets the DNSSec keys for a domain (if enabled)
func (d *DomainServiceHandler) GetDNSSec(ctx context.Context, domain string) ([]string, *http.Response, error) {
req, err := d.client.NewRequest(ctx, http.MethodGet, fmt.Sprintf("%s/%s/dnssec", domainPath, domain), nil)
if err != nil {
return nil, nil, err
}
dnsSec := new(dnsSecBase)
resp, err := d.client.DoWithContext(ctx, req, dnsSec)
if err != nil {
return nil, resp, err
}
return dnsSec.DNSSec, resp, nil
}