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

204 lines
4.7 KiB
Go
Raw Permalink Normal View History

2019-03-11 16:56:48 +00:00
package internal
2018-11-02 01:30:09 +00:00
import (
"bytes"
2023-05-05 07:49:38 +00:00
"context"
2018-11-02 01:30:09 +00:00
"encoding/json"
"errors"
"fmt"
2021-08-25 09:44:11 +00:00
"io"
2018-11-02 01:30:09 +00:00
"net/http"
2023-05-05 07:49:38 +00:00
"net/url"
"time"
2018-11-02 01:30:09 +00:00
2023-05-05 07:49:38 +00:00
"github.com/go-acme/lego/v4/providers/dns/internal/errutils"
2018-11-02 01:30:09 +00:00
)
2023-05-05 07:49:38 +00:00
const dnsServiceBaseURL = "https://dns-service.%s.conoha.io"
2018-11-02 01:30:09 +00:00
// Client is a ConoHa API client.
type Client struct {
2023-05-05 07:49:38 +00:00
token string
baseURL *url.URL
HTTPClient *http.Client
2018-11-02 01:30:09 +00:00
}
// NewClient returns a client instance logged into the ConoHa service.
2023-05-05 07:49:38 +00:00
func NewClient(region string, token string) (*Client, error) {
baseURL, err := url.Parse(fmt.Sprintf(dnsServiceBaseURL, region))
if err != nil {
return nil, err
2018-11-02 01:30:09 +00:00
}
2023-05-05 07:49:38 +00:00
return &Client{
token: token,
baseURL: baseURL,
HTTPClient: &http.Client{Timeout: 5 * time.Second},
}, nil
}
2018-11-02 01:30:09 +00:00
2023-05-05 07:49:38 +00:00
// GetDomainID returns an ID of specified domain.
func (c *Client) GetDomainID(ctx context.Context, domainName string) (string, error) {
domainList, err := c.getDomains(ctx)
2018-11-02 01:30:09 +00:00
if err != nil {
2023-05-05 07:49:38 +00:00
return "", err
2018-11-02 01:30:09 +00:00
}
2023-05-05 07:49:38 +00:00
for _, domain := range domainList.Domains {
if domain.Name == domainName {
return domain.ID, nil
}
}
2018-11-02 01:30:09 +00:00
2023-05-05 07:49:38 +00:00
return "", fmt.Errorf("no such domain: %s", domainName)
2018-11-02 01:30:09 +00:00
}
2023-05-05 07:49:38 +00:00
// https://www.conoha.jp/docs/paas-dns-list-domains.php
func (c *Client) getDomains(ctx context.Context) (*DomainListResponse, error) {
endpoint := c.baseURL.JoinPath("v1", "domains")
2018-11-02 01:30:09 +00:00
2023-05-05 07:49:38 +00:00
req, err := newJSONRequest(ctx, http.MethodGet, endpoint, nil)
2018-11-02 01:30:09 +00:00
if err != nil {
return nil, err
}
domainList := &DomainListResponse{}
2023-05-05 07:49:38 +00:00
err = c.do(req, domainList)
2018-11-02 01:30:09 +00:00
if err != nil {
2023-05-05 07:49:38 +00:00
return nil, err
2018-11-02 01:30:09 +00:00
}
2023-05-05 07:49:38 +00:00
return domainList, nil
2018-11-02 01:30:09 +00:00
}
// GetRecordID returns an ID of specified record.
2023-05-05 07:49:38 +00:00
func (c *Client) GetRecordID(ctx context.Context, domainID, recordName, recordType, data string) (string, error) {
recordList, err := c.getRecords(ctx, domainID)
2018-11-02 01:30:09 +00:00
if err != nil {
return "", err
}
for _, record := range recordList.Records {
if record.Name == recordName && record.Type == recordType && record.Data == data {
return record.ID, nil
}
}
2023-05-05 07:49:38 +00:00
2018-11-02 01:30:09 +00:00
return "", errors.New("no such record")
}
2023-05-05 07:49:38 +00:00
// https://www.conoha.jp/docs/paas-dns-list-records-in-a-domain.php
func (c *Client) getRecords(ctx context.Context, domainID string) (*RecordListResponse, error) {
endpoint := c.baseURL.JoinPath("v1", "domains", domainID, "records")
req, err := newJSONRequest(ctx, http.MethodGet, endpoint, nil)
if err != nil {
return nil, err
}
recordList := &RecordListResponse{}
err = c.do(req, recordList)
if err != nil {
return nil, err
}
return recordList, nil
2018-11-02 01:30:09 +00:00
}
2023-05-05 07:49:38 +00:00
// CreateRecord adds new record.
func (c *Client) CreateRecord(ctx context.Context, domainID string, record Record) error {
_, err := c.createRecord(ctx, domainID, record)
return err
2018-11-02 01:30:09 +00:00
}
2023-05-05 07:49:38 +00:00
// https://www.conoha.jp/docs/paas-dns-create-record.php
func (c *Client) createRecord(ctx context.Context, domainID string, record Record) (*Record, error) {
endpoint := c.baseURL.JoinPath("v1", "domains", domainID, "records")
2018-11-02 01:30:09 +00:00
2023-05-05 07:49:38 +00:00
req, err := newJSONRequest(ctx, http.MethodPost, endpoint, record)
if err != nil {
return nil, err
2018-11-02 01:30:09 +00:00
}
2023-05-05 07:49:38 +00:00
newRecord := &Record{}
err = c.do(req, newRecord)
2018-11-02 01:30:09 +00:00
if err != nil {
2023-05-05 07:49:38 +00:00
return nil, err
2018-11-02 01:30:09 +00:00
}
2023-05-05 07:49:38 +00:00
return newRecord, nil
}
2018-11-02 01:30:09 +00:00
2023-05-05 07:49:38 +00:00
// DeleteRecord removes specified record.
// https://www.conoha.jp/docs/paas-dns-delete-a-record.php
func (c *Client) DeleteRecord(ctx context.Context, domainID, recordID string) error {
endpoint := c.baseURL.JoinPath("v1", "domains", domainID, "records", recordID)
req, err := newJSONRequest(ctx, http.MethodDelete, endpoint, nil)
2018-11-02 01:30:09 +00:00
if err != nil {
return err
}
2023-05-05 07:49:38 +00:00
return c.do(req, nil)
}
func (c *Client) do(req *http.Request, result any) error {
if c.token != "" {
req.Header.Set("X-Auth-Token", c.token)
}
resp, err := c.HTTPClient.Do(req)
if err != nil {
return errutils.NewHTTPDoError(req, err)
}
defer func() { _ = resp.Body.Close() }()
2018-11-02 01:30:09 +00:00
if resp.StatusCode != http.StatusOK {
2023-05-05 07:49:38 +00:00
return errutils.NewUnexpectedResponseStatusCodeError(req, resp)
}
2018-11-02 01:30:09 +00:00
2023-05-05 07:49:38 +00:00
if result == nil {
return nil
2018-11-02 01:30:09 +00:00
}
2023-05-05 07:49:38 +00: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)
}
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)
2018-11-02 01:30:09 +00:00
if err != nil {
2023-05-05 07:49:38 +00:00
return nil, fmt.Errorf("failed to create request JSON body: %w", err)
2018-11-02 01:30:09 +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)
}
req.Header.Set("Accept", "application/json")
2018-11-02 01:30:09 +00:00
2023-05-05 07:49:38 +00:00
if payload != nil {
req.Header.Set("Content-Type", "application/json")
2018-11-02 01:30:09 +00:00
}
2023-05-05 07:49:38 +00:00
return req, nil
2018-11-02 01:30:09 +00:00
}