2019-03-11 16:56:48 +00:00
|
|
|
package internal
|
2018-11-04 01:36:04 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2019-02-22 16:33:12 +00:00
|
|
|
"strings"
|
2018-11-04 01:36:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Domain represents domain name.
|
|
|
|
type Domain struct {
|
|
|
|
ID int `json:"id,omitempty"`
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Record represents DNS record.
|
|
|
|
type Record struct {
|
|
|
|
ID int `json:"id,omitempty"`
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
Type string `json:"type,omitempty"` // Record type (SOA, NS, A/AAAA, CNAME, SRV, MX, TXT, SPF)
|
|
|
|
TTL int `json:"ttl,omitempty"`
|
|
|
|
Email string `json:"email,omitempty"` // Email of domain's admin (only for SOA records)
|
|
|
|
Content string `json:"content,omitempty"` // Record content (not for SRV)
|
|
|
|
}
|
|
|
|
|
2018-11-08 09:31:42 +00:00
|
|
|
// APIError API error message
|
|
|
|
type APIError struct {
|
|
|
|
Description string `json:"error"`
|
|
|
|
Code int `json:"code"`
|
|
|
|
Field string `json:"field"`
|
|
|
|
}
|
|
|
|
|
2020-02-27 18:14:46 +00:00
|
|
|
func (a APIError) Error() string {
|
2018-11-08 09:31:42 +00:00
|
|
|
return fmt.Sprintf("API error: %d - %s - %s", a.Code, a.Description, a.Field)
|
2018-11-04 01:36:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ClientOpts represents options to init client.
|
|
|
|
type ClientOpts struct {
|
|
|
|
BaseURL string
|
|
|
|
Token string
|
|
|
|
UserAgent string
|
|
|
|
HTTPClient *http.Client
|
|
|
|
}
|
|
|
|
|
2018-11-08 09:31:42 +00:00
|
|
|
// Client represents DNS client.
|
|
|
|
type Client struct {
|
|
|
|
baseURL string
|
|
|
|
token string
|
|
|
|
userAgent string
|
|
|
|
httpClient *http.Client
|
2018-11-04 01:36:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient returns a client instance.
|
|
|
|
func NewClient(opts ClientOpts) *Client {
|
|
|
|
if opts.HTTPClient == nil {
|
|
|
|
opts.HTTPClient = &http.Client{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Client{
|
|
|
|
token: opts.Token,
|
|
|
|
baseURL: opts.BaseURL,
|
|
|
|
httpClient: opts.HTTPClient,
|
|
|
|
userAgent: opts.UserAgent,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-22 16:33:12 +00:00
|
|
|
// GetDomainByName gets Domain object by its name. If `domainName` level > 2 and there is
|
|
|
|
// no such domain on the account - it'll recursively search for the first
|
|
|
|
// which is exists in Vscale Domains API.
|
2018-11-04 01:36:04 +00:00
|
|
|
func (c *Client) GetDomainByName(domainName string) (*Domain, error) {
|
|
|
|
uri := fmt.Sprintf("/%s", domainName)
|
|
|
|
req, err := c.newRequest(http.MethodGet, uri, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
domain := &Domain{}
|
2019-02-22 16:33:12 +00:00
|
|
|
resp, err := c.do(req, domain)
|
2018-11-04 01:36:04 +00:00
|
|
|
if err != nil {
|
2019-02-22 16:33:12 +00:00
|
|
|
switch {
|
|
|
|
case resp.StatusCode == http.StatusNotFound && strings.Count(domainName, ".") > 1:
|
|
|
|
// Look up for the next sub domain
|
|
|
|
subIndex := strings.Index(domainName, ".")
|
|
|
|
return c.GetDomainByName(domainName[subIndex+1:])
|
|
|
|
default:
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-11-04 01:36:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return domain, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddRecord adds Record for given domain.
|
|
|
|
func (c *Client) AddRecord(domainID int, body Record) (*Record, error) {
|
|
|
|
uri := fmt.Sprintf("/%d/records/", domainID)
|
|
|
|
req, err := c.newRequest(http.MethodPost, uri, body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
record := &Record{}
|
|
|
|
_, err = c.do(req, record)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return record, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListRecords returns list records for specific domain.
|
|
|
|
func (c *Client) ListRecords(domainID int) ([]*Record, error) {
|
|
|
|
uri := fmt.Sprintf("/%d/records/", domainID)
|
|
|
|
req, err := c.newRequest(http.MethodGet, uri, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var records []*Record
|
|
|
|
_, err = c.do(req, &records)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return records, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteRecord deletes specific record.
|
|
|
|
func (c *Client) DeleteRecord(domainID, recordID int) error {
|
|
|
|
uri := fmt.Sprintf("/%d/records/%d", domainID, recordID)
|
|
|
|
req, err := c.newRequest(http.MethodDelete, uri, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = c.do(req, nil)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) newRequest(method, uri string, body interface{}) (*http.Request, error) {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
|
|
|
|
if body != nil {
|
|
|
|
err := json.NewEncoder(buf).Encode(body)
|
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, fmt.Errorf("failed to encode request body with error: %w", err)
|
2018-11-04 01:36:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest(method, c.baseURL+uri, buf)
|
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, fmt.Errorf("failed to create new http request with error: %w", err)
|
2018-11-04 01:36:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Add("X-Token", c.token)
|
|
|
|
req.Header.Add("Content-Type", "application/json")
|
|
|
|
req.Header.Add("Accept", "application/json")
|
|
|
|
|
|
|
|
return req, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) do(req *http.Request, to interface{}) (*http.Response, error) {
|
|
|
|
resp, err := c.httpClient.Do(req)
|
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, fmt.Errorf("request failed with error: %w", err)
|
2018-11-04 01:36:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = checkResponse(resp)
|
|
|
|
if err != nil {
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if to != nil {
|
2018-11-08 09:31:42 +00:00
|
|
|
if err = unmarshalBody(resp, to); err != nil {
|
|
|
|
return resp, err
|
2018-11-04 01:36:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkResponse(resp *http.Response) error {
|
2020-02-10 17:20:52 +00:00
|
|
|
if resp.StatusCode >= http.StatusBadRequest {
|
2018-11-04 01:36:04 +00:00
|
|
|
if resp.Body == nil {
|
|
|
|
return fmt.Errorf("request failed with status code %d and empty body", resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
apiError := APIError{}
|
|
|
|
err = json.Unmarshal(body, &apiError)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("request failed with status code %d, response body: %s", resp.StatusCode, string(body))
|
|
|
|
}
|
|
|
|
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("request failed with status code %d: %w", resp.StatusCode, apiError)
|
2018-11-04 01:36:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-11-08 09:31:42 +00:00
|
|
|
func unmarshalBody(resp *http.Response, to interface{}) error {
|
2018-11-04 01:36:04 +00:00
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2018-11-08 09:31:42 +00:00
|
|
|
err = json.Unmarshal(body, to)
|
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("unmarshaling error: %w: %s", err, string(body))
|
2018-11-08 09:31:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2018-11-04 01:36:04 +00:00
|
|
|
}
|