2022-04-29 20:50:39 +00:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2023-05-05 07:49:38 +00:00
|
|
|
"context"
|
2022-04-29 20:50:39 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-acme/lego/v4/challenge/dns01"
|
2023-05-05 07:49:38 +00:00
|
|
|
"github.com/go-acme/lego/v4/providers/dns/internal/errutils"
|
|
|
|
"golang.org/x/oauth2"
|
2022-04-29 20:50:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const defaultBaseURL = "https://api.vercel.com"
|
|
|
|
|
|
|
|
// Client Vercel client.
|
|
|
|
type Client struct {
|
2023-05-05 07:49:38 +00:00
|
|
|
teamID string
|
|
|
|
|
2022-04-29 20:50:39 +00:00
|
|
|
baseURL *url.URL
|
2023-05-05 07:49:38 +00:00
|
|
|
httpClient *http.Client
|
2022-04-29 20:50:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient creates a Client.
|
2023-05-05 07:49:38 +00:00
|
|
|
func NewClient(hc *http.Client, teamID string) *Client {
|
2022-04-29 20:50:39 +00:00
|
|
|
baseURL, _ := url.Parse(defaultBaseURL)
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
if hc == nil {
|
|
|
|
hc = &http.Client{Timeout: 10 * time.Second}
|
|
|
|
}
|
|
|
|
|
2022-04-29 20:50:39 +00:00
|
|
|
return &Client{
|
|
|
|
teamID: teamID,
|
|
|
|
baseURL: baseURL,
|
2023-05-05 07:49:38 +00:00
|
|
|
httpClient: hc,
|
2022-04-29 20:50:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateRecord creates a DNS record.
|
|
|
|
// https://vercel.com/docs/rest-api#endpoints/dns/create-a-dns-record
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c *Client) CreateRecord(ctx context.Context, zone string, record Record) (*CreateRecordResponse, error) {
|
2023-02-09 16:19:58 +00:00
|
|
|
endpoint := c.baseURL.JoinPath("v2", "domains", dns01.UnFqdn(zone), "records")
|
2022-04-29 20:50:39 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
req, err := newJSONRequest(ctx, http.MethodPost, endpoint, record)
|
2022-04-29 20:50:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
respData := &CreateRecordResponse{}
|
|
|
|
err = c.do(req, respData)
|
2022-04-29 20:50:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
return respData, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteRecord deletes a DNS record.
|
|
|
|
// https://vercel.com/docs/rest-api#endpoints/dns/delete-a-dns-record
|
|
|
|
func (c *Client) DeleteRecord(ctx context.Context, zone string, recordID string) error {
|
|
|
|
endpoint := c.baseURL.JoinPath("v2", "domains", dns01.UnFqdn(zone), "records", recordID)
|
|
|
|
|
|
|
|
req, err := newJSONRequest(ctx, http.MethodDelete, endpoint, nil)
|
2022-04-29 20:50:39 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return err
|
2022-04-29 20:50:39 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
return c.do(req, nil)
|
|
|
|
}
|
2022-04-29 20:50:39 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c *Client) do(req *http.Request, result any) error {
|
|
|
|
if c.teamID != "" {
|
|
|
|
query := req.URL.Query()
|
|
|
|
query.Add("teamId", c.teamID)
|
|
|
|
req.URL.RawQuery = query.Encode()
|
2022-04-29 20:50:39 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
resp, err := c.httpClient.Do(req)
|
2022-04-29 20:50:39 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return errutils.NewHTTPDoError(req, err)
|
2022-04-29 20:50:39 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
defer func() { _ = resp.Body.Close() }()
|
2022-04-29 20:50:39 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
if resp.StatusCode/100 != 2 {
|
|
|
|
return parseError(req, resp)
|
|
|
|
}
|
2022-04-29 20:50:39 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
if result == nil {
|
|
|
|
return nil
|
2022-04-29 20:50:39 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
raw, err := io.ReadAll(resp.Body)
|
2022-04-29 20:50:39 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return errutils.NewReadResponseError(req, resp.StatusCode, err)
|
2022-04-29 20:50:39 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
err = json.Unmarshal(raw, result)
|
|
|
|
if err != nil {
|
|
|
|
return errutils.NewUnmarshalError(req, resp.StatusCode, raw, err)
|
2022-04-29 20:50:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00: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)
|
|
|
|
}
|
2022-04-29 20:50:39 +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)
|
2022-04-29 20:50:39 +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")
|
|
|
|
}
|
2022-04-29 20:50:39 +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)
|
2022-04-29 20:50:39 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
var response APIErrorResponse
|
|
|
|
err := json.Unmarshal(raw, &response)
|
2022-04-29 20:50:39 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return errutils.NewUnexpectedStatusCodeError(req, resp.StatusCode, raw)
|
2022-04-29 20:50:39 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
return fmt.Errorf("[status code: %d] %w", resp.StatusCode, response.Error)
|
2022-04-29 20:50:39 +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,
|
|
|
|
}
|
|
|
|
|
|
|
|
return client
|
2022-04-29 20:50:39 +00:00
|
|
|
}
|