2017-02-07 21:33:23 +00:00
|
|
|
package egoscale
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2018-02-12 17:10:05 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2017-02-07 21:33:23 +00:00
|
|
|
"net/http"
|
2018-09-14 08:06:03 +00:00
|
|
|
"net/url"
|
2017-02-07 21:33:23 +00:00
|
|
|
"strconv"
|
2018-02-12 17:10:05 +00:00
|
|
|
"strings"
|
2017-02-07 21:33:23 +00:00
|
|
|
)
|
|
|
|
|
2018-02-12 17:10:05 +00:00
|
|
|
// DNSDomain represents a domain
|
|
|
|
type DNSDomain struct {
|
|
|
|
ID int64 `json:"id"`
|
|
|
|
AccountID int64 `json:"account_id,omitempty"`
|
|
|
|
UserID int64 `json:"user_id,omitempty"`
|
|
|
|
RegistrantID int64 `json:"registrant_id,omitempty"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
UnicodeName string `json:"unicode_name"`
|
|
|
|
Token string `json:"token"`
|
|
|
|
State string `json:"state"`
|
|
|
|
Language string `json:"language,omitempty"`
|
|
|
|
Lockable bool `json:"lockable"`
|
|
|
|
AutoRenew bool `json:"auto_renew"`
|
|
|
|
WhoisProtected bool `json:"whois_protected"`
|
|
|
|
RecordCount int64 `json:"record_count"`
|
|
|
|
ServiceCount int64 `json:"service_count"`
|
|
|
|
ExpiresOn string `json:"expires_on,omitempty"`
|
|
|
|
CreatedAt string `json:"created_at"`
|
|
|
|
UpdatedAt string `json:"updated_at"`
|
|
|
|
}
|
2017-02-07 21:33:23 +00:00
|
|
|
|
2018-02-12 17:10:05 +00:00
|
|
|
// DNSDomainResponse represents a domain creation response
|
|
|
|
type DNSDomainResponse struct {
|
|
|
|
Domain *DNSDomain `json:"domain"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// DNSRecord represents a DNS record
|
|
|
|
type DNSRecord struct {
|
|
|
|
ID int64 `json:"id,omitempty"`
|
|
|
|
DomainID int64 `json:"domain_id,omitempty"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
TTL int `json:"ttl,omitempty"`
|
|
|
|
CreatedAt string `json:"created_at,omitempty"`
|
|
|
|
UpdatedAt string `json:"updated_at,omitempty"`
|
|
|
|
Content string `json:"content"`
|
|
|
|
RecordType string `json:"record_type"`
|
|
|
|
Prio int `json:"prio,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// DNSRecordResponse represents the creation of a DNS record
|
|
|
|
type DNSRecordResponse struct {
|
|
|
|
Record DNSRecord `json:"record"`
|
|
|
|
}
|
|
|
|
|
2018-09-14 08:06:03 +00:00
|
|
|
// UpdateDNSRecord represents a DNS record
|
|
|
|
type UpdateDNSRecord struct {
|
|
|
|
ID int64 `json:"id,omitempty"`
|
|
|
|
DomainID int64 `json:"domain_id,omitempty"`
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
TTL int `json:"ttl,omitempty"`
|
|
|
|
CreatedAt string `json:"created_at,omitempty"`
|
|
|
|
UpdatedAt string `json:"updated_at,omitempty"`
|
|
|
|
Content string `json:"content,omitempty"`
|
|
|
|
RecordType string `json:"record_type,omitempty"`
|
|
|
|
Prio int `json:"prio,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateDNSRecordResponse represents the creation of a DNS record
|
|
|
|
type UpdateDNSRecordResponse struct {
|
|
|
|
Record UpdateDNSRecord `json:"record"`
|
|
|
|
}
|
|
|
|
|
2018-02-12 17:10:05 +00:00
|
|
|
// DNSErrorResponse represents an error in the API
|
|
|
|
type DNSErrorResponse struct {
|
2018-09-14 08:06:03 +00:00
|
|
|
Message string `json:"message,omitempty"`
|
|
|
|
Errors map[string][]string `json:"errors"`
|
2018-02-12 17:10:05 +00:00
|
|
|
}
|
|
|
|
|
2018-09-14 08:06:03 +00:00
|
|
|
// Record represent record type
|
|
|
|
type Record int
|
|
|
|
|
|
|
|
//go:generate stringer -type=Record
|
|
|
|
const (
|
|
|
|
// A record type
|
|
|
|
A Record = iota
|
|
|
|
// AAAA record type
|
|
|
|
AAAA
|
|
|
|
// ALIAS record type
|
|
|
|
ALIAS
|
|
|
|
// CNAME record type
|
|
|
|
CNAME
|
|
|
|
// HINFO record type
|
|
|
|
HINFO
|
|
|
|
// MX record type
|
|
|
|
MX
|
|
|
|
// NAPTR record type
|
|
|
|
NAPTR
|
|
|
|
// NS record type
|
|
|
|
NS
|
|
|
|
// POOL record type
|
|
|
|
POOL
|
|
|
|
// SPF record type
|
|
|
|
SPF
|
|
|
|
// SRV record type
|
|
|
|
SRV
|
|
|
|
// SSHFP record type
|
|
|
|
SSHFP
|
|
|
|
// TXT record type
|
|
|
|
TXT
|
|
|
|
// URL record type
|
|
|
|
URL
|
|
|
|
)
|
2018-02-12 17:10:05 +00:00
|
|
|
|
|
|
|
// Error formats the DNSerror into a string
|
2018-09-14 08:06:03 +00:00
|
|
|
func (req *DNSErrorResponse) Error() string {
|
|
|
|
if len(req.Errors) > 0 {
|
|
|
|
errs := []string{}
|
|
|
|
for name, ss := range req.Errors {
|
|
|
|
if len(ss) > 0 {
|
|
|
|
errs = append(errs, fmt.Sprintf("%s: %s", name, strings.Join(ss, ", ")))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("dns error: %s (%s)", req.Message, strings.Join(errs, "; "))
|
2018-02-12 17:10:05 +00:00
|
|
|
}
|
2018-09-14 08:06:03 +00:00
|
|
|
return fmt.Sprintf("dns error: %s", req.Message)
|
2018-02-12 17:10:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateDomain creates a DNS domain
|
2018-09-14 08:06:03 +00:00
|
|
|
func (client *Client) CreateDomain(name string) (*DNSDomain, error) {
|
2018-02-12 17:10:05 +00:00
|
|
|
m, err := json.Marshal(DNSDomainResponse{
|
|
|
|
Domain: &DNSDomain{
|
|
|
|
Name: name,
|
|
|
|
},
|
|
|
|
})
|
2017-10-31 09:42:03 +00:00
|
|
|
if err != nil {
|
2017-02-07 21:33:23 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-09-14 08:06:03 +00:00
|
|
|
resp, err := client.dnsRequest("/v1/domains", nil, string(m), "POST")
|
2017-02-07 21:33:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-02-12 17:10:05 +00:00
|
|
|
var d *DNSDomainResponse
|
2017-02-07 21:33:23 +00:00
|
|
|
if err := json.Unmarshal(resp, &d); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-02-12 17:10:05 +00:00
|
|
|
return d.Domain, nil
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
2018-02-12 17:10:05 +00:00
|
|
|
// GetDomain gets a DNS domain
|
2018-09-14 08:06:03 +00:00
|
|
|
func (client *Client) GetDomain(name string) (*DNSDomain, error) {
|
|
|
|
resp, err := client.dnsRequest("/v1/domains/"+name, nil, "", "GET")
|
2017-02-07 21:33:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-02-12 17:10:05 +00:00
|
|
|
var d *DNSDomainResponse
|
2017-02-07 21:33:23 +00:00
|
|
|
if err := json.Unmarshal(resp, &d); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-02-12 17:10:05 +00:00
|
|
|
return d.Domain, nil
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
2018-09-14 08:06:03 +00:00
|
|
|
// GetDomains gets DNS domains
|
|
|
|
func (client *Client) GetDomains() ([]DNSDomain, error) {
|
|
|
|
resp, err := client.dnsRequest("/v1/domains", nil, "", "GET")
|
2017-02-07 21:33:23 +00:00
|
|
|
if err != nil {
|
2018-09-14 08:06:03 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var d []DNSDomainResponse
|
|
|
|
if err := json.Unmarshal(resp, &d); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
domains := make([]DNSDomain, len(d))
|
|
|
|
for i := range d {
|
|
|
|
domains[i] = *d[i].Domain
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
2018-09-14 08:06:03 +00:00
|
|
|
return domains, nil
|
|
|
|
}
|
2017-02-07 21:33:23 +00:00
|
|
|
|
2018-09-14 08:06:03 +00:00
|
|
|
// DeleteDomain delets a DNS domain
|
|
|
|
func (client *Client) DeleteDomain(name string) error {
|
|
|
|
_, err := client.dnsRequest("/v1/domains/"+name, nil, "", "DELETE")
|
|
|
|
return err
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
2018-02-12 17:10:05 +00:00
|
|
|
// GetRecord returns a DNS record
|
2018-09-14 08:06:03 +00:00
|
|
|
func (client *Client) GetRecord(domain string, recordID int64) (*DNSRecord, error) {
|
2018-02-12 17:10:05 +00:00
|
|
|
id := strconv.FormatInt(recordID, 10)
|
2018-09-14 08:06:03 +00:00
|
|
|
resp, err := client.dnsRequest("/v1/domains/"+domain+"/records/"+id, nil, "", "GET")
|
2017-02-07 21:33:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-02-12 17:10:05 +00:00
|
|
|
var r DNSRecordResponse
|
2017-02-07 21:33:23 +00:00
|
|
|
if err = json.Unmarshal(resp, &r); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-02-12 17:10:05 +00:00
|
|
|
return &(r.Record), nil
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
2018-02-12 17:10:05 +00:00
|
|
|
// GetRecords returns the DNS records
|
2018-09-14 08:06:03 +00:00
|
|
|
func (client *Client) GetRecords(domain string) ([]DNSRecord, error) {
|
|
|
|
resp, err := client.dnsRequest("/v1/domains/"+domain+"/records", nil, "", "GET")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var r []DNSRecordResponse
|
|
|
|
if err = json.Unmarshal(resp, &r); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
records := make([]DNSRecord, 0, len(r))
|
|
|
|
for _, rec := range r {
|
|
|
|
records = append(records, rec.Record)
|
|
|
|
}
|
|
|
|
|
|
|
|
return records, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRecordsWithFilters returns the DNS records (filters can be empty)
|
|
|
|
func (client *Client) GetRecordsWithFilters(domain, name, recordType string) ([]DNSRecord, error) {
|
|
|
|
|
|
|
|
filters := url.Values{}
|
|
|
|
if name != "" {
|
|
|
|
filters.Add("name", name)
|
|
|
|
}
|
|
|
|
if recordType != "" {
|
|
|
|
filters.Add("record_type", recordType)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := client.dnsRequest("/v1/domains/"+domain+"/records", filters, "", "GET")
|
2018-02-12 17:10:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-02-07 21:33:23 +00:00
|
|
|
|
2018-02-12 17:10:05 +00:00
|
|
|
var r []DNSRecordResponse
|
|
|
|
if err = json.Unmarshal(resp, &r); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
records := make([]DNSRecord, 0, len(r))
|
|
|
|
for _, rec := range r {
|
|
|
|
records = append(records, rec.Record)
|
|
|
|
}
|
2017-02-07 21:33:23 +00:00
|
|
|
|
2018-02-12 17:10:05 +00:00
|
|
|
return records, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateRecord creates a DNS record
|
2018-09-14 08:06:03 +00:00
|
|
|
func (client *Client) CreateRecord(name string, rec DNSRecord) (*DNSRecord, error) {
|
2018-02-12 17:10:05 +00:00
|
|
|
body, err := json.Marshal(DNSRecordResponse{
|
|
|
|
Record: rec,
|
|
|
|
})
|
2017-10-31 09:42:03 +00:00
|
|
|
if err != nil {
|
2017-02-07 21:33:23 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-09-14 08:06:03 +00:00
|
|
|
resp, err := client.dnsRequest("/v1/domains/"+name+"/records", nil, string(body), "POST")
|
2017-02-07 21:33:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-02-12 17:10:05 +00:00
|
|
|
var r DNSRecordResponse
|
2017-02-07 21:33:23 +00:00
|
|
|
if err = json.Unmarshal(resp, &r); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-02-12 17:10:05 +00:00
|
|
|
return &(r.Record), nil
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
2018-02-12 17:10:05 +00:00
|
|
|
// UpdateRecord updates a DNS record
|
2018-09-14 08:06:03 +00:00
|
|
|
func (client *Client) UpdateRecord(name string, rec UpdateDNSRecord) (*DNSRecord, error) {
|
|
|
|
body, err := json.Marshal(UpdateDNSRecordResponse{
|
2018-02-12 17:10:05 +00:00
|
|
|
Record: rec,
|
|
|
|
})
|
2017-10-31 09:42:03 +00:00
|
|
|
if err != nil {
|
2017-02-07 21:33:23 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-02-12 17:10:05 +00:00
|
|
|
id := strconv.FormatInt(rec.ID, 10)
|
2018-09-14 08:06:03 +00:00
|
|
|
resp, err := client.dnsRequest("/v1/domains/"+name+"/records/"+id, nil, string(body), "PUT")
|
2017-02-07 21:33:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-02-12 17:10:05 +00:00
|
|
|
var r DNSRecordResponse
|
2017-02-07 21:33:23 +00:00
|
|
|
if err = json.Unmarshal(resp, &r); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-02-12 17:10:05 +00:00
|
|
|
return &(r.Record), nil
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
2018-02-12 17:10:05 +00:00
|
|
|
// DeleteRecord deletes a record
|
2018-09-14 08:06:03 +00:00
|
|
|
func (client *Client) DeleteRecord(name string, recordID int64) error {
|
2018-02-12 17:10:05 +00:00
|
|
|
id := strconv.FormatInt(recordID, 10)
|
2018-09-14 08:06:03 +00:00
|
|
|
_, err := client.dnsRequest("/v1/domains/"+name+"/records/"+id, nil, "", "DELETE")
|
2018-02-12 17:10:05 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-09-14 08:06:03 +00:00
|
|
|
func (client *Client) dnsRequest(uri string, urlValues url.Values, params, method string) (json.RawMessage, error) {
|
|
|
|
rawURL := client.Endpoint + uri
|
|
|
|
url, err := url.Parse(rawURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
q := url.Query()
|
|
|
|
for k, vs := range urlValues {
|
|
|
|
for _, v := range vs {
|
|
|
|
q.Add(k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
url.RawQuery = q.Encode()
|
|
|
|
|
|
|
|
req, err := http.NewRequest(method, url.String(), strings.NewReader(params))
|
2018-02-12 17:10:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-02-07 21:33:23 +00:00
|
|
|
var hdr = make(http.Header)
|
2018-09-14 08:06:03 +00:00
|
|
|
hdr.Add("X-DNS-TOKEN", client.APIKey+":"+client.apiSecret)
|
|
|
|
hdr.Add("User-Agent", fmt.Sprintf("exoscale/egoscale (%v)", Version))
|
2017-02-07 21:33:23 +00:00
|
|
|
hdr.Add("Accept", "application/json")
|
2018-02-12 17:10:05 +00:00
|
|
|
if params != "" {
|
|
|
|
hdr.Add("Content-Type", "application/json")
|
|
|
|
}
|
|
|
|
req.Header = hdr
|
2017-02-07 21:33:23 +00:00
|
|
|
|
2018-09-14 08:06:03 +00:00
|
|
|
resp, err := client.HTTPClient.Do(req)
|
2017-02-07 21:33:23 +00:00
|
|
|
if err != nil {
|
2018-02-12 17:10:05 +00:00
|
|
|
return nil, err
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
2018-09-14 08:06:03 +00:00
|
|
|
defer resp.Body.Close() // nolint: errcheck
|
|
|
|
|
|
|
|
contentType := resp.Header.Get("content-type")
|
|
|
|
if !strings.Contains(contentType, "application/json") {
|
|
|
|
return nil, fmt.Errorf(`response content-type expected to be "application/json", got %q`, contentType)
|
|
|
|
}
|
2017-02-07 21:33:23 +00:00
|
|
|
|
2018-09-14 08:06:03 +00:00
|
|
|
b, err := ioutil.ReadAll(resp.Body)
|
2018-02-12 17:10:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-09-14 08:06:03 +00:00
|
|
|
if resp.StatusCode >= 400 {
|
|
|
|
e := new(DNSErrorResponse)
|
|
|
|
if err := json.Unmarshal(b, e); err != nil {
|
2018-02-12 17:10:05 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-09-14 08:06:03 +00:00
|
|
|
return nil, e
|
2018-02-12 17:10:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return b, nil
|
2017-10-31 09:42:03 +00:00
|
|
|
}
|