lego/providers/dns/gcore/internal/client.go

190 lines
4.8 KiB
Go
Raw Normal View History

package internal
import (
2023-05-05 07:49:38 +00:00
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
2021-08-25 09:44:11 +00:00
"io"
"net/http"
"net/url"
"time"
2023-05-05 07:49:38 +00:00
"github.com/go-acme/lego/v4/providers/dns/internal/errutils"
)
2023-07-25 12:42:54 +00:00
const defaultBaseURL = "https://api.gcore.com/dns"
2023-05-05 07:49:38 +00:00
const (
2023-05-05 07:49:38 +00:00
authorizationHeader = "Authorization"
tokenTypeHeader = "APIKey"
)
2023-05-05 07:49:38 +00:00
const txtRecordType = "TXT"
// Client for DNS API.
type Client struct {
2023-05-05 07:49:38 +00:00
token string
baseURL *url.URL
2023-05-05 07:49:38 +00:00
HTTPClient *http.Client
}
// NewClient constructor of Client.
func NewClient(token string) *Client {
baseURL, _ := url.Parse(defaultBaseURL)
return &Client{
token: token,
baseURL: baseURL,
HTTPClient: &http.Client{Timeout: 10 * time.Second},
}
}
// GetZone gets zone information.
2023-07-25 12:42:54 +00:00
// https://api.gcore.com/docs/dns#tag/zones/operation/Zone
func (c *Client) GetZone(ctx context.Context, name string) (Zone, error) {
2023-02-09 16:19:58 +00:00
endpoint := c.baseURL.JoinPath("v2", "zones", name)
2023-02-09 16:19:58 +00:00
zone := Zone{}
2023-05-05 07:49:38 +00:00
err := c.doRequest(ctx, http.MethodGet, endpoint, nil, &zone)
if err != nil {
return Zone{}, fmt.Errorf("get zone %s: %w", name, err)
}
return zone, nil
}
// GetRRSet gets RRSet item.
2023-07-25 12:42:54 +00:00
// https://api.gcore.com/docs/dns#tag/rrsets/operation/RRSet
func (c *Client) GetRRSet(ctx context.Context, zone, name string) (RRSet, error) {
2023-02-09 16:19:58 +00:00
endpoint := c.baseURL.JoinPath("v2", "zones", zone, name, txtRecordType)
2023-02-09 16:19:58 +00:00
var result RRSet
2023-05-05 07:49:38 +00:00
err := c.doRequest(ctx, http.MethodGet, endpoint, nil, &result)
if err != nil {
return RRSet{}, fmt.Errorf("get txt records %s -> %s: %w", zone, name, err)
}
return result, nil
}
// DeleteRRSet removes RRSet record.
2023-07-25 12:42:54 +00:00
// https://api.gcore.com/docs/dns#tag/rrsets/operation/DeleteRRSet
func (c *Client) DeleteRRSet(ctx context.Context, zone, name string) error {
2023-02-09 16:19:58 +00:00
endpoint := c.baseURL.JoinPath("v2", "zones", zone, name, txtRecordType)
2023-05-05 07:49:38 +00:00
err := c.doRequest(ctx, http.MethodDelete, endpoint, nil, nil)
if err != nil {
// Support DELETE idempotence https://developer.mozilla.org/en-US/docs/Glossary/Idempotent
statusErr := new(APIError)
if errors.As(err, statusErr) && statusErr.StatusCode == http.StatusNotFound {
return nil
}
return fmt.Errorf("delete record request: %w", err)
}
return nil
}
// AddRRSet adds TXT record (create or update).
func (c *Client) AddRRSet(ctx context.Context, zone, recordName, value string, ttl int) error {
record := RRSet{TTL: ttl, Records: []Records{{Content: []string{value}}}}
txt, err := c.GetRRSet(ctx, zone, recordName)
if err == nil && len(txt.Records) > 0 {
record.Records = append(record.Records, txt.Records...)
return c.updateRRSet(ctx, zone, recordName, record)
}
return c.createRRSet(ctx, zone, recordName, record)
}
2023-07-25 12:42:54 +00:00
// https://api.gcore.com/docs/dns#tag/rrsets/operation/CreateRRSet
func (c *Client) createRRSet(ctx context.Context, zone, name string, record RRSet) error {
2023-02-09 16:19:58 +00:00
endpoint := c.baseURL.JoinPath("v2", "zones", zone, name, txtRecordType)
2023-05-05 07:49:38 +00:00
return c.doRequest(ctx, http.MethodPost, endpoint, record, nil)
}
2023-07-25 12:42:54 +00:00
// https://api.gcore.com/docs/dns#tag/rrsets/operation/UpdateRRSet
func (c *Client) updateRRSet(ctx context.Context, zone, name string, record RRSet) error {
2023-02-09 16:19:58 +00:00
endpoint := c.baseURL.JoinPath("v2", "zones", zone, name, txtRecordType)
2023-05-05 07:49:38 +00:00
return c.doRequest(ctx, http.MethodPut, endpoint, record, nil)
}
2023-05-05 07:49:38 +00:00
func (c *Client) doRequest(ctx context.Context, method string, endpoint *url.URL, bodyParams any, result any) error {
req, err := newJSONRequest(ctx, method, endpoint, bodyParams)
if err != nil {
return fmt.Errorf("new request: %w", err)
}
2023-05-05 07:49:38 +00:00
req.Header.Set(authorizationHeader, fmt.Sprintf("%s %s", tokenTypeHeader, c.token))
resp, err := c.HTTPClient.Do(req)
if err != nil {
2023-05-05 07:49:38 +00:00
return errutils.NewHTTPDoError(req, err)
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode/100 != 2 {
2023-05-05 07:49:38 +00:00
return parseError(resp)
}
2023-05-05 07:49:38 +00:00
if result == nil {
return nil
}
raw, err := io.ReadAll(resp.Body)
if err != nil {
return errutils.NewReadResponseError(req, resp.StatusCode, err)
}
2023-05-05 07:49:38 +00:00
err = json.Unmarshal(raw, result)
if err != nil {
return errutils.NewUnmarshalError(req, resp.StatusCode, raw, err)
}
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 {
2023-05-05 07:49:38 +00:00
return nil, fmt.Errorf("failed to create request JSON body: %w", err)
}
2023-05-05 07:49:38 +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)
}
2023-05-05 07:49:38 +00:00
req.Header.Set("Accept", "application/json")
if payload != nil {
req.Header.Set("Content-Type", "application/json")
}
return req, nil
}
func parseError(resp *http.Response) error {
raw, _ := io.ReadAll(resp.Body)
errAPI := APIError{StatusCode: resp.StatusCode}
err := json.Unmarshal(raw, &errAPI)
if err != nil {
errAPI.Message = string(raw)
}
2023-05-05 07:49:38 +00:00
return errAPI
}