domain_records.go raw

   1  package govultr
   2  
   3  import (
   4  	"context"
   5  	"fmt"
   6  	"net/http"
   7  
   8  	"github.com/google/go-querystring/query"
   9  )
  10  
  11  // DomainRecordService is the interface to interact with the DNS Records endpoints on the Vultr API
  12  // Link: https://www.vultr.com/api/#tag/dns
  13  type DomainRecordService interface {
  14  	Create(ctx context.Context, domain string, domainRecordCreateReq *DomainRecordCreateReq) (*DomainRecord, *http.Response, error)
  15  	Get(ctx context.Context, domain, recordID string) (*DomainRecord, *http.Response, error)
  16  	Update(ctx context.Context, domain, recordID string, domainRecordUpdateReq *DomainRecordUpdateReq) error
  17  	Delete(ctx context.Context, domain, recordID string) error
  18  	List(ctx context.Context, domain string, options *ListOptions) ([]DomainRecord, *Meta, *http.Response, error)
  19  }
  20  
  21  // DomainRecordsServiceHandler handles interaction with the DNS Records methods for the Vultr API
  22  type DomainRecordsServiceHandler struct {
  23  	client *Client
  24  }
  25  
  26  // DomainRecord represents a DNS record on Vultr
  27  type DomainRecord struct {
  28  	ID       string `json:"id,omitempty"`
  29  	Type     string `json:"type,omitempty"`
  30  	Name     string `json:"name,omitempty"`
  31  	Data     string `json:"data,omitempty"`
  32  	Priority int    `json:"priority,omitempty"`
  33  	TTL      int    `json:"ttl,omitempty"`
  34  }
  35  
  36  // DomainRecordCreateReq struct to use for create domain record calls.
  37  type DomainRecordCreateReq struct {
  38  	Name     string `json:"name"`
  39  	Type     string `json:"type"`
  40  	Data     string `json:"data"`
  41  	TTL      int    `json:"ttl,omitempty"`
  42  	Priority *int   `json:"priority,omitempty"`
  43  }
  44  
  45  // DomainRecordUpdateReq struct to use for update domain record calls.
  46  type DomainRecordUpdateReq struct {
  47  	Name     *string `json:"name,omitempty"`
  48  	Type     string  `json:"type,omitempty"`
  49  	Data     string  `json:"data,omitempty"`
  50  	TTL      int     `json:"ttl,omitempty"`
  51  	Priority *int    `json:"priority,omitempty"`
  52  }
  53  
  54  type domainRecordsBase struct {
  55  	Records []DomainRecord `json:"records,omitempty"`
  56  	Meta    *Meta          `json:"meta,omitempty"`
  57  }
  58  
  59  type domainRecordBase struct {
  60  	Record *DomainRecord `json:"record,omitempty"`
  61  }
  62  
  63  // Create will add a DNS record.
  64  func (d *DomainRecordsServiceHandler) Create(ctx context.Context, domain string, domainRecordCreateReq *DomainRecordCreateReq) (*DomainRecord, *http.Response, error) { //nolint:lll
  65  	req, err := d.client.NewRequest(ctx, http.MethodPost, fmt.Sprintf("%s/%s/records", domainPath, domain), domainRecordCreateReq)
  66  	if err != nil {
  67  		return nil, nil, err
  68  	}
  69  
  70  	record := new(domainRecordBase)
  71  	resp, err := d.client.DoWithContext(ctx, req, record)
  72  	if err != nil {
  73  		return nil, resp, err
  74  	}
  75  
  76  	return record.Record, resp, nil
  77  }
  78  
  79  // Get record from a domain
  80  func (d *DomainRecordsServiceHandler) Get(ctx context.Context, domain, recordID string) (*DomainRecord, *http.Response, error) {
  81  	req, err := d.client.NewRequest(ctx, http.MethodGet, fmt.Sprintf("%s/%s/records/%s", domainPath, domain, recordID), nil)
  82  	if err != nil {
  83  		return nil, nil, err
  84  	}
  85  
  86  	record := new(domainRecordBase)
  87  	resp, err := d.client.DoWithContext(ctx, req, record)
  88  	if err != nil {
  89  		return nil, resp, err
  90  	}
  91  
  92  	return record.Record, resp, nil
  93  }
  94  
  95  // Update will update a Domain record
  96  func (d *DomainRecordsServiceHandler) Update(ctx context.Context, domain, recordID string, domainRecordUpdateReq *DomainRecordUpdateReq) error { //nolint:lll
  97  	uri := fmt.Sprintf("%s/%s/records/%s", domainPath, domain, recordID)
  98  
  99  	req, err := d.client.NewRequest(ctx, http.MethodPatch, uri, domainRecordUpdateReq)
 100  	if err != nil {
 101  		return err
 102  	}
 103  
 104  	_, err = d.client.DoWithContext(ctx, req, nil)
 105  	return err
 106  }
 107  
 108  // Delete will delete a domain name and all associated records.
 109  func (d *DomainRecordsServiceHandler) Delete(ctx context.Context, domain, recordID string) error {
 110  	req, err := d.client.NewRequest(ctx, http.MethodDelete, fmt.Sprintf("%s/%s/records/%s", domainPath, domain, recordID), nil)
 111  	if err != nil {
 112  		return err
 113  	}
 114  	_, err = d.client.DoWithContext(ctx, req, nil)
 115  	return err
 116  }
 117  
 118  // List will list all the records associated with a particular domain on Vultr.
 119  func (d *DomainRecordsServiceHandler) List(ctx context.Context, domain string, options *ListOptions) ([]DomainRecord, *Meta, *http.Response, error) { //nolint:lll
 120  	req, err := d.client.NewRequest(ctx, http.MethodGet, fmt.Sprintf("%s/%s/records", domainPath, domain), nil)
 121  	if err != nil {
 122  		return nil, nil, nil, err
 123  	}
 124  
 125  	newValues, err := query.Values(options)
 126  	if err != nil {
 127  		return nil, nil, nil, err
 128  	}
 129  	req.URL.RawQuery = newValues.Encode()
 130  
 131  	records := new(domainRecordsBase)
 132  	resp, err := d.client.DoWithContext(ctx, req, records)
 133  	if err != nil {
 134  		return nil, nil, resp, err
 135  	}
 136  
 137  	return records.Records, records.Meta, resp, nil
 138  }
 139