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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
package govultr
import (
"context"
"net/http"
"net/url"
)
// DNSDomainService is the interface to interact with the DNS endpoints on the Vultr API
// Link: https://www.vultr.com/api/#dns
type DNSDomainService interface {
Create(ctx context.Context, domain, InstanceIP string) error
Delete(ctx context.Context, domain string) error
ToggleDNSSec(ctx context.Context, domain string, enabled bool) error
DNSSecInfo(ctx context.Context, domain string) ([]string, error)
List(ctx context.Context) ([]DNSDomain, error)
GetSoa(ctx context.Context, domain string) (*Soa, error)
UpdateSoa(ctx context.Context, domain, nsPrimary, email string) error
}
// DNSDomainServiceHandler handles interaction with the DNS methods for the Vultr API
type DNSDomainServiceHandler struct {
client *Client
}
// DNSDomain represents a DNS Domain entry on Vultr
type DNSDomain struct {
Domain string `json:"domain"`
DateCreated string `json:"date_created"`
}
// Soa represents record information for a domain on Vultr
type Soa struct {
NsPrimary string `json:"nsprimary"`
Email string `json:"email"`
}
// Create will create a DNS Domain entry on Vultr
func (d *DNSDomainServiceHandler) Create(ctx context.Context, domain, InstanceIP string) error {
uri := "/v1/dns/create_domain"
values := url.Values{
"domain": {domain},
"serverip": {InstanceIP},
}
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 *DNSDomainServiceHandler) Delete(ctx context.Context, domain string) error {
uri := "/v1/dns/delete_domain"
values := url.Values{
"domain": {domain},
}
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
}
// ToggleDNSSec will enable or disable DNSSEC for a domain on Vultr
func (d *DNSDomainServiceHandler) ToggleDNSSec(ctx context.Context, domain string, enabled bool) error {
uri := "/v1/dns/dnssec_enable"
enable := "no"
if enabled == true {
enable = "yes"
}
values := url.Values{
"domain": {domain},
"enable": {enable},
}
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
}
// DNSSecInfo gets the DNSSec keys for a domain (if enabled)
func (d *DNSDomainServiceHandler) DNSSecInfo(ctx context.Context, domain string) ([]string, error) {
uri := "/v1/dns/dnssec_info"
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 DNSSec []string
err = d.client.DoWithContext(ctx, req, &DNSSec)
if err != nil {
return nil, err
}
return DNSSec, nil
}
// List gets all domains associated with the current Vultr account.
func (d *DNSDomainServiceHandler) List(ctx context.Context) ([]DNSDomain, error) {
uri := "/v1/dns/list"
req, err := d.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, err
}
var dnsDomains []DNSDomain
err = d.client.DoWithContext(ctx, req, &dnsDomains)
if err != nil {
return nil, err
}
return dnsDomains, nil
}
// GetSoa gets the SOA record information for a domain
func (d *DNSDomainServiceHandler) GetSoa(ctx context.Context, domain string) (*Soa, error) {
uri := "/v1/dns/soa_info"
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()
soa := new(Soa)
err = d.client.DoWithContext(ctx, req, soa)
if err != nil {
return nil, err
}
return soa, nil
}
// UpdateSoa will update the SOA record information for a domain.
func (d *DNSDomainServiceHandler) UpdateSoa(ctx context.Context, domain, nsPrimary, email string) error {
uri := "/v1/dns/soa_update"
values := url.Values{
"domain": {domain},
"nsprimary": {nsPrimary},
"email": {email},
}
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
}
|