-
Notifications
You must be signed in to change notification settings - Fork 1
/
records.go
121 lines (101 loc) · 3 KB
/
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
121
package gonjalla
import "encoding/json"
// ValidTTL is an array containing all the valid TTL values
var ValidTTL = []int{60, 300, 900, 3600, 10800, 21600, 86400}
// ValidPriority is an array containing all the valid Priority values
var ValidPriority = []int{0, 1, 5, 10, 20, 30, 40, 50, 60}
// Record struct contains data returned by `list-records`
type Record struct {
ID string `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
Content string `json:"content"`
TTL int `json:"ttl"`
Priority *int `json:"prio,omitempty"`
}
// ListRecords returns a listing of all records for a given domain
func ListRecords(token string, domain string) ([]Record, error) {
params := map[string]interface{}{
"domain": domain,
}
data, err := Request(token, "list-records", params)
if err != nil {
return nil, err
}
type Response struct {
Records []Record `json:"records"`
}
var response Response
err = json.Unmarshal(data, &response)
if err != nil {
return nil, err
}
return response.Records, nil
}
// AddRecord adds a given record to a given domain.
// Returns a new Record struct, containing the response from the API if
// successful. This response will have some fields like ID (which can only
// be known after the execution) filled.
func AddRecord(token string, domain string, record Record) (Record, error) {
marshal, err := json.Marshal(record)
if err != nil {
return Record{}, err
}
params := map[string]interface{}{
"domain": domain,
}
err = json.Unmarshal(marshal, ¶ms)
if err != nil {
return Record{}, err
}
data, err := Request(token, "add-record", params)
if err != nil {
return Record{}, err
}
var response Record
err = json.Unmarshal(data, &response)
if err != nil {
return Record{}, err
}
return response, nil
}
// RemoveRecord removes a given record from a given domain.
// If there are no errors it will return `nil`.
func RemoveRecord(token string, domain string, id string) error {
params := map[string]interface{}{
"domain": domain,
"id": id,
}
_, err := Request(token, "remove-record", params)
if err != nil {
return err
}
return nil
}
// EditRecord edits a record for a given domain.
// This function is fairly dumb. It takes in a `Record` struct, and uses all
// its filled fields to send to Njalla.
// So, if you want to only change a given field, get the `Record` object from
// say ListRecords, change the one field you want, and then pass that here.
//
// Note that the record type cannot be changed, so if you want to do so, you'll
// have to remove and create the record again under a different type. Trying to
// change the record type will just return an API error.
func EditRecord(token string, domain string, record Record) error {
marshal, err := json.Marshal(record)
if err != nil {
return err
}
params := map[string]interface{}{
"domain": domain,
}
err = json.Unmarshal(marshal, ¶ms)
if err != nil {
return err
}
_, err = Request(token, "edit-record", params)
if err != nil {
return err
}
return nil
}