2021-01-14 19:42:12 +00:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
|
|
|
|
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 {
|
|
|
|
HTTPClient *http.Client
|
|
|
|
BaseURL *url.URL
|
|
|
|
|
|
|
|
apiKey string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient creates a new Client.
|
|
|
|
func NewClient(apiKey string) (*Client, error) {
|
|
|
|
baseURL, err := url.Parse(defaultBaseURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Client{
|
|
|
|
HTTPClient: http.DefaultClient,
|
|
|
|
BaseURL: baseURL,
|
|
|
|
apiKey: apiKey,
|
|
|
|
}, 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")
|
|
|
|
|
|
|
|
req, err := c.makeRequest(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)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := c.HTTPClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to call API: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() { _ = resp.Body.Close() }()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return nil, readError(resp.Body, resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
var zones []Zone
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&zones)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to parse response: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2021-01-14 19:42:12 +00:00
|
|
|
body, err := json.Marshal(records)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to marshal request body: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-02-09 16:19:58 +00:00
|
|
|
req, err := c.makeRequest(ctx, http.MethodPatch, endpoint, bytes.NewReader(body))
|
2021-01-14 19:42:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create request: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := c.HTTPClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to call API: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() { _ = resp.Body.Close() }()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return readError(resp.Body, resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
req, err := c.makeRequest(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()
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := c.HTTPClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to call API: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() { _ = resp.Body.Close() }()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return nil, readError(resp.Body, resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
var zone CustomerZone
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&zone)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to parse response: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
req, err := c.makeRequest(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)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := c.HTTPClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to call API: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() { _ = resp.Body.Close() }()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return readError(resp.Body, resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-09 16:19:58 +00:00
|
|
|
func (c *Client) makeRequest(ctx context.Context, method string, endpoint *url.URL, body io.Reader) (*http.Request, error) {
|
2021-01-14 19:42:12 +00:00
|
|
|
req, err := http.NewRequestWithContext(ctx, method, endpoint.String(), body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create request: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("Accept", "application/json")
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
req.Header.Set("X-API-Key", c.apiKey)
|
|
|
|
|
|
|
|
return req, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func readError(body io.Reader, statusCode int) error {
|
2021-08-25 09:44:11 +00:00
|
|
|
bodyBytes, _ := io.ReadAll(body)
|
2021-01-14 19:42:12 +00:00
|
|
|
|
|
|
|
cErr := &ClientError{StatusCode: statusCode}
|
|
|
|
|
|
|
|
err := json.Unmarshal(bodyBytes, &cErr.errors)
|
|
|
|
if err != nil {
|
|
|
|
cErr.message = string(bodyBytes)
|
|
|
|
return cErr
|
|
|
|
}
|
|
|
|
|
|
|
|
return cErr
|
|
|
|
}
|