records.go raw

   1  package vegadns
   2  
   3  import (
   4  	"context"
   5  	"errors"
   6  	"net/http"
   7  	"net/url"
   8  	"strconv"
   9  )
  10  
  11  // Record struct representing a Record object.
  12  // https://github.com/shupp/VegaDNS-API/blob/master/records_format.json
  13  type Record struct {
  14  	Name       string `json:"name"`
  15  	Value      string `json:"value"`
  16  	RecordType string `json:"record_type"`
  17  	TTL        int    `json:"ttl"`
  18  	RecordID   int    `json:"record_id"`
  19  	LocationID string `json:"location_id"`
  20  	DomainID   int    `json:"domain_id"`
  21  }
  22  
  23  // RecordsResponse api response list of records.
  24  type RecordsResponse struct {
  25  	Status  string   `json:"status"`
  26  	Total   int      `json:"total_records"`
  27  	Domain  Domain   `json:"domain"`
  28  	Records []Record `json:"records"`
  29  }
  30  
  31  // GetRecordID helper to get the id of a record.
  32  func (c *Client) GetRecordID(ctx context.Context, domainID int, name, recordType string) (int, error) {
  33  	records, err := c.GetRecords(ctx, domainID)
  34  	if err != nil {
  35  		return -1, err
  36  	}
  37  
  38  	for _, r := range records {
  39  		if r.Name == name && r.RecordType == recordType {
  40  			return r.RecordID, nil
  41  		}
  42  	}
  43  
  44  	return -1, errors.New("record not found")
  45  }
  46  
  47  // GetRecords retrieves all DNS records associated with the specified domain ID.
  48  // https://generator.swagger.io/?url=https://raw.githubusercontent.com/shupp/VegaDNS-API/refs/heads/master/swagger/vegadns.swagger.json#/Records/get_records
  49  func (c *Client) GetRecords(ctx context.Context, domainID int) ([]Record, error) {
  50  	params := make(url.Values)
  51  	params.Set("domain_id", strconv.Itoa(domainID))
  52  
  53  	req, err := c.newRequest(ctx, http.MethodGet, c.baseURL.JoinPath("records"), params)
  54  	if err != nil {
  55  		return nil, err
  56  	}
  57  
  58  	answer := RecordsResponse{}
  59  
  60  	err = c.do(req, http.StatusOK, &answer)
  61  	if err != nil {
  62  		return nil, err
  63  	}
  64  
  65  	return answer.Records, nil
  66  }
  67  
  68  // CreateTXTRecord creates a TXT record.
  69  // https://generator.swagger.io/?url=https://raw.githubusercontent.com/shupp/VegaDNS-API/refs/heads/master/swagger/vegadns.swagger.json#/Records/post_records
  70  func (c *Client) CreateTXTRecord(ctx context.Context, domainID int, name, value string, ttl int) error {
  71  	params := make(url.Values)
  72  	params.Set("record_type", "TXT")
  73  	params.Set("ttl", strconv.Itoa(ttl))
  74  	params.Set("domain_id", strconv.Itoa(domainID))
  75  	params.Set("name", name)
  76  	params.Set("value", value)
  77  
  78  	req, err := c.newRequest(ctx, http.MethodPost, c.baseURL.JoinPath("records"), params)
  79  	if err != nil {
  80  		return err
  81  	}
  82  
  83  	return c.do(req, http.StatusCreated, nil)
  84  }
  85  
  86  // DeleteRecord deletes a record.
  87  // https://generator.swagger.io/?url=https://raw.githubusercontent.com/shupp/VegaDNS-API/refs/heads/master/swagger/vegadns.swagger.json#/Records/delete_records__record_id_
  88  func (c *Client) DeleteRecord(ctx context.Context, recordID int) error {
  89  	req, err := c.newRequest(ctx, http.MethodDelete, c.baseURL.JoinPath("records", strconv.Itoa(recordID)), nil)
  90  	if err != nil {
  91  		return err
  92  	}
  93  
  94  	return c.do(req, http.StatusOK, nil)
  95  }
  96