lego/providers/dns/constellix/internal/domains.go

91 lines
2.2 KiB
Go
Raw Normal View History

package internal
import (
2023-05-05 07:49:38 +00:00
"context"
2020-04-18 18:39:10 +00:00
"errors"
"fmt"
"net/http"
querystring "github.com/google/go-querystring/query"
)
// DomainService API access to Domain.
type DomainService service
2020-04-18 18:39:10 +00:00
// GetAll domains.
// https://api-docs.constellix.com/?version=latest#484c3f21-d724-4ee4-a6fa-ab22c8eb9e9b
2023-05-05 07:49:38 +00:00
func (s *DomainService) GetAll(ctx context.Context, params *PaginationParameters) ([]Domain, error) {
2020-04-18 18:39:10 +00:00
endpoint, err := s.client.createEndpoint(defaultVersion, "domains")
if err != nil {
return nil, fmt.Errorf("failed to create request endpoint: %w", err)
}
2023-05-05 07:49:38 +00:00
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil)
if err != nil {
2023-05-05 07:49:38 +00:00
return nil, fmt.Errorf("unable to create request: %w", err)
}
2020-04-18 18:39:10 +00:00
if params != nil {
v, errQ := querystring.Values(params)
if errQ != nil {
return nil, errQ
}
2020-04-18 18:39:10 +00:00
req.URL.RawQuery = v.Encode()
}
2020-04-18 18:39:10 +00:00
var domains []Domain
err = s.client.do(req, &domains)
if err != nil {
return nil, err
}
2020-04-18 18:39:10 +00:00
return domains, nil
}
2020-04-18 18:39:10 +00:00
// GetByName Gets domain by name.
2023-05-05 07:49:38 +00:00
func (s *DomainService) GetByName(ctx context.Context, domainName string) (Domain, error) {
domains, err := s.Search(ctx, Exact, domainName)
2020-04-18 18:39:10 +00:00
if err != nil {
return Domain{}, err
}
if len(domains) == 0 {
return Domain{}, fmt.Errorf("domain not found: %s", domainName)
}
if len(domains) > 1 {
return Domain{}, fmt.Errorf("multiple domains found: %v", domains)
}
2020-04-18 18:39:10 +00:00
return domains[0], nil
}
2020-04-18 18:39:10 +00:00
// Search searches for a domain by name.
// https://api-docs.constellix.com/?version=latest#3d7b2679-2209-49f3-b011-b7d24e512008
2023-05-05 07:49:38 +00:00
func (s *DomainService) Search(ctx context.Context, filter searchFilter, value string) ([]Domain, error) {
2020-04-18 18:39:10 +00:00
endpoint, err := s.client.createEndpoint(defaultVersion, "domains", "search")
if err != nil {
return nil, fmt.Errorf("failed to create request endpoint: %w", err)
}
2023-05-05 07:49:38 +00:00
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil)
if err != nil {
2023-05-05 07:49:38 +00:00
return nil, fmt.Errorf("unable to create request: %w", err)
}
2020-04-18 18:39:10 +00:00
query := req.URL.Query()
query.Set(string(filter), value)
req.URL.RawQuery = query.Encode()
var domains []Domain
err = s.client.do(req, &domains)
if err != nil {
2020-04-18 18:39:10 +00:00
var nf *NotFound
if !errors.As(err, &nf) {
return nil, err
}
}
return domains, nil
}