2020-03-15 16:17:17 +00:00
|
|
|
package selectel
|
2018-11-04 01:36:04 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2023-05-05 07:49:38 +00:00
|
|
|
"context"
|
2018-11-04 01:36:04 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2021-08-25 09:44:11 +00:00
|
|
|
"io"
|
2018-11-04 01:36:04 +00:00
|
|
|
"net/http"
|
2023-05-05 07:49:38 +00:00
|
|
|
"net/url"
|
|
|
|
"strconv"
|
2019-02-22 16:24:17 +00:00
|
|
|
"strings"
|
2023-05-05 07:49:38 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-acme/lego/v4/providers/dns/internal/errutils"
|
2018-11-04 01:36:04 +00:00
|
|
|
)
|
|
|
|
|
2020-03-15 16:17:17 +00:00
|
|
|
// Base URL for the Selectel/VScale DNS services.
|
|
|
|
const (
|
|
|
|
DefaultSelectelBaseURL = "https://api.selectel.ru/domains/v1"
|
|
|
|
DefaultVScaleBaseURL = "https://api.vscale.io/v1/domains"
|
|
|
|
)
|
2018-11-04 01:36:04 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
const tokenHeader = "X-Token"
|
|
|
|
|
2018-11-08 09:31:42 +00:00
|
|
|
// Client represents DNS client.
|
|
|
|
type Client struct {
|
2023-05-05 07:49:38 +00:00
|
|
|
token string
|
|
|
|
|
|
|
|
BaseURL *url.URL
|
2020-03-15 16:17:17 +00:00
|
|
|
HTTPClient *http.Client
|
2018-11-04 01:36:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient returns a client instance.
|
2020-03-15 16:17:17 +00:00
|
|
|
func NewClient(token string) *Client {
|
2023-05-05 07:49:38 +00:00
|
|
|
baseURL, _ := url.Parse(DefaultVScaleBaseURL)
|
|
|
|
|
2018-11-04 01:36:04 +00:00
|
|
|
return &Client{
|
2020-03-15 16:17:17 +00:00
|
|
|
token: token,
|
2023-05-05 07:49:38 +00:00
|
|
|
BaseURL: baseURL,
|
|
|
|
HTTPClient: &http.Client{Timeout: 5 * time.Second},
|
2018-11-04 01:36:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-22 16:24:17 +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 Selectel Domain API.
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c *Client) GetDomainByName(ctx context.Context, domainName string) (*Domain, error) {
|
|
|
|
req, err := newJSONRequest(ctx, http.MethodGet, c.BaseURL.JoinPath(domainName), nil)
|
2018-11-04 01:36:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
domain := &Domain{}
|
2023-05-05 07:49:38 +00:00
|
|
|
statusCode, err := c.do(req, domain)
|
2018-11-04 01:36:04 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
if statusCode == http.StatusNotFound && strings.Count(domainName, ".") > 1 {
|
2023-07-29 10:59:24 +00:00
|
|
|
// Look up for the next subdomain
|
2019-02-22 16:24:17 +00:00
|
|
|
subIndex := strings.Index(domainName, ".")
|
2023-05-05 07:49:38 +00:00
|
|
|
return c.GetDomainByName(ctx, domainName[subIndex+1:])
|
2019-02-22 16:24:17 +00:00
|
|
|
}
|
2020-03-15 16:17:17 +00:00
|
|
|
|
|
|
|
return nil, err
|
2018-11-04 01:36:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return domain, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddRecord adds Record for given domain.
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c *Client) AddRecord(ctx context.Context, domainID int, body Record) (*Record, error) {
|
|
|
|
req, err := newJSONRequest(ctx, http.MethodPost, c.BaseURL.JoinPath(strconv.Itoa(domainID), "records", "/"), body)
|
2018-11-04 01:36:04 +00:00
|
|
|
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.
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c *Client) ListRecords(ctx context.Context, domainID int) ([]Record, error) {
|
|
|
|
req, err := newJSONRequest(ctx, http.MethodGet, c.BaseURL.JoinPath(strconv.Itoa(domainID), "records", "/"), nil)
|
2018-11-04 01:36:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-03-15 16:17:17 +00:00
|
|
|
var records []Record
|
2018-11-04 01:36:04 +00:00
|
|
|
_, err = c.do(req, &records)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-05-05 07:49:38 +00:00
|
|
|
|
2018-11-04 01:36:04 +00:00
|
|
|
return records, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteRecord deletes specific record.
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c *Client) DeleteRecord(ctx context.Context, domainID, recordID int) error {
|
|
|
|
endpoint := c.BaseURL.JoinPath(strconv.Itoa(domainID), "records", strconv.Itoa(recordID))
|
|
|
|
|
|
|
|
req, err := newJSONRequest(ctx, http.MethodDelete, endpoint, nil)
|
2018-11-04 01:36:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = c.do(req, nil)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c *Client) do(req *http.Request, result any) (int, error) {
|
|
|
|
req.Header.Set(tokenHeader, c.token)
|
2018-11-04 01:36:04 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
resp, err := c.HTTPClient.Do(req)
|
2018-11-04 01:36:04 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return 0, errutils.NewHTTPDoError(req, err)
|
2018-11-04 01:36:04 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
defer func() { _ = resp.Body.Close() }()
|
2018-11-04 01:36:04 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
if resp.StatusCode/100 != 2 {
|
|
|
|
return resp.StatusCode, parseError(req, resp)
|
|
|
|
}
|
2018-11-04 01:36:04 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
if result == nil {
|
|
|
|
return resp.StatusCode, nil
|
2018-11-04 01:36:04 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
raw, err := io.ReadAll(resp.Body)
|
2018-11-04 01:36:04 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return resp.StatusCode, errutils.NewReadResponseError(req, resp.StatusCode, err)
|
2018-11-04 01:36:04 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
err = json.Unmarshal(raw, result)
|
|
|
|
if err != nil {
|
|
|
|
return resp.StatusCode, errutils.NewUnmarshalError(req, resp.StatusCode, raw, err)
|
2018-11-04 01:36:04 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
return resp.StatusCode, nil
|
2018-11-04 01:36:04 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
func newJSONRequest(ctx context.Context, method string, endpoint *url.URL, payload any) (*http.Request, error) {
|
|
|
|
buf := new(bytes.Buffer)
|
2018-11-04 01:36:04 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
if payload != nil {
|
|
|
|
err := json.NewEncoder(buf).Encode(payload)
|
2018-11-04 01:36:04 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return nil, fmt.Errorf("failed to create request JSON body: %w", err)
|
2018-11-04 01:36:04 +00:00
|
|
|
}
|
2023-05-05 07:49:38 +00:00
|
|
|
}
|
2018-11-04 01:36:04 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
req, err := http.NewRequestWithContext(ctx, method, endpoint.String(), buf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to create request: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("Accept", "application/json")
|
2018-11-04 01:36:04 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
if payload != nil {
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
2018-11-04 01:36:04 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
return req, nil
|
2018-11-04 01:36:04 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
func parseError(req *http.Request, resp *http.Response) error {
|
|
|
|
raw, _ := io.ReadAll(resp.Body)
|
2018-11-04 01:36:04 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
errAPI := &APIError{}
|
|
|
|
err := json.Unmarshal(raw, errAPI)
|
2018-11-08 09:31:42 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return errutils.NewUnexpectedStatusCodeError(req, resp.StatusCode, raw)
|
2018-11-08 09:31:42 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
return fmt.Errorf("request failed with status code %d: %w", resp.StatusCode, errAPI)
|
2018-11-04 01:36:04 +00:00
|
|
|
}
|