2021-11-01 21:02:31 +00:00
|
|
|
package internal
|
2018-12-06 21:50:17 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2021-11-01 21:02:31 +00:00
|
|
|
"net/url"
|
2020-04-17 20:38:13 +00:00
|
|
|
"path"
|
2021-11-01 21:02:31 +00:00
|
|
|
"time"
|
2018-12-06 21:50:17 +00:00
|
|
|
)
|
|
|
|
|
2021-11-01 21:02:31 +00:00
|
|
|
// DefaultBaseURL represents the API endpoint to call.
|
|
|
|
const DefaultBaseURL = "https://api.godaddy.com"
|
|
|
|
|
|
|
|
type Client struct {
|
|
|
|
HTTPClient *http.Client
|
|
|
|
baseURL *url.URL
|
|
|
|
apiKey string
|
|
|
|
apiSecret string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewClient(apiKey string, apiSecret string) *Client {
|
|
|
|
baseURL, _ := url.Parse(DefaultBaseURL)
|
|
|
|
|
|
|
|
return &Client{
|
|
|
|
HTTPClient: &http.Client{Timeout: 5 * time.Second},
|
|
|
|
baseURL: baseURL,
|
|
|
|
apiKey: apiKey,
|
|
|
|
apiSecret: apiSecret,
|
|
|
|
}
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
|
|
|
|
2021-11-01 21:02:31 +00:00
|
|
|
func (d *Client) GetRecords(domainZone, rType, recordName string) ([]DNSRecord, error) {
|
2020-04-17 20:38:13 +00:00
|
|
|
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 {
|
2021-08-25 09:44:11 +00:00
|
|
|
bodyBytes, _ := io.ReadAll(resp.Body)
|
2020-04-17 20:38:13 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-11-01 21:02:31 +00:00
|
|
|
func (d *Client) UpdateTxtRecords(records []DNSRecord, domainZone, 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 {
|
2021-08-25 09:44:11 +00:00
|
|
|
bodyBytes, _ := io.ReadAll(resp.Body)
|
2018-12-06 21:50:17 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-11-01 21:02:31 +00:00
|
|
|
func (d *Client) makeRequest(method, uri string, body io.Reader) (*http.Response, error) {
|
2023-02-09 16:19:58 +00:00
|
|
|
endpoint := d.baseURL.JoinPath(uri)
|
2021-11-01 21:02:31 +00:00
|
|
|
|
|
|
|
req, err := http.NewRequest(method, endpoint.String(), body)
|
2018-12-06 21:50:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("Accept", "application/json")
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
2021-11-01 21:02:31 +00:00
|
|
|
req.Header.Set("Authorization", fmt.Sprintf("sso-key %s:%s", d.apiKey, d.apiSecret))
|
2018-12-06 21:50:17 +00:00
|
|
|
|
2021-11-01 21:02:31 +00:00
|
|
|
return d.HTTPClient.Do(req)
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|