-
Notifications
You must be signed in to change notification settings - Fork 3
/
client_records.go
120 lines (111 loc) · 3.21 KB
/
client_records.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package cloudflare
import (
"bytes"
"encoding/json"
"golang.org/x/net/context"
)
// Records - Cloudflare Records API Client.
type Records struct {
// Options - Client options.
*Options
}
// Create - Creates a zone DNS record.
// Required parameters of a record are - `type`, `name` and `content`.
// Optional parameters of a record are - `ttl`.
func (records *Records) Create(ctx context.Context, record *Record) (err error) {
buffer := new(bytes.Buffer)
err = json.NewEncoder(buffer).Encode(record)
if err != nil {
return
}
response, err := httpDo(ctx, records.Options, "POST", apiURL("/zones/%s/dns_records", record.ZoneID), buffer)
if err != nil {
return
}
defer response.Body.Close()
result, err := readResponse(response.Body)
if err != nil {
return
}
res := new(Record)
err = json.Unmarshal(result.Result, &res)
if err != nil {
return
}
record.ID = res.ID
record.TTL = res.TTL
record.Locked = res.Locked
record.Proxied = res.Proxied
record.Proxiable = res.Proxiable
record.CreatedOn = res.CreatedOn
record.ModifiedOn = res.ModifiedOn
return
}
// List - Lists all zone DNS records.
func (records *Records) List(ctx context.Context, zoneID string) ([]*Record, error) {
return records.listPages(ctx, zoneID, 1)
}
// Details - Requests zone DNS record details by zone ID and record ID.
func (records *Records) Details(ctx context.Context, zoneID, recordID string) (record *Record, err error) {
response, err := httpDo(ctx, records.Options, "GET", apiURL("/zones/%s/dns_records/%s", zoneID, recordID), nil)
if err != nil {
return
}
defer response.Body.Close()
result, err := readResponse(response.Body)
if err != nil {
return
}
record = new(Record)
err = json.Unmarshal(result.Result, &record)
return
}
// Patch - Patches a zone DNS record.
func (records *Records) Patch(ctx context.Context, record *Record) (err error) {
buffer := new(bytes.Buffer)
err = json.NewEncoder(buffer).Encode(record)
if err != nil {
return
}
response, err := httpDo(ctx, records.Options, "PUT", apiURL("/zones/%s/dns_records/%s", record.ZoneID, record.ID), buffer)
if err != nil {
return
}
defer response.Body.Close()
_, err = readResponse(response.Body)
return
}
// Delete - Deletes zone DNS record by zone ID and record ID.
func (records *Records) Delete(ctx context.Context, zoneID, recordID string) (err error) {
response, err := httpDo(ctx, records.Options, "DELETE", apiURL("/zones/%s/dns_records/%s", zoneID, recordID), nil)
if err != nil {
return
}
defer response.Body.Close()
_, err = readResponse(response.Body)
return
}
// listPages - Gets all pages starting from `page`.
func (records *Records) listPages(ctx context.Context, zoneID string, page int) (list []*Record, err error) {
response, err := httpDo(ctx, records.Options, "GET", apiURL("/zones/%s/dns_records?page=%d&per_page=50", zoneID, page), nil)
if err != nil {
return
}
defer response.Body.Close()
result, err := readResponse(response.Body)
if err != nil {
return
}
err = json.Unmarshal(result.Result, &list)
if err != nil {
return
}
if result.ResultInfo == nil || page >= result.ResultInfo.TotalPages {
return
}
next, err := records.listPages(ctx, zoneID, page+1)
if err != nil {
return
}
return append(list, next...), nil
}