2019-03-11 16:56:48 +00:00
|
|
|
package godaddy
|
2018-12-06 21:50:17 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2020-04-17 20:38:13 +00:00
|
|
|
"path"
|
2018-12-06 21:50:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// DNSRecord a DNS record
|
|
|
|
type DNSRecord struct {
|
2020-04-17 20:38:13 +00:00
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
Type string `json:"type,omitempty"`
|
2018-12-06 21:50:17 +00:00
|
|
|
Data string `json:"data"`
|
|
|
|
Priority int `json:"priority,omitempty"`
|
|
|
|
TTL int `json:"ttl,omitempty"`
|
|
|
|
}
|
|
|
|
|
2020-04-17 20:38:13 +00:00
|
|
|
func (d *DNSProvider) getRecords(domainZone string, rType string, recordName string) ([]DNSRecord, error) {
|
|
|
|
resource := path.Clean(fmt.Sprintf("/v1/domains/%s/records/%s/%s", domainZone, rType, recordName))
|
|
|
|
|
|
|
|
resp, err := d.makeRequest(http.MethodGet, resource, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() { _ = resp.Body.Close() }()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
bodyBytes, _ := ioutil.ReadAll(resp.Body)
|
|
|
|
return nil, fmt.Errorf("could not get records: Domain: %s; Record: %s, Status: %v; Body: %s",
|
|
|
|
domainZone, recordName, resp.StatusCode, string(bodyBytes))
|
|
|
|
}
|
|
|
|
|
|
|
|
var records []DNSRecord
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&records)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return records, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DNSProvider) updateTxtRecords(records []DNSRecord, domainZone string, recordName string) error {
|
2018-12-06 21:50:17 +00:00
|
|
|
body, err := json.Marshal(records)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-17 20:38:13 +00:00
|
|
|
resource := path.Clean(fmt.Sprintf("/v1/domains/%s/records/TXT/%s", domainZone, recordName))
|
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
var resp *http.Response
|
2020-04-17 20:38:13 +00:00
|
|
|
resp, err = d.makeRequest(http.MethodPut, resource, bytes.NewReader(body))
|
2018-12-06 21:50:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-17 20:38:13 +00:00
|
|
|
defer func() { _ = resp.Body.Close() }()
|
2018-12-06 21:50:17 +00:00
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
bodyBytes, _ := ioutil.ReadAll(resp.Body)
|
|
|
|
return fmt.Errorf("could not create record %v; Status: %v; Body: %s", string(body), resp.StatusCode, string(bodyBytes))
|
|
|
|
}
|
2020-04-17 20:38:13 +00:00
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DNSProvider) makeRequest(method, uri string, body io.Reader) (*http.Response, error) {
|
|
|
|
req, err := http.NewRequest(method, fmt.Sprintf("%s%s", defaultBaseURL, uri), body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("Accept", "application/json")
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
req.Header.Set("Authorization", fmt.Sprintf("sso-key %s:%s", d.config.APIKey, d.config.APISecret))
|
|
|
|
|
|
|
|
return d.config.HTTPClient.Do(req)
|
|
|
|
}
|