record.go raw
1 package rest
2
3 import (
4 "errors"
5 "fmt"
6 "net/http"
7
8 "gopkg.in/ns1/ns1-go.v2/rest/model/dns"
9 )
10
11 // RecordsService handles 'zones/ZONE/DOMAIN/TYPE' endpoint.
12 type RecordsService service
13
14 // Get takes a zone, domain and record type t and returns full configuration for a DNS record.
15 //
16 // NS1 API docs: https://ns1.com/api/#record-get
17 func (s *RecordsService) Get(zone, domain, t string) (*dns.Record, *http.Response, error) {
18 path := fmt.Sprintf("zones/%s/%s/%s", zone, domain, t)
19
20 req, err := s.client.NewRequest("GET", path, nil)
21 if err != nil {
22 return nil, nil, err
23 }
24
25 var r dns.Record
26 resp, err := s.client.Do(req, &r)
27 if err != nil {
28 switch err := err.(type) {
29 case *Error:
30 if err.Message == "record not found" {
31 return nil, resp, ErrRecordMissing
32 }
33 }
34 return nil, resp, err
35 }
36
37 return &r, resp, nil
38 }
39
40 // Create takes a *Record and creates a new DNS record in the specified zone, for the specified domain, of the given record type.
41 //
42 // The given record must have at least one answer.
43 // NS1 API docs: https://ns1.com/api/#record-put
44 func (s *RecordsService) Create(r *dns.Record) (*http.Response, error) {
45 path := fmt.Sprintf("zones/%s/%s/%s", r.Zone, r.Domain, r.Type)
46
47 req, err := s.client.NewRequest("PUT", path, &r)
48 if err != nil {
49 return nil, err
50 }
51
52 // Update record fields with data from api(ensure consistent)
53 resp, err := s.client.Do(req, &r)
54 if err != nil {
55 switch err := err.(type) {
56 case *Error:
57 switch err.Message {
58 case "zone not found":
59 return resp, ErrZoneMissing
60 case "record already exists":
61 return resp, ErrRecordExists
62 }
63 }
64 return resp, err
65 }
66
67 return resp, nil
68 }
69
70 // Update takes a *Record and modifies configuration details for an existing DNS record.
71 //
72 // Only the fields to be updated are required in the given record.
73 // NS1 API docs: https://ns1.com/api/#record-post
74 func (s *RecordsService) Update(r *dns.Record) (*http.Response, error) {
75 path := fmt.Sprintf("zones/%s/%s/%s", r.Zone, r.Domain, r.Type)
76
77 req, err := s.client.NewRequest("POST", path, &r)
78 if err != nil {
79 return nil, err
80 }
81
82 // Update records fields with data from api(ensure consistent)
83 resp, err := s.client.Do(req, &r)
84 if err != nil {
85 switch err := err.(type) {
86 case *Error:
87 switch err.Message {
88 case "zone not found":
89 return resp, ErrZoneMissing
90 case "record not found":
91 return resp, ErrRecordMissing
92 case "record already exists":
93 return resp, ErrRecordExists
94 }
95 }
96 return resp, err
97 }
98
99 return resp, nil
100 }
101
102 // Delete takes a zone, domain and record type t and removes an existing record and all associated answers and configuration details.
103 //
104 // NS1 API docs: https://ns1.com/api/#record-delete
105 func (s *RecordsService) Delete(zone string, domain string, t string) (*http.Response, error) {
106 path := fmt.Sprintf("zones/%s/%s/%s", zone, domain, t)
107
108 req, err := s.client.NewRequest("DELETE", path, nil)
109 if err != nil {
110 return nil, err
111 }
112
113 resp, err := s.client.Do(req, nil)
114 if err != nil {
115 switch err := err.(type) {
116 case *Error:
117 if err.Message == "record not found" {
118 return resp, ErrRecordMissing
119 }
120 }
121 return resp, err
122 }
123
124 return resp, nil
125 }
126
127 var (
128 // ErrRecordExists bundles PUT create error.
129 ErrRecordExists = errors.New("record already exists")
130 // ErrRecordMissing bundles GET/POST/DELETE error.
131 ErrRecordMissing = errors.New("record does not exist")
132 )
133