-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgostellix.go
285 lines (257 loc) · 9.66 KB
/
gostellix.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package gostellix
import (
"bytes"
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"strconv"
"time"
)
const defaultAPIURL string = "https://api.dns.constellix.com/"
const authHeaderName string = "x-cns-security-token"
const defaultUserAgent string = "gostellix/0.1"
// ConstellixSoa holds SOA records
type ConstellixSoa struct {
PrimaryNameserver string `json:"primaryNameserver,omitempty"`
Email string `json:"email,omitempty"`
TTL int `json:"ttl,omitempty"`
Serial int `json:"serial,omitempty"`
Refresh int `json:"refresh,omitempty"`
Retry int `json:"retry,omitempty"`
Expire int `json:"expire,omitempty"`
NegCache int `json:"negCache,omitempty"`
}
// ConstellixDomain holds a domain
type ConstellixDomain struct {
ID int `json:"id,omitempty"`
Name string `json:"name"`
TypeID int `json:"typeId,omitempty"`
HasGtdRegions bool `json:"hasGtdRegions,omitempty"`
HasGeoIP bool `json:"hasGeoIP,omitempty"`
NameserverGroup int `json:"nameserverGroup,omitempty"`
Nameservers []string `json:"nameservers,omitempty"`
CreatedTs string `json:"createdTs,omitempty"`
ModifiedTs string `json:"modifiedTs,omitempty"`
Note string `json:"note,omitempty"`
Version int `json:"version,omitempty"`
Status string `json:"status,omitempty"`
Tags []string `json:"tags,omitempty"`
Soa *ConstellixSoa `json:"soa,omitempty"`
}
// RoundRobinObj holds RoundRobin records from a domain
type RoundRobinObj struct {
Value string `json:"value"`
Level string `json:"level,omitifempty"` // for MX records
DisableFlag bool `json:"disableFlag"`
}
// GeolocationObj geolocation info
type GeolocationObj struct {
GeoipUserRegion []int `json:"geoipUserRegion,omitempty"`
Drop bool `json:"drop,omitifempty"`
}
// RecordFailoverValues Failover values
type RecordFailoverValues struct {
Value string `json:"value"`
CheckID string `json:"checkId"`
DisableFlag string `json:"disableFlag"`
}
// RecordFailoverObj Failover info
type RecordFailoverObj struct {
FailoverType int `json:"failoverType"`
Values []RecordFailoverValues `json:"values"`
DisableFlag bool `json:"disableFlag"`
}
// RoundRobinFailoverObjValues RR Failover values
type RoundRobinFailoverObjValues struct {
Value string `json:"value"`
DisableFlag bool `json:"disableFlag"`
CheckID int `json:"checkId"`
}
// RoundRobinFailoverObj RR Failover Info
type RoundRobinFailoverObj struct {
Values []RoundRobinFailoverObjValues `json:"values"`
DisableFlag bool `json:"disableFlag"`
}
// ConstellixRecord domain record
type ConstellixRecord struct {
ID int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
TTL int `json:"ttl,omitempty"`
Geolocation *GeolocationObj `json:"geolocation,omitempty"`
RecordOption string `json:"recordOption,omitempty"`
NoAnswer bool `json:"noAnswer,omitempty"`
Note string `json:"note,omitempty"`
GtdRegion int `json:"gtdRegion,omitempty"`
Type string `json:"type,omitempty"`
ParentID int `json:"parentId,omitempty"`
Parent string `json:"parent,omitempty"`
Source string `json:"source,omitempty"`
ContactIDs []int `json:"contactIds,omitempty"`
RoundRobin []RoundRobinObj `json:"roundRobin"`
RecordFailover *RecordFailoverObj `json:"recordFailover,omitifempty"`
Pools []int `json:"pools,omitempty"`
RoundRobinFailover *RoundRobinFailoverObj `json:"roundRobinFailover"`
ModifiedTs int `json:"modifiedTs,omitempty"`
Value []string `json:"value,omitempty"`
}
// Client Client for the Constellix API
type Client struct {
APIURL string
Token string
UserAgent string
HTTPClient *http.Client
}
func buildSecurityToken(apikey, secretkey string) string {
millis := time.Now().UnixNano() / 1000000
timestamp := strconv.FormatInt(millis, 10)
mac := hmac.New(sha1.New, []byte(secretkey))
mac.Write([]byte(timestamp))
hmacstr := base64.StdEncoding.EncodeToString(mac.Sum(nil))
return apikey + ":" + hmacstr + ":" + timestamp
}
// New Create a new client
func New(apikey, secretkey string) *Client {
return &Client{
APIURL: defaultAPIURL,
Token: buildSecurityToken(apikey, secretkey),
HTTPClient: http.DefaultClient,
UserAgent: defaultUserAgent,
}
}
// GetAllDomains get all domains on the acct
func (client *Client) GetAllDomains() ([]ConstellixDomain, error) {
var domains []ConstellixDomain
body, _ := client.APIRequest("v1/domains", "", "GET")
json.Unmarshal(body, &domains)
return domains, nil
}
// GetDomainByName Get a single domain, by name
func (client *Client) GetDomainByName(name string) (ConstellixDomain, error) {
allDomains, err := client.GetAllDomains()
if err != nil {
return ConstellixDomain{}, err
}
for _, domain := range allDomains {
if domain.Name == name {
return domain, nil
}
}
return ConstellixDomain{}, errors.New("No such domain")
}
// ListDomains get a bare list of all domain names
func (client *Client) ListDomains() ([]string, error) {
var domainList []string
var err error
domainObjects, err := client.GetAllDomains()
for _, domain := range domainObjects {
domainList = append(domainList, domain.Name)
}
return domainList, err
}
// CreateDomains Create a list of domains by name
func (client *Client) CreateDomains(domains []string) error {
nameObj := struct {
Names []string `json:"names"`
}{domains}
res, err := json.Marshal(nameObj)
if err != nil {
return err
}
jsonStr := string(res)
_, err = client.APIRequest("v1/domains/", jsonStr, "POST")
return err
}
// DeleteDomain delete domain, by integer id
func (client *Client) DeleteDomain(id int) error {
_, err := client.APIRequest("v1/domains/"+strconv.Itoa(id), "", "DELETE")
return err
}
// ModifyDomain replace existing Domain
func (client *Client) ModifyDomain(domain ConstellixDomain) error {
res, err := json.Marshal(domain)
jsonStr := string(res)
_, err = client.APIRequest("v1/domains/"+strconv.Itoa(domain.ID), jsonStr, "PUT")
return err
}
// GetRecordsByDomainName get all records for a domain, by name
func (client *Client) GetRecordsByDomainName(domainName string) ([]ConstellixRecord, error) {
allDomains, err := client.GetAllDomains()
if err != nil {
return []ConstellixRecord{}, nil
}
for _, domain := range allDomains {
if domain.Name == domainName {
return client.GetDomainRecords(domain.ID)
}
}
return []ConstellixRecord{}, nil
}
// GetDomainRecords get all records for one domain by domain ID
func (client *Client) GetDomainRecords(domainid int) ([]ConstellixRecord, error) {
var records []ConstellixRecord
body, err := client.APIRequest("v1/domains/"+strconv.Itoa(domainid)+"/records", "", "GET")
//TODO: go through paging
//TODO: check error
if err != nil {
return []ConstellixRecord{}, err
}
err = json.Unmarshal(body, &records)
return records, err
}
// GetDomainRecord Get one record from a domain, by ID
func (client *Client) GetDomainRecord(domainID int, recordType string, recordID int) (ConstellixRecord, error) {
var domain ConstellixRecord
body, err := client.APIRequest("v1/domains/"+strconv.Itoa(domainID)+"/records/"+recordType+"/"+strconv.Itoa(recordID), "", "GET")
json.Unmarshal(body, &domain)
return domain, err
}
// DeleteDomainRecord delete one domain record
func (client *Client) DeleteDomainRecord(domainID int, recordType string, recordID int) error {
_, err := client.APIRequest("v1/domains/"+strconv.Itoa(domainID)+"/records/"+recordType+"/"+strconv.Itoa(recordID), "", "DELETE")
return err
}
// CreateDomainRecord add a record to a domain
func (client *Client) CreateDomainRecord(domainID int, record ConstellixRecord) error {
recordJSON, _ := json.Marshal(record)
_, err := client.APIRequest("v1/domains/"+strconv.Itoa(domainID)+"/records/"+record.Type, string(recordJSON), "POST")
return err
}
// ModifyDomainRecord add a record to a domain
func (client *Client) ModifyDomainRecord(domainID int, record ConstellixRecord) error {
recordJSON, _ := json.Marshal(record)
_, err := client.APIRequest("v1/domains/"+strconv.Itoa(domainID)+"/records/"+record.Type, string(recordJSON), "PUT")
return err
}
// APIRequest make an API request
func (client *Client) APIRequest(endpoint, params, reqtype string) (response []byte, err error) {
requrl := client.APIURL + endpoint
var req *http.Request
if reqtype == "PUT" || reqtype == "POST" {
req, err = http.NewRequest(reqtype, requrl, bytes.NewBuffer([]byte(params)))
} else if reqtype == "GET" || reqtype == "DELETE" {
if params != "" {
requrl += "?" + params
}
req, err = http.NewRequest(reqtype, requrl, nil)
} else {
// unknown request type
return nil, errors.New("Unknown request type: " + reqtype)
}
req.Header.Add(authHeaderName, client.Token)
req.Header.Add("User-Agent", client.UserAgent)
req.Header.Add("Content-type", "application/json")
resp, err := client.HTTPClient.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, errors.New(resp.Status)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
return body, err
}