traefik/vendor/github.com/exoscale/egoscale/dns.go

154 lines
3.3 KiB
Go
Raw Normal View History

2017-02-07 21:33:23 +00:00
package egoscale
import (
"encoding/json"
"net/http"
"strconv"
)
func (exo *Client) CreateDomain(name string) (*DNSDomain, error) {
var hdr = make(http.Header)
var domain DNSDomainCreateRequest
2017-10-31 09:42:03 +00:00
hdr.Add("X-DNS-TOKEN", exo.apiKey+":"+exo.apiSecret)
2017-02-07 21:33:23 +00:00
domain.Domain.Name = name
2017-10-31 09:42:03 +00:00
m, err := json.Marshal(domain)
if err != nil {
2017-02-07 21:33:23 +00:00
return nil, err
}
resp, err := exo.DetailedRequest("/v1/domains", string(m), "POST", hdr)
if err != nil {
return nil, err
}
var d *DNSDomain
if err := json.Unmarshal(resp, &d); err != nil {
return nil, err
}
return d, nil
}
func (exo *Client) GetDomain(name string) (*DNSDomain, error) {
var hdr = make(http.Header)
2017-10-31 09:42:03 +00:00
hdr.Add("X-DNS-TOKEN", exo.apiKey+":"+exo.apiSecret)
2017-02-07 21:33:23 +00:00
hdr.Add("Accept", "application/json")
2017-10-31 09:42:03 +00:00
resp, err := exo.DetailedRequest("/v1/domains/"+name, "", "GET", hdr)
2017-02-07 21:33:23 +00:00
if err != nil {
return nil, err
}
var d *DNSDomain
if err := json.Unmarshal(resp, &d); err != nil {
return nil, err
}
return d, nil
}
2017-10-31 09:42:03 +00:00
func (exo *Client) DeleteDomain(name string) error {
2017-02-07 21:33:23 +00:00
var hdr = make(http.Header)
2017-10-31 09:42:03 +00:00
hdr.Add("X-DNS-TOKEN", exo.apiKey+":"+exo.apiSecret)
2017-02-07 21:33:23 +00:00
hdr.Add("Accept", "application/json")
2017-10-31 09:42:03 +00:00
_, err := exo.DetailedRequest("/v1/domains/"+name, "", "DELETE", hdr)
2017-02-07 21:33:23 +00:00
if err != nil {
return err
}
return nil
}
func (exo *Client) GetRecords(name string) ([]*DNSRecordResponse, error) {
var hdr = make(http.Header)
2017-10-31 09:42:03 +00:00
hdr.Add("X-DNS-TOKEN", exo.apiKey+":"+exo.apiSecret)
2017-02-07 21:33:23 +00:00
hdr.Add("Accept", "application/json")
2017-10-31 09:42:03 +00:00
resp, err := exo.DetailedRequest("/v1/domains/"+name+"/records", "", "GET", hdr)
2017-02-07 21:33:23 +00:00
if err != nil {
return nil, err
}
var r []*DNSRecordResponse
if err = json.Unmarshal(resp, &r); err != nil {
return nil, err
}
return r, nil
}
2017-10-31 09:42:03 +00:00
func (exo *Client) CreateRecord(name string, rec DNSRecord) (*DNSRecordResponse, error) {
2017-02-07 21:33:23 +00:00
var hdr = make(http.Header)
2017-10-31 09:42:03 +00:00
hdr.Add("X-DNS-TOKEN", exo.apiKey+":"+exo.apiSecret)
2017-02-07 21:33:23 +00:00
hdr.Add("Accept", "application/json")
hdr.Add("Content-Type", "application/json")
var rr DNSRecordResponse
rr.Record = rec
2017-10-31 09:42:03 +00:00
body, err := json.Marshal(rr)
if err != nil {
2017-02-07 21:33:23 +00:00
return nil, err
}
2017-10-31 09:42:03 +00:00
resp, err := exo.DetailedRequest("/v1/domains/"+name+"/records", string(body), "POST", hdr)
2017-02-07 21:33:23 +00:00
if err != nil {
return nil, err
}
var r *DNSRecordResponse
if err = json.Unmarshal(resp, &r); err != nil {
return nil, err
}
2017-10-31 09:42:03 +00:00
return r, nil
2017-02-07 21:33:23 +00:00
}
2017-10-31 09:42:03 +00:00
func (exo *Client) UpdateRecord(name string, rec DNSRecord) (*DNSRecordResponse, error) {
2017-02-07 21:33:23 +00:00
var hdr = make(http.Header)
id := strconv.FormatInt(rec.Id, 10)
2017-10-31 09:42:03 +00:00
hdr.Add("X-DNS-TOKEN", exo.apiKey+":"+exo.apiSecret)
2017-02-07 21:33:23 +00:00
hdr.Add("Accept", "application/json")
hdr.Add("Content-Type", "application/json")
var rr DNSRecordResponse
rr.Record = rec
2017-10-31 09:42:03 +00:00
body, err := json.Marshal(rr)
if err != nil {
2017-02-07 21:33:23 +00:00
return nil, err
}
2017-10-31 09:42:03 +00:00
resp, err := exo.DetailedRequest("/v1/domains/"+name+"/records/"+id,
2017-02-07 21:33:23 +00:00
string(body), "PUT", hdr)
if err != nil {
return nil, err
}
var r *DNSRecordResponse
if err = json.Unmarshal(resp, &r); err != nil {
return nil, err
}
return r, nil
}
2017-10-31 09:42:03 +00:00
func (exo *Client) DeleteRecord(name string, rec DNSRecord) error {
2017-02-07 21:33:23 +00:00
var hdr = make(http.Header)
id := strconv.FormatInt(rec.Id, 10)
2017-10-31 09:42:03 +00:00
hdr.Add("X-DNS-TOKEN", exo.apiKey+":"+exo.apiSecret)
2017-02-07 21:33:23 +00:00
hdr.Add("Accept", "application/json")
hdr.Add("Content-Type", "application/json")
2017-10-31 09:42:03 +00:00
_, err := exo.DetailedRequest("/v1/domains/"+name+"/records/"+id,
2017-02-07 21:33:23 +00:00
"", "DELETE", hdr)
if err != nil {
return err
}
return nil
2017-10-31 09:42:03 +00:00
}