2021-06-03 01:50:45 +00:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2023-05-05 07:49:38 +00:00
|
|
|
"context"
|
2021-06-03 01:50:45 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2023-05-05 07:49:38 +00:00
|
|
|
"io"
|
2021-06-03 01:50:45 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2023-10-31 13:08:50 +00:00
|
|
|
"strconv"
|
2023-02-09 16:19:58 +00:00
|
|
|
"strings"
|
2021-06-03 01:50:45 +00:00
|
|
|
"time"
|
2023-05-05 07:49:38 +00:00
|
|
|
|
|
|
|
"github.com/go-acme/lego/v4/providers/dns/internal/errutils"
|
2021-06-03 01:50:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const defaultBaseURL = "https://api.simply.com/1/"
|
|
|
|
|
|
|
|
// Client is a Simply.com API client.
|
|
|
|
type Client struct {
|
|
|
|
accountName string
|
|
|
|
apiKey string
|
2023-05-05 07:49:38 +00:00
|
|
|
|
|
|
|
baseURL *url.URL
|
|
|
|
HTTPClient *http.Client
|
2021-06-03 01:50:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient creates a new Client.
|
|
|
|
func NewClient(accountName string, apiKey string) (*Client, error) {
|
|
|
|
if accountName == "" {
|
|
|
|
return nil, errors.New("credentials missing: accountName")
|
|
|
|
}
|
|
|
|
|
|
|
|
if apiKey == "" {
|
|
|
|
return nil, errors.New("credentials missing: apiKey")
|
|
|
|
}
|
|
|
|
|
|
|
|
baseURL, err := url.Parse(defaultBaseURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Client{
|
|
|
|
accountName: accountName,
|
|
|
|
apiKey: apiKey,
|
2023-05-05 07:49:38 +00:00
|
|
|
baseURL: baseURL,
|
|
|
|
HTTPClient: &http.Client{Timeout: 5 * time.Second},
|
2021-06-03 01:50:45 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRecords lists all the records in the zone.
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c *Client) GetRecords(ctx context.Context, zoneName string) ([]Record, error) {
|
|
|
|
endpoint := c.createEndpoint(zoneName, "/")
|
|
|
|
|
|
|
|
req, err := newJSONRequest(ctx, http.MethodGet, endpoint, nil)
|
2021-06-03 01:50:45 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return nil, fmt.Errorf("failed to create request: %w", err)
|
2021-06-03 01:50:45 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
result := &apiResponse[[]Record, json.RawMessage]{}
|
|
|
|
err = c.do(req, result)
|
2021-06-03 01:50:45 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return nil, err
|
2021-06-03 01:50:45 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
return result.Records, nil
|
2021-06-03 01:50:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddRecord adds a record.
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c *Client) AddRecord(ctx context.Context, zoneName string, record Record) (int64, error) {
|
|
|
|
endpoint := c.createEndpoint(zoneName, "/")
|
2021-06-03 01:50:45 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
req, err := newJSONRequest(ctx, http.MethodPost, endpoint, record)
|
2021-06-03 01:50:45 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return 0, fmt.Errorf("failed to create request: %w", err)
|
2021-06-03 01:50:45 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
result := &apiResponse[json.RawMessage, recordHeader]{}
|
|
|
|
err = c.do(req, result)
|
2021-06-03 01:50:45 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return 0, err
|
2021-06-03 01:50:45 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
return result.Record.ID, nil
|
2021-06-03 01:50:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// EditRecord updates a record.
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c *Client) EditRecord(ctx context.Context, zoneName string, id int64, record Record) error {
|
2023-10-31 13:08:50 +00:00
|
|
|
endpoint := c.createEndpoint(zoneName, strconv.FormatInt(id, 10))
|
2023-05-05 07:49:38 +00:00
|
|
|
|
|
|
|
req, err := newJSONRequest(ctx, http.MethodPut, endpoint, record)
|
2021-06-03 01:50:45 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return fmt.Errorf("failed to create request: %w", err)
|
2021-06-03 01:50:45 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
return c.do(req, &apiResponse[json.RawMessage, json.RawMessage]{})
|
2021-06-03 01:50:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteRecord deletes a record.
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c *Client) DeleteRecord(ctx context.Context, zoneName string, id int64) error {
|
2023-10-31 13:08:50 +00:00
|
|
|
endpoint := c.createEndpoint(zoneName, strconv.FormatInt(id, 10))
|
2021-06-03 01:50:45 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
req, err := newJSONRequest(ctx, http.MethodDelete, endpoint, nil)
|
2021-06-03 01:50:45 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return fmt.Errorf("failed to create request: %w", err)
|
2021-06-03 01:50:45 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
return c.do(req, &apiResponse[json.RawMessage, json.RawMessage]{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) createEndpoint(zoneName string, uri string) *url.URL {
|
|
|
|
return c.baseURL.JoinPath(c.accountName, c.apiKey, "my", "products", zoneName, "dns", "records", strings.TrimSuffix(uri, "/"))
|
|
|
|
}
|
2021-06-03 01:50:45 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c *Client) do(req *http.Request, result Response) error {
|
2021-06-03 01:50:45 +00:00
|
|
|
resp, err := c.HTTPClient.Do(req)
|
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return errutils.NewHTTPDoError(req, err)
|
2021-06-03 01:50:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func() { _ = resp.Body.Close() }()
|
|
|
|
|
|
|
|
if resp.StatusCode >= http.StatusInternalServerError {
|
2023-05-05 07:49:38 +00:00
|
|
|
return errutils.NewUnexpectedResponseStatusCodeError(req, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
raw, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return errutils.NewReadResponseError(req, resp.StatusCode, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(raw, result)
|
|
|
|
if err != nil {
|
|
|
|
return errutils.NewUnmarshalError(req, resp.StatusCode, raw, err)
|
2021-06-03 01:50:45 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
if result.GetStatus() != http.StatusOK {
|
|
|
|
return fmt.Errorf("unexpected error: %s", result.GetMessage())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func newJSONRequest(ctx context.Context, method string, endpoint *url.URL, payload any) (*http.Request, error) {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
|
|
|
|
if payload != nil {
|
|
|
|
err := json.NewEncoder(buf).Encode(payload)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create request JSON body: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequestWithContext(ctx, method, endpoint.String(), buf)
|
2021-06-03 01:50:45 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return nil, fmt.Errorf("unable to create request: %w", err)
|
2021-06-03 01:50:45 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
req.Header.Set("Accept", "application/json")
|
|
|
|
|
|
|
|
if payload != nil {
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
2021-06-03 01:50:45 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
return req, nil
|
2021-06-03 01:50:45 +00:00
|
|
|
}
|