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

90 lines
2.1 KiB
Go
Raw Normal View History

package internal
import (
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
func (s *DomainService) GetAll(params *PaginationParameters) ([]Domain, error) {
endpoint, err := s.client.createEndpoint(defaultVersion, "domains")
if err != nil {
return nil, fmt.Errorf("failed to create request endpoint: %w", err)
}
2020-04-18 18:39:10 +00:00
req, err := http.NewRequest(http.MethodGet, endpoint, nil)
if err != nil {
2020-04-18 18:39:10 +00:00
return nil, fmt.Errorf("failed 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.
func (s *DomainService) GetByName(domainName string) (Domain, error) {
domains, err := s.Search(Exact, domainName)
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
func (s *DomainService) Search(filter searchFilter, value string) ([]Domain, error) {
endpoint, err := s.client.createEndpoint(defaultVersion, "domains", "search")
if err != nil {
return nil, fmt.Errorf("failed to create request endpoint: %w", err)
}
req, err := http.NewRequest(http.MethodGet, endpoint, nil)
if err != nil {
return nil, fmt.Errorf("failed 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
}