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

210 lines
5.1 KiB
Go
Raw Normal View History

2022-11-26 02:06:08 +00:00
package internal
import (
"bytes"
2023-05-05 07:49:38 +00:00
"context"
2022-11-26 02:06:08 +00:00
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"time"
2023-05-05 07:49:38 +00:00
"github.com/go-acme/lego/v4/providers/dns/internal/errutils"
"golang.org/x/oauth2"
2022-11-26 02:06:08 +00:00
)
const defaultBaseURL = "https://dns-service.iran.liara.ir"
// Client a Liara DNS API client.
type Client struct {
baseURL *url.URL
2023-05-05 07:49:38 +00:00
httpClient *http.Client
2022-11-26 02:06:08 +00:00
}
// NewClient creates a new Client.
2023-05-05 07:49:38 +00:00
func NewClient(hc *http.Client) *Client {
2022-11-26 02:06:08 +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-11-26 02:06:08 +00:00
}
2023-05-05 07:49:38 +00:00
return &Client{httpClient: hc, baseURL: baseURL}
2022-11-26 02:06:08 +00:00
}
// GetRecords gets the records of a domain.
// https://dns-service.iran.liara.ir/swagger
2023-05-05 07:49:38 +00:00
func (c Client) GetRecords(ctx context.Context, domainName string) ([]Record, error) {
2023-02-09 16:19:58 +00:00
endpoint := c.baseURL.JoinPath("api", "v1", "zones", domainName, "dns-records")
2022-11-26 02:06:08 +00:00
2023-05-05 07:49:38 +00:00
req, err := newJSONRequest(ctx, http.MethodGet, endpoint, nil)
2022-11-26 02:06:08 +00:00
if err != nil {
return nil, fmt.Errorf("create request: %w", err)
}
2023-05-05 07:49:38 +00:00
resp, err := c.httpClient.Do(req)
2022-11-26 02:06:08 +00:00
if err != nil {
2023-05-05 07:49:38 +00:00
return nil, errutils.NewHTTPDoError(req, err)
2022-11-26 02:06:08 +00:00
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
2023-05-05 07:49:38 +00:00
return nil, parseError(req, resp)
2022-11-26 02:06:08 +00:00
}
2023-05-05 07:49:38 +00:00
raw, err := io.ReadAll(resp.Body)
2022-11-26 02:06:08 +00:00
if err != nil {
2023-05-05 07:49:38 +00:00
return nil, errutils.NewReadResponseError(req, resp.StatusCode, err)
}
var response Response[[]Record]
err = json.Unmarshal(raw, &response)
if err != nil {
return nil, errutils.NewUnmarshalError(req, resp.StatusCode, raw, err)
2022-11-26 02:06:08 +00:00
}
return response.Data, nil
}
// CreateRecord creates a record.
2023-05-05 07:49:38 +00:00
func (c Client) CreateRecord(ctx context.Context, domainName string, record Record) (*Record, error) {
2023-02-09 16:19:58 +00:00
endpoint := c.baseURL.JoinPath("api", "v1", "zones", domainName, "dns-records")
2022-11-26 02:06:08 +00:00
2023-05-05 07:49:38 +00:00
req, err := newJSONRequest(ctx, http.MethodPost, endpoint, record)
2022-11-26 02:06:08 +00:00
if err != nil {
return nil, fmt.Errorf("create request: %w", err)
}
2023-05-05 07:49:38 +00:00
resp, err := c.httpClient.Do(req)
2022-11-26 02:06:08 +00:00
if err != nil {
2023-05-05 07:49:38 +00:00
return nil, errutils.NewHTTPDoError(req, err)
2022-11-26 02:06:08 +00:00
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusCreated {
2023-05-05 07:49:38 +00:00
return nil, parseError(req, resp)
2022-11-26 02:06:08 +00:00
}
2023-05-05 07:49:38 +00:00
raw, err := io.ReadAll(resp.Body)
2022-11-26 02:06:08 +00:00
if err != nil {
2023-05-05 07:49:38 +00:00
return nil, errutils.NewReadResponseError(req, resp.StatusCode, err)
2022-11-26 02:06:08 +00:00
}
2023-05-05 07:49:38 +00:00
var response Response[*Record]
err = json.Unmarshal(raw, &response)
if err != nil {
return nil, errutils.NewUnmarshalError(req, resp.StatusCode, raw, err)
}
return response.Data, nil
2022-11-26 02:06:08 +00:00
}
// GetRecord gets a specific record.
2023-05-05 07:49:38 +00:00
func (c Client) GetRecord(ctx context.Context, domainName, recordID string) (*Record, error) {
2023-02-09 16:19:58 +00:00
endpoint := c.baseURL.JoinPath("api", "v1", "zones", domainName, "dns-records", recordID)
2022-11-26 02:06:08 +00:00
2023-05-05 07:49:38 +00:00
req, err := newJSONRequest(ctx, http.MethodGet, endpoint, nil)
2022-11-26 02:06:08 +00:00
if err != nil {
return nil, fmt.Errorf("create request: %w", err)
}
2023-05-05 07:49:38 +00:00
resp, err := c.httpClient.Do(req)
2022-11-26 02:06:08 +00:00
if err != nil {
2023-05-05 07:49:38 +00:00
return nil, errutils.NewHTTPDoError(req, err)
2022-11-26 02:06:08 +00:00
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
2023-05-05 07:49:38 +00:00
return nil, parseError(req, resp)
}
raw, err := io.ReadAll(resp.Body)
if err != nil {
return nil, errutils.NewReadResponseError(req, resp.StatusCode, err)
2022-11-26 02:06:08 +00:00
}
2023-05-05 07:49:38 +00:00
var response Response[*Record]
err = json.Unmarshal(raw, &response)
2022-11-26 02:06:08 +00:00
if err != nil {
2023-05-05 07:49:38 +00:00
return nil, errutils.NewUnmarshalError(req, resp.StatusCode, raw, err)
2022-11-26 02:06:08 +00:00
}
2023-05-05 07:49:38 +00:00
return response.Data, nil
2022-11-26 02:06:08 +00:00
}
// DeleteRecord deletes a record.
2023-05-05 07:49:38 +00:00
func (c Client) DeleteRecord(ctx context.Context, domainName, recordID string) error {
2023-02-09 16:19:58 +00:00
endpoint := c.baseURL.JoinPath("api", "v1", "zones", domainName, "dns-records", recordID)
2022-11-26 02:06:08 +00:00
2023-05-05 07:49:38 +00:00
req, err := newJSONRequest(ctx, http.MethodDelete, endpoint, nil)
2022-11-26 02:06:08 +00:00
if err != nil {
return fmt.Errorf("create request: %w", err)
}
2023-05-05 07:49:38 +00:00
resp, err := c.httpClient.Do(req)
2022-11-26 02:06:08 +00:00
if err != nil {
2023-05-05 07:49:38 +00:00
return errutils.NewHTTPDoError(req, err)
2022-11-26 02:06:08 +00:00
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusNoContent && resp.StatusCode != http.StatusNotFound {
2023-05-05 07:49:38 +00:00
return parseError(req, resp)
2022-11-26 02:06:08 +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)
}
}
req, err := http.NewRequestWithContext(ctx, method, endpoint.String(), buf)
2022-11-26 02:06:08 +00:00
if err != nil {
2023-05-05 07:49:38 +00:00
return nil, fmt.Errorf("unable to create request: %w", err)
2022-11-26 02:06:08 +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")
}
return req, nil
}
func parseError(req *http.Request, resp *http.Response) error {
raw, _ := io.ReadAll(resp.Body)
var errAPI APIError
err := json.Unmarshal(raw, &errAPI)
2022-11-26 02:06:08 +00:00
if err != nil {
2023-05-05 07:49:38 +00:00
return errutils.NewUnexpectedStatusCodeError(req, resp.StatusCode, raw)
}
return fmt.Errorf("[status code: %d] %w", resp.StatusCode, &errAPI)
}
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,
2022-11-26 02:06:08 +00:00
}
2023-05-05 07:49:38 +00:00
return client
2022-11-26 02:06:08 +00:00
}