records.go raw

   1  package dnspod
   2  
   3  import (
   4  	"fmt"
   5  )
   6  
   7  const (
   8  	methodRecordList   = "Record.List"
   9  	methodRecordCreate = "Record.Create"
  10  	methodRecordInfo   = "Record.Info"
  11  	methodRecordRemove = "Record.Remove"
  12  	methodRecordModify = "Record.Modify"
  13  )
  14  
  15  // Record is the DNS record representation.
  16  type Record struct {
  17  	ID            string `json:"id,omitempty"`
  18  	Name          string `json:"name,omitempty"`
  19  	Line          string `json:"line,omitempty"`
  20  	LineID        string `json:"line_id,omitempty"`
  21  	Type          string `json:"type,omitempty"`
  22  	TTL           string `json:"ttl,omitempty"`
  23  	Value         string `json:"value,omitempty"`
  24  	MX            string `json:"mx,omitempty"`
  25  	Enabled       string `json:"enabled,omitempty"`
  26  	Status        string `json:"status,omitempty"`
  27  	MonitorStatus string `json:"monitor_status,omitempty"`
  28  	Remark        string `json:"remark,omitempty"`
  29  	UpdateOn      string `json:"updated_on,omitempty"`
  30  	UseAQB        string `json:"use_aqb,omitempty"`
  31  }
  32  
  33  type recordsWrapper struct {
  34  	Status  Status     `json:"status"`
  35  	Info    DomainInfo `json:"info"`
  36  	Records []Record   `json:"records"`
  37  }
  38  
  39  type recordWrapper struct {
  40  	Status Status     `json:"status"`
  41  	Info   DomainInfo `json:"info"`
  42  	Record Record     `json:"record"`
  43  }
  44  
  45  // RecordsService handles communication with the DNS records related methods of the dnspod API.
  46  //
  47  // dnspod API docs: https://www.dnspod.cn/docs/records.html
  48  type RecordsService struct {
  49  	client *Client
  50  }
  51  
  52  // List List the domain records.
  53  //
  54  // dnspod API docs: https://www.dnspod.cn/docs/records.html#record-list
  55  func (s *RecordsService) List(domainID string, recordName string) ([]Record, *Response, error) {
  56  	payload := s.client.CommonParams.toPayLoad()
  57  	payload.Add("domain_id", domainID)
  58  	if recordName != "" {
  59  		payload.Add("sub_domain", recordName)
  60  	}
  61  
  62  	wrappedRecords := recordsWrapper{}
  63  
  64  	res, err := s.client.post(methodRecordList, payload, &wrappedRecords)
  65  	if err != nil {
  66  		return nil, res, err
  67  	}
  68  
  69  	if wrappedRecords.Status.Code != "1" {
  70  		return nil, nil, fmt.Errorf("could not get domains: %s", wrappedRecords.Status.Message)
  71  	}
  72  
  73  	return wrappedRecords.Records, res, nil
  74  }
  75  
  76  // Create Creates a domain record.
  77  //
  78  // dnspod API docs: https://www.dnspod.cn/docs/records.html#record-create
  79  func (s *RecordsService) Create(domain string, recordAttributes Record) (Record, *Response, error) {
  80  	payload := s.client.CommonParams.toPayLoad()
  81  	payload.Add("domain_id", domain)
  82  
  83  	if recordAttributes.Name != "" {
  84  		payload.Add("sub_domain", recordAttributes.Name)
  85  	}
  86  
  87  	if recordAttributes.Type != "" {
  88  		payload.Add("record_type", recordAttributes.Type)
  89  	}
  90  
  91  	if recordAttributes.Line != "" {
  92  		payload.Add("record_line", recordAttributes.Line)
  93  	}
  94  
  95  	if recordAttributes.LineID != "" {
  96  		payload.Add("record_line_id", recordAttributes.LineID)
  97  	}
  98  
  99  	if recordAttributes.Value != "" {
 100  		payload.Add("value", recordAttributes.Value)
 101  	}
 102  
 103  	if recordAttributes.MX != "" {
 104  		payload.Add("mx", recordAttributes.MX)
 105  	}
 106  
 107  	if recordAttributes.TTL != "" {
 108  		payload.Add("ttl", recordAttributes.TTL)
 109  	}
 110  
 111  	if recordAttributes.Status != "" {
 112  		payload.Add("status", recordAttributes.Status)
 113  	}
 114  
 115  	returnedRecord := recordWrapper{}
 116  
 117  	res, err := s.client.post(methodRecordCreate, payload, &returnedRecord)
 118  	if err != nil {
 119  		return Record{}, res, err
 120  	}
 121  
 122  	if returnedRecord.Status.Code != "1" {
 123  		return returnedRecord.Record, nil, fmt.Errorf("could not get domains: %s", returnedRecord.Status.Message)
 124  	}
 125  
 126  	return returnedRecord.Record, res, nil
 127  }
 128  
 129  // Get Fetches the domain record.
 130  //
 131  // dnspod API docs: https://www.dnspod.cn/docs/records.html#record-info
 132  func (s *RecordsService) Get(domain string, recordID string) (Record, *Response, error) {
 133  	payload := s.client.CommonParams.toPayLoad()
 134  	payload.Add("domain_id", domain)
 135  	payload.Add("record_id", recordID)
 136  
 137  	returnedRecord := recordWrapper{}
 138  
 139  	res, err := s.client.post(methodRecordInfo, payload, &returnedRecord)
 140  	if err != nil {
 141  		return Record{}, res, err
 142  	}
 143  
 144  	if returnedRecord.Status.Code != "1" {
 145  		return returnedRecord.Record, nil, fmt.Errorf("could not get domains: %s", returnedRecord.Status.Message)
 146  	}
 147  
 148  	return returnedRecord.Record, res, nil
 149  }
 150  
 151  // Update Updates a domain record.
 152  //
 153  // dnspod API docs: https://www.dnspod.cn/docs/records.html#record-modify
 154  func (s *RecordsService) Update(domain string, recordID string, recordAttributes Record) (Record, *Response, error) {
 155  	payload := s.client.CommonParams.toPayLoad()
 156  	payload.Add("domain_id", domain)
 157  
 158  	if recordAttributes.Name != "" {
 159  		payload.Add("sub_domain", recordAttributes.Name)
 160  	}
 161  
 162  	if recordAttributes.Type != "" {
 163  		payload.Add("record_type", recordAttributes.Type)
 164  	}
 165  
 166  	if recordAttributes.Line != "" {
 167  		payload.Add("record_line", recordAttributes.Line)
 168  	}
 169  
 170  	if recordAttributes.LineID != "" {
 171  		payload.Add("record_line_id", recordAttributes.LineID)
 172  	}
 173  
 174  	if recordAttributes.Value != "" {
 175  		payload.Add("value", recordAttributes.Value)
 176  	}
 177  
 178  	if recordAttributes.MX != "" {
 179  		payload.Add("mx", recordAttributes.MX)
 180  	}
 181  
 182  	if recordAttributes.TTL != "" {
 183  		payload.Add("ttl", recordAttributes.TTL)
 184  	}
 185  
 186  	if recordAttributes.Status != "" {
 187  		payload.Add("status", recordAttributes.Status)
 188  	}
 189  
 190  	returnedRecord := recordWrapper{}
 191  
 192  	res, err := s.client.post(methodRecordModify, payload, &returnedRecord)
 193  	if err != nil {
 194  		return Record{}, res, err
 195  	}
 196  
 197  	if returnedRecord.Status.Code != "1" {
 198  		return returnedRecord.Record, nil, fmt.Errorf("could not get domains: %s", returnedRecord.Status.Message)
 199  	}
 200  
 201  	return returnedRecord.Record, res, nil
 202  }
 203  
 204  // Delete Deletes a domain record.
 205  //
 206  // dnspod API docs: https://www.dnspod.cn/docs/records.html#record-remove
 207  func (s *RecordsService) Delete(domain string, recordID string) (*Response, error) {
 208  	payload := s.client.CommonParams.toPayLoad()
 209  	payload.Add("domain_id", domain)
 210  	payload.Add("record_id", recordID)
 211  
 212  	returnedRecord := recordWrapper{}
 213  
 214  	res, err := s.client.post(methodRecordRemove, payload, &returnedRecord)
 215  	if err != nil {
 216  		return res, err
 217  	}
 218  
 219  	if returnedRecord.Status.Code != "1" {
 220  		return nil, fmt.Errorf("could not get domains: %s", returnedRecord.Status.Message)
 221  	}
 222  
 223  	return res, nil
 224  }
 225