2020-05-08 10:26:44 +00:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2023-05-05 07:49:38 +00:00
|
|
|
"context"
|
2020-05-08 10:26:44 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2021-08-25 09:44:11 +00:00
|
|
|
"io"
|
2020-05-08 10:26:44 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2023-05-05 07:49:38 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-acme/lego/v4/providers/dns/internal/errutils"
|
|
|
|
"golang.org/x/oauth2"
|
2020-05-08 10:26:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const defaultBaseURL = "https://api.netlify.com/api/v1"
|
|
|
|
|
|
|
|
// Client Netlify API client.
|
|
|
|
type Client struct {
|
2023-05-05 07:49:38 +00:00
|
|
|
baseURL *url.URL
|
|
|
|
httpClient *http.Client
|
2020-05-08 10:26:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient creates a new Client.
|
2023-05-05 07:49:38 +00:00
|
|
|
func NewClient(hc *http.Client) *Client {
|
|
|
|
baseURL, _ := url.Parse(defaultBaseURL)
|
|
|
|
|
|
|
|
if hc == nil {
|
|
|
|
hc = &http.Client{Timeout: 5 * time.Second}
|
2020-05-08 10:26:44 +00:00
|
|
|
}
|
2023-05-05 07:49:38 +00:00
|
|
|
|
|
|
|
return &Client{baseURL: baseURL, httpClient: hc}
|
2020-05-08 10:26:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetRecords gets a DNS records.
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c *Client) GetRecords(ctx context.Context, zoneID string) ([]DNSRecord, error) {
|
|
|
|
endpoint := c.baseURL.JoinPath("dns_zones", zoneID, "dns_records")
|
2020-05-08 10:26:44 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
req, err := newJSONRequest(ctx, http.MethodGet, endpoint, nil)
|
2020-05-08 10:26:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create request: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
resp, err := c.httpClient.Do(req)
|
2020-05-08 10:26:44 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return nil, errutils.NewHTTPDoError(req, err)
|
2020-05-08 10:26:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func() { _ = resp.Body.Close() }()
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return nil, errutils.NewUnexpectedResponseStatusCodeError(req, resp)
|
2020-05-08 10:26:44 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
raw, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errutils.NewReadResponseError(req, resp.StatusCode, err)
|
2020-05-08 10:26:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var records []DNSRecord
|
2023-05-05 07:49:38 +00:00
|
|
|
err = json.Unmarshal(raw, &records)
|
2020-05-08 10:26:44 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return nil, errutils.NewUnmarshalError(req, resp.StatusCode, raw, err)
|
2020-05-08 10:26:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return records, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateRecord creates a DNS records.
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c *Client) CreateRecord(ctx context.Context, zoneID string, record DNSRecord) (*DNSRecord, error) {
|
|
|
|
endpoint := c.baseURL.JoinPath("dns_zones", zoneID, "dns_records")
|
2020-05-08 10:26:44 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
req, err := newJSONRequest(ctx, http.MethodPost, endpoint, record)
|
2020-05-08 10:26:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create request: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
resp, err := c.httpClient.Do(req)
|
2020-05-08 10:26:44 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return nil, errutils.NewHTTPDoError(req, err)
|
2020-05-08 10:26:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func() { _ = resp.Body.Close() }()
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
if resp.StatusCode != http.StatusCreated {
|
|
|
|
return nil, errutils.NewUnexpectedResponseStatusCodeError(req, resp)
|
2020-05-08 10:26:44 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
raw, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errutils.NewReadResponseError(req, resp.StatusCode, err)
|
2020-05-08 10:26:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var recordResp DNSRecord
|
2023-05-05 07:49:38 +00:00
|
|
|
err = json.Unmarshal(raw, &recordResp)
|
2020-05-08 10:26:44 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return nil, errutils.NewUnmarshalError(req, resp.StatusCode, raw, err)
|
2020-05-08 10:26:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &recordResp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveRecord removes a DNS records.
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c *Client) RemoveRecord(ctx context.Context, zoneID, recordID string) error {
|
|
|
|
endpoint := c.baseURL.JoinPath("dns_zones", zoneID, "dns_records", recordID)
|
|
|
|
|
|
|
|
req, err := newJSONRequest(ctx, http.MethodDelete, endpoint, nil)
|
2020-05-08 10:26:44 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return err
|
2020-05-08 10:26:44 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
resp, err := c.httpClient.Do(req)
|
2020-05-08 10:26:44 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return errutils.NewHTTPDoError(req, err)
|
2020-05-08 10:26:44 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
defer func() { _ = resp.Body.Close() }()
|
2020-05-08 10:26:44 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
if resp.StatusCode != http.StatusNoContent {
|
|
|
|
return errutils.NewUnexpectedResponseStatusCodeError(req, resp)
|
2020-05-08 10:26:44 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-05-08 10:26:44 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
func newJSONRequest(ctx context.Context, method string, endpoint *url.URL, payload interface{}) (*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-08 10:26:44 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return nil, fmt.Errorf("unable to create request: %w", err)
|
2020-05-08 10:26:44 +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; charset=utf-8")
|
2020-05-08 10:26:44 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
return req, nil
|
2020-05-08 10:26:44 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
func OAuthStaticAccessToken(client *http.Client, accessToken string) *http.Client {
|
|
|
|
if client == nil {
|
|
|
|
client = &http.Client{Timeout: 5 * time.Second}
|
|
|
|
}
|
|
|
|
|
|
|
|
client.Transport = &oauth2.Transport{
|
|
|
|
Source: oauth2.StaticTokenSource(&oauth2.Token{AccessToken: accessToken}),
|
|
|
|
Base: client.Transport,
|
2020-05-08 10:26:44 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
return client
|
2020-05-08 10:26:44 +00:00
|
|
|
}
|