Debian Codegraph @

dnsimple/domains_records.go

raw
1 package dnsimple
2
3 import (
4 "fmt"
5 "net/url"
6 "time"
7 )
8
9 type Record struct {
10 Id int `json:"id,omitempty"`
11 DomainId int `json:"domain_id,omitempty"`
12 Name string `json:"name,omitempty"`
13 Content string `json:"content,omitempty"`
14 TTL int `json:"ttl,omitempty"`
15 Priority int `json:"prio,omitempty"`
16 Type string `json:"record_type,omitempty"`
17 CreatedAt *time.Time `json:"created_at,omitempty"`
18 UpdatedAt *time.Time `json:"updated_at,omitempty"`
19 }
20
21 type recordWrapper struct {
22 Record Record `json:"record"`
23 }
24
25 // recordPath generates the resource path for given record that belongs to a domain.
26 func recordPath(domain interface{}, record interface{}) string {
27 path := fmt.Sprintf("domains/%s/records", domainIdentifier(domain))
28
29 if record != nil {
30 path += fmt.Sprintf("/%d", record)
31 }
32
33 return path
34 }
35
36 // List the domain records.
37 //
38 // DNSimple API docs: http://developer.dnsimple.com/domains/records/#list
39 func (s *DomainsService) ListRecords(domain interface{}, recordName, recordType string) ([]Record, *Response, error) {
40 reqStr := recordPath(domain, nil)
41 v := url.Values{}
42
43 if recordName != "" {
44 v.Add("name", recordName)
45 }
46 if recordType != "" {
47 v.Add("type", recordType)
48 }
49 reqStr += "?" + v.Encode()
50
51 wrappedRecords := []recordWrapper{}
52
53 res, err := s.client.get(reqStr, &wrappedRecords)
54 if err != nil {
55 return []Record{}, res, err
56 }
57
58 records := []Record{}
59 for _, record := range wrappedRecords {
60 records = append(records, record.Record)
61 }
62
63 return records, res, nil
64 }
65
66 // CreateRecord creates a domain record.
67 //
68 // DNSimple API docs: http://developer.dnsimple.com/domains/records/#create
69 func (s *DomainsService) CreateRecord(domain interface{}, recordAttributes Record) (Record, *Response, error) {
70 path := recordPath(domain, nil)
71 wrappedRecord := recordWrapper{Record: recordAttributes}
72 returnedRecord := recordWrapper{}
73
74 res, err := s.client.post(path, wrappedRecord, &returnedRecord)
75 if err != nil {
76 return Record{}, res, err
77 }
78
79 return returnedRecord.Record, res, nil
80 }
81
82 // GetRecord fetches the domain record.
83 //
84 // DNSimple API docs: http://developer.dnsimple.com/domains/records/#get
85 func (s *DomainsService) GetRecord(domain interface{}, recordID int) (Record, *Response, error) {
86 path := recordPath(domain, recordID)
87 wrappedRecord := recordWrapper{}
88
89 res, err := s.client.get(path, &wrappedRecord)
90 if err != nil {
91 return Record{}, res, err
92 }
93
94 return wrappedRecord.Record, res, nil
95 }
96
97 // UpdateRecord updates a domain record.
98 //
99 // DNSimple API docs: http://developer.dnsimple.com/domains/records/#update
100 func (s *DomainsService) UpdateRecord(domain interface{}, recordID int, recordAttributes Record) (Record, *Response, error) {
101 path := recordPath(domain, recordID)
102 // name, content, ttl, priority
103 wrappedRecord := recordWrapper{
104 Record: Record{
105 Name: recordAttributes.Name,
106 Content: recordAttributes.Content,
107 TTL: recordAttributes.TTL,
108 Priority: recordAttributes.Priority}}
109 returnedRecord := recordWrapper{}
110
111 res, err := s.client.put(path, wrappedRecord, &returnedRecord)
112 if err != nil {
113 return Record{}, res, err
114 }
115
116 return returnedRecord.Record, res, nil
117 }
118
119 // DeleteRecord deletes a domain record.
120 //
121 // DNSimple API docs: http://developer.dnsimple.com/domains/records/#delete
122 func (s *DomainsService) DeleteRecord(domain interface{}, recordID int) (*Response, error) {
123 path := recordPath(domain, recordID)
124
125 return s.client.delete(path, nil)
126 }
127
128 // UpdateIP updates the IP of specific A record.
129 //
130 // This is not part of the standard API. However,
131 // this is useful for Dynamic DNS (DDNS or DynDNS).
132 func (record *Record) UpdateIP(client *Client, IP string) error {
133 newRecord := Record{Content: IP, Name: record.Name}
134 _, _, err := client.Domains.UpdateRecord(record.DomainId, record.Id, newRecord)
135 return err
136 }