2021-01-14 19:42:12 +00:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2023-05-05 07:49:38 +00:00
|
|
|
"time"
|
2021-01-14 19:42:12 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
"github.com/go-acme/lego/v4/providers/dns/internal/errutils"
|
2021-01-14 19:42:12 +00:00
|
|
|
querystring "github.com/google/go-querystring/query"
|
|
|
|
)
|
|
|
|
|
|
|
|
// defaultBaseURL represents the API endpoint to call.
|
|
|
|
const defaultBaseURL = "https://api.hosting.ionos.com/dns"
|
|
|
|
|
|
|
|
// Client Ionos API client.
|
|
|
|
type Client struct {
|
|
|
|
apiKey string
|
2023-05-05 07:49:38 +00:00
|
|
|
|
|
|
|
BaseURL *url.URL
|
|
|
|
HTTPClient *http.Client
|
2021-01-14 19:42:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient creates a new Client.
|
|
|
|
func NewClient(apiKey string) (*Client, error) {
|
|
|
|
baseURL, err := url.Parse(defaultBaseURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Client{
|
|
|
|
apiKey: apiKey,
|
2023-05-05 07:49:38 +00:00
|
|
|
BaseURL: baseURL,
|
|
|
|
HTTPClient: &http.Client{Timeout: 5 * time.Second},
|
2021-01-14 19:42:12 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListZones gets all zones.
|
|
|
|
func (c *Client) ListZones(ctx context.Context) ([]Zone, error) {
|
2023-02-09 16:19:58 +00:00
|
|
|
endpoint := c.BaseURL.JoinPath("v1", "zones")
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
req, err := makeJSONRequest(ctx, http.MethodGet, endpoint, nil)
|
2021-01-14 19:42:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create request: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var zones []Zone
|
2023-05-05 07:49:38 +00:00
|
|
|
err = c.do(req, &zones)
|
2021-01-14 19:42:12 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return nil, fmt.Errorf("failed to call API: %w", err)
|
2021-01-14 19:42:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return zones, nil
|
|
|
|
}
|
|
|
|
|
2023-02-09 16:19:58 +00:00
|
|
|
// ReplaceRecords replaces some records of a zones.
|
2021-01-14 19:42:12 +00:00
|
|
|
func (c *Client) ReplaceRecords(ctx context.Context, zoneID string, records []Record) error {
|
2023-02-09 16:19:58 +00:00
|
|
|
endpoint := c.BaseURL.JoinPath("v1", "zones", zoneID)
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
req, err := makeJSONRequest(ctx, http.MethodPatch, endpoint, records)
|
2021-01-14 19:42:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create request: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
err = c.do(req, nil)
|
2021-01-14 19:42:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to call API: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRecords gets the records of a zones.
|
|
|
|
func (c *Client) GetRecords(ctx context.Context, zoneID string, filter *RecordsFilter) ([]Record, error) {
|
2023-02-09 16:19:58 +00:00
|
|
|
endpoint := c.BaseURL.JoinPath("v1", "zones", zoneID)
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
req, err := makeJSONRequest(ctx, http.MethodGet, endpoint, nil)
|
2021-01-14 19:42:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create request: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if filter != nil {
|
|
|
|
v, errQ := querystring.Values(filter)
|
|
|
|
if errQ != nil {
|
|
|
|
return nil, errQ
|
|
|
|
}
|
|
|
|
|
|
|
|
req.URL.RawQuery = v.Encode()
|
|
|
|
}
|
|
|
|
|
|
|
|
var zone CustomerZone
|
2023-05-05 07:49:38 +00:00
|
|
|
err = c.do(req, &zone)
|
2021-01-14 19:42:12 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return nil, fmt.Errorf("failed to call API: %w", err)
|
2021-01-14 19:42:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return zone.Records, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveRecord removes a record.
|
|
|
|
func (c *Client) RemoveRecord(ctx context.Context, zoneID, recordID string) error {
|
2023-02-09 16:19:58 +00:00
|
|
|
endpoint := c.BaseURL.JoinPath("v1", "zones", zoneID, "records", recordID)
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
req, err := makeJSONRequest(ctx, http.MethodDelete, endpoint, nil)
|
2021-01-14 19:42:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create request: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
err = c.do(req, nil)
|
2021-01-14 19:42:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to call API: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) do(req *http.Request, result any) error {
|
|
|
|
req.Header.Set("X-API-Key", c.apiKey)
|
|
|
|
|
|
|
|
resp, err := c.HTTPClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return errutils.NewHTTPDoError(req, err)
|
|
|
|
}
|
|
|
|
|
2021-01-14 19:42:12 +00:00
|
|
|
defer func() { _ = resp.Body.Close() }()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
2023-05-05 07:49:38 +00:00
|
|
|
return parseError(req, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
if result == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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-01-14 19:42:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
func makeJSONRequest(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-01-14 19:42:12 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return nil, fmt.Errorf("unable to create request: %w", err)
|
2021-01-14 19:42:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("Accept", "application/json")
|
2023-05-05 07:49:38 +00:00
|
|
|
|
|
|
|
if payload != nil {
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
}
|
2021-01-14 19:42:12 +00:00
|
|
|
|
|
|
|
return req, nil
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
func parseError(req *http.Request, resp *http.Response) error {
|
|
|
|
raw, _ := io.ReadAll(resp.Body)
|
2021-01-14 19:42:12 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
errClient := &ClientError{StatusCode: resp.StatusCode}
|
|
|
|
err := json.Unmarshal(raw, &errClient.errors)
|
2021-01-14 19:42:12 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return errutils.NewUnexpectedStatusCodeError(req, resp.StatusCode, raw)
|
2021-01-14 19:42:12 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
return errClient
|
2021-01-14 19:42:12 +00:00
|
|
|
}
|