domains.go raw

   1  package vegadns
   2  
   3  import (
   4  	"context"
   5  	"fmt"
   6  	"net/http"
   7  	"net/url"
   8  )
   9  
  10  // Domain struct containing a domain object.
  11  type Domain struct {
  12  	Status   string `json:"status"`
  13  	Domain   string `json:"domain"`
  14  	DomainID int    `json:"domain_id"`
  15  	OwnerID  int    `json:"owner_id"`
  16  }
  17  
  18  // DomainResponse api response of a domain list.
  19  type DomainResponse struct {
  20  	Status  string   `json:"status"`
  21  	Total   int      `json:"total_domains"`
  22  	Domains []Domain `json:"domains"`
  23  }
  24  
  25  // GetDomainID returns the id for a domain.
  26  func (c *Client) GetDomainID(ctx context.Context, domain string) (int, error) {
  27  	domains, err := c.GetDomains(ctx, domain)
  28  	if err != nil {
  29  		return -1, err
  30  	}
  31  
  32  	for _, d := range domains {
  33  		if d.Domain == domain {
  34  			return d.DomainID, nil
  35  		}
  36  	}
  37  
  38  	return -1, fmt.Errorf("domain %s not found", domain)
  39  }
  40  
  41  // GetDomains gets domains.
  42  // https://generator.swagger.io/?url=https://raw.githubusercontent.com/shupp/VegaDNS-API/refs/heads/master/swagger/vegadns.swagger.json#/Domains/get_domains
  43  func (c *Client) GetDomains(ctx context.Context, domain string) ([]Domain, error) {
  44  	params := make(url.Values)
  45  
  46  	if domain != "" {
  47  		params.Set("search", domain)
  48  	}
  49  
  50  	req, err := c.newRequest(ctx, http.MethodGet, c.baseURL.JoinPath("domains"), params)
  51  	if err != nil {
  52  		return nil, err
  53  	}
  54  
  55  	answer := DomainResponse{}
  56  
  57  	err = c.do(req, http.StatusOK, &answer)
  58  	if err != nil {
  59  		return nil, err
  60  	}
  61  
  62  	return answer.Domains, nil
  63  }
  64