1
0
mirror of https://github.com/go-acme/lego.git synced 2024-12-23 09:15:11 +02:00
lego/providers/dns/luadns/internal/client.go

163 lines
3.7 KiB
Go
Raw Normal View History

2020-05-10 06:03:54 +02:00
package internal
import (
"bytes"
2023-05-05 09:49:38 +02:00
"context"
2020-05-10 06:03:54 +02:00
"encoding/json"
"fmt"
"io"
"net/http"
2023-05-05 09:49:38 +02:00
"net/url"
"strconv"
"time"
"github.com/go-acme/lego/v4/providers/dns/internal/errutils"
2020-05-10 06:03:54 +02:00
)
// defaultBaseURL represents the API endpoint to call.
const defaultBaseURL = "https://api.luadns.com"
// Client Lua DNS API client.
type Client struct {
apiUsername string
apiToken string
2023-05-05 09:49:38 +02:00
baseURL *url.URL
HTTPClient *http.Client
2020-05-10 06:03:54 +02:00
}
// NewClient creates a new Client.
func NewClient(apiUsername, apiToken string) *Client {
2023-05-05 09:49:38 +02:00
baseURL, _ := url.Parse(defaultBaseURL)
2020-05-10 06:03:54 +02:00
return &Client{
apiUsername: apiUsername,
apiToken: apiToken,
2023-05-05 09:49:38 +02:00
baseURL: baseURL,
HTTPClient: &http.Client{Timeout: 5 * time.Second},
2020-05-10 06:03:54 +02:00
}
}
// ListZones gets all the hosted zones.
// https://luadns.com/api.html#list-zones
2023-05-05 09:49:38 +02:00
func (c *Client) ListZones(ctx context.Context) ([]DNSZone, error) {
endpoint := c.baseURL.JoinPath("v1", "zones")
req, err := newJSONRequest(ctx, http.MethodGet, endpoint, nil)
2020-05-10 06:03:54 +02:00
if err != nil {
return nil, err
}
var zones []DNSZone
2023-05-05 09:49:38 +02:00
err = c.do(req, &zones)
2020-05-10 06:03:54 +02:00
if err != nil {
2023-05-05 09:49:38 +02:00
return nil, fmt.Errorf("could not list zones: %w", err)
2020-05-10 06:03:54 +02:00
}
return zones, nil
}
// CreateRecord creates a new record in a zone.
// https://luadns.com/api.html#create-a-record
2023-05-05 09:49:38 +02:00
func (c *Client) CreateRecord(ctx context.Context, zone DNSZone, newRecord DNSRecord) (*DNSRecord, error) {
endpoint := c.baseURL.JoinPath("v1", "zones", strconv.Itoa(zone.ID), "records")
2020-05-10 06:03:54 +02:00
2023-05-05 09:49:38 +02:00
req, err := newJSONRequest(ctx, http.MethodPost, endpoint, newRecord)
2020-05-10 06:03:54 +02:00
if err != nil {
return nil, err
}
var record *DNSRecord
2023-05-05 09:49:38 +02:00
err = c.do(req, &record)
2020-05-10 06:03:54 +02:00
if err != nil {
2023-05-05 09:49:38 +02:00
return nil, fmt.Errorf("could not create record %#v: %w", record, err)
2020-05-10 06:03:54 +02:00
}
return record, nil
}
// DeleteRecord deletes a record.
// https://luadns.com/api.html#delete-a-record
2023-05-05 09:49:38 +02:00
func (c *Client) DeleteRecord(ctx context.Context, record *DNSRecord) error {
endpoint := c.baseURL.JoinPath("v1", "zones", strconv.Itoa(record.ZoneID), "records", strconv.Itoa(record.ID))
req, err := newJSONRequest(ctx, http.MethodDelete, endpoint, record)
2020-05-10 06:03:54 +02:00
if err != nil {
2023-05-05 09:49:38 +02:00
return err
}
err = c.do(req, nil)
if err != nil {
return fmt.Errorf("could not delete record %#v: %w", record, err)
2020-05-10 06:03:54 +02:00
}
2023-05-05 09:49:38 +02:00
return nil
}
func (c *Client) do(req *http.Request, result any) error {
req.SetBasicAuth(c.apiUsername, c.apiToken)
2020-05-10 06:03:54 +02:00
2023-05-05 09:49:38 +02:00
resp, err := c.HTTPClient.Do(req)
2020-05-10 06:03:54 +02:00
if err != nil {
2023-05-05 09:49:38 +02:00
return errutils.NewHTTPDoError(req, err)
2020-05-10 06:03:54 +02:00
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
2023-05-05 09:49:38 +02:00
return parseError(req, resp)
}
2020-05-10 06:03:54 +02:00
2023-05-05 09:49:38 +02:00
if result == nil {
return nil
}
2020-05-10 06:03:54 +02:00
2023-05-05 09:49:38 +02:00
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)
2020-05-10 06:03:54 +02:00
}
return nil
}
2023-05-05 09:49:38 +02:00
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)
2020-05-10 06:03:54 +02:00
if err != nil {
2023-05-05 09:49:38 +02:00
return nil, fmt.Errorf("unable to create request: %w", err)
2020-05-10 06:03:54 +02:00
}
req.Header.Set("Accept", "application/json")
2023-05-05 09:49:38 +02:00
if payload != nil {
req.Header.Set("Content-Type", "application/json")
}
return req, nil
}
func parseError(req *http.Request, resp *http.Response) error {
raw, _ := io.ReadAll(resp.Body)
var errResp errorResponse
err := json.Unmarshal(raw, &errResp)
if err != nil {
return errutils.NewUnexpectedStatusCodeError(req, resp.StatusCode, raw)
}
return fmt.Errorf("status=%d: %w", resp.StatusCode, errResp)
2020-05-10 06:03:54 +02:00
}