-
Notifications
You must be signed in to change notification settings - Fork 11
/
rrset.go
401 lines (348 loc) · 12 KB
/
rrset.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
package udnssdk
import (
"fmt"
"log"
"net/http"
"time"
"github.com/fatih/structs"
"github.com/mitchellh/mapstructure"
)
// RRSetsService provides access to RRSet resources
type RRSetsService struct {
client *Client
}
// Here is the big 'Profile' mess that should get refactored to a more managable place
// ProfileSchema are the schema URIs for RRSet Profiles
type ProfileSchema string
const (
// DirPoolSchema is the schema URI for a Directional pool profile
DirPoolSchema ProfileSchema = "http://schemas.ultradns.com/DirPool.jsonschema"
// RDPoolSchema is the schema URI for a Resource Distribution pool profile
RDPoolSchema = "http://schemas.ultradns.com/RDPool.jsonschema"
// SBPoolSchema is the schema URI for a SiteBacker pool profile
SBPoolSchema = "http://schemas.ultradns.com/SBPool.jsonschema"
// TCPoolSchema is the schema URI for a Traffic Controller pool profile
TCPoolSchema = "http://schemas.ultradns.com/TCPool.jsonschema"
)
// RawProfile represents the naive interface to an RRSet Profile
type RawProfile map[string]interface{}
// Context extracts the schema context from a RawProfile
func (rp RawProfile) Context() ProfileSchema {
return ProfileSchema(rp["@context"].(string))
}
// GetProfileObject extracts the full Profile by its schema type
func (rp RawProfile) GetProfileObject() (interface{}, error) {
c := rp.Context()
switch c {
case DirPoolSchema:
return rp.DirPoolProfile()
case RDPoolSchema:
return rp.RDPoolProfile()
case SBPoolSchema:
return rp.SBPoolProfile()
case TCPoolSchema:
return rp.TCPoolProfile()
default:
return nil, fmt.Errorf("fallthrough on GetProfileObject type %s", c)
}
}
// decode takes a RawProfile and uses reflection to convert it into the
// given Go native structure. val must be a pointer to a struct.
// This is identical to mapstructure.Decode, but uses the `json:` tag instead of `mapstructure:`
func decodeProfile(m interface{}, rawVal interface{}) error {
config := &mapstructure.DecoderConfig{
Metadata: nil,
TagName: "json",
Result: rawVal,
ErrorUnused: true,
WeaklyTypedInput: true,
}
decoder, err := mapstructure.NewDecoder(config)
if err != nil {
return err
}
return decoder.Decode(m)
}
// DirPoolProfile extracts the full Profile as a DirPoolProfile or returns an error
func (rp RawProfile) DirPoolProfile() (DirPoolProfile, error) {
var result DirPoolProfile
c := rp.Context()
if c != DirPoolSchema {
return result, fmt.Errorf("incorrect JSON-LD @context for DirPoolProfile %s", c)
}
err := decodeProfile(rp, &result)
return result, err
}
// RDPoolProfile extracts the full Profile as a RDPoolProfile or returns an error
func (rp RawProfile) RDPoolProfile() (RDPoolProfile, error) {
var result RDPoolProfile
c := rp.Context()
if c != RDPoolSchema {
return result, fmt.Errorf("incorrect JSON-LD @context for RDPoolProfile %s", c)
}
err := decodeProfile(rp, &result)
return result, err
}
// SBPoolProfile extracts the full Profile as a SBPoolProfile or returns an error
func (rp RawProfile) SBPoolProfile() (SBPoolProfile, error) {
var result SBPoolProfile
c := rp.Context()
if c != SBPoolSchema {
return result, fmt.Errorf("incorrect JSON-LD @context for SBPoolProfile %s", c)
}
err := decodeProfile(rp, &result)
return result, err
}
// TCPoolProfile extracts the full Profile as a TCPoolProfile or returns an error
func (rp RawProfile) TCPoolProfile() (TCPoolProfile, error) {
var result TCPoolProfile
c := rp.Context()
if c != TCPoolSchema {
return result, fmt.Errorf("incorrect JSON-LD @context for TCPoolProfile %s", c)
}
err := decodeProfile(rp, &result)
return result, err
}
// encodeProfile takes a struct and converts to a RawProfile
func encodeProfile(rawVal interface{}) RawProfile {
s := structs.New(rawVal)
s.TagName = "json"
return s.Map()
}
// RawProfile converts to a naive RawProfile
func (p DirPoolProfile) RawProfile() RawProfile {
return encodeProfile(p)
}
// RawProfile converts to a naive RawProfile
func (p RDPoolProfile) RawProfile() RawProfile {
return encodeProfile(p)
}
// RawProfile converts to a naive RawProfile
func (p SBPoolProfile) RawProfile() RawProfile {
return encodeProfile(p)
}
// RawProfile converts to a naive RawProfile
func (p TCPoolProfile) RawProfile() RawProfile {
return encodeProfile(p)
}
// DirPoolProfile wraps a Profile for a Directional Pool
type DirPoolProfile struct {
Context ProfileSchema `json:"@context"`
Description string `json:"description"`
ConflictResolve string `json:"conflictResolve,omitempty"`
RDataInfo []DPRDataInfo `json:"rdataInfo"`
NoResponse DPRDataInfo `json:"noResponse,omitempty"`
}
// DPRDataInfo wraps the rdataInfo object of a DirPoolProfile response
type DPRDataInfo struct {
AllNonConfigured bool `json:"allNonConfigured,omitempty" terraform:"all_non_configured"`
IPInfo *IPInfo `json:"ipInfo,omitempty" terraform:"ip_info"`
GeoInfo *GeoInfo `json:"geoInfo,omitempty" terraform:"geo_info"`
Type string `json:"type,omitempty" terraform:"type"` // not mentioned in REST API doc
}
// IPInfo wraps the ipInfo object of a DPRDataInfo
type IPInfo struct {
Name string `json:"name" terraform:"name"`
IsAccountLevel bool `json:"isAccountLevel,omitempty" terraform:"is_account_level"`
Ips []IPAddrDTO `json:"ips,omitempty" terraform:"-"`
}
// GeoInfo wraps the geoInfo object of a DPRDataInfo
type GeoInfo struct {
Name string `json:"name" terraform:"name"`
IsAccountLevel bool `json:"isAccountLevel,omitempty" terraform:"is_account_level"`
Codes []string `json:"codes,omitempty" terraform:"-"`
}
// RDPoolProfile wraps a Profile for a Resource Distribution pool
type RDPoolProfile struct {
Context ProfileSchema `json:"@context"`
Order string `json:"order"`
Description string `json:"description"`
}
// SBPoolProfile wraps a Profile for a SiteBacker pool
type SBPoolProfile struct {
Context ProfileSchema `json:"@context"`
Description string `json:"description"`
RunProbes bool `json:"runProbes"`
ActOnProbes bool `json:"actOnProbes"`
Order string `json:"order,omitempty"`
MaxActive int `json:"maxActive,omitempty"`
MaxServed int `json:"maxServed,omitempty"`
RDataInfo []SBRDataInfo `json:"rdataInfo"`
BackupRecords []BackupRecord `json:"backupRecords"`
}
// SBRDataInfo wraps the rdataInfo object of a SBPoolProfile
type SBRDataInfo struct {
State string `json:"state"`
RunProbes bool `json:"runProbes"`
Priority int `json:"priority"`
FailoverDelay int `json:"failoverDelay,omitempty"`
Threshold int `json:"threshold"`
Weight int `json:"weight"`
AvailableToServe bool `json:"availableToServe,omitempty"`
}
// BackupRecord wraps the backupRecord objects of an SBPoolProfile response
type BackupRecord struct {
RData string `json:"rdata,omitempty"`
FailoverDelay int `json:"failoverDelay,omitempty"`
AvailableToServe bool `json:"availableToServe,omitempty"`
}
// TCPoolProfile wraps a Profile for a Traffic Controller pool
type TCPoolProfile struct {
Context ProfileSchema `json:"@context"`
Description string `json:"description"`
RunProbes bool `json:"runProbes"`
ActOnProbes bool `json:"actOnProbes"`
MaxToLB int `json:"maxToLB,omitempty"`
RDataInfo []SBRDataInfo `json:"rdataInfo"`
BackupRecord *BackupRecord `json:"backupRecord,omitempty"`
Status string `json:"status,omitempty"`
}
// RRSet wraps an RRSet resource
type RRSet struct {
OwnerName string `json:"ownerName"`
RRType string `json:"rrtype"`
TTL int `json:"ttl"`
RData []string `json:"rdata"`
Profile RawProfile `json:"profile,omitempty"`
}
// RRSetListDTO wraps a list of RRSet resources
type RRSetListDTO struct {
ZoneName string `json:"zoneName"`
Rrsets []RRSet `json:"rrsets"`
Queryinfo QueryInfo `json:"queryInfo"`
Resultinfo ResultInfo `json:"resultInfo"`
}
// RRSetKey collects the identifiers of a Zone
type RRSetKey struct {
Zone string
Type string
Name string
}
// URI generates the URI for an RRSet
func (k RRSetKey) URI() string {
uri := fmt.Sprintf("zones/%s/rrsets", k.Zone)
if k.Type != "" {
uri += fmt.Sprintf("/%v", k.Type)
if k.Name != "" {
uri += fmt.Sprintf("/%v", k.Name)
}
}
return uri
}
// QueryURI generates the query URI for an RRSet and offset
func (k RRSetKey) QueryURI(offset int) string {
// TODO: find a more appropriate place to set "" to "ANY"
if k.Type == "" {
k.Type = "ANY"
}
return fmt.Sprintf("%s?offset=%d", k.URI(), offset)
}
// AlertsURI generates the URI for an RRSet
func (k RRSetKey) AlertsURI() string {
return fmt.Sprintf("%s/alerts", k.URI())
}
// AlertsQueryURI generates the alerts query URI for an RRSet with query
func (k RRSetKey) AlertsQueryURI(offset int) string {
uri := k.AlertsURI()
if offset != 0 {
uri = fmt.Sprintf("%s?offset=%d", uri, offset)
}
return uri
}
// EventsURI generates the URI for an RRSet
func (k RRSetKey) EventsURI() string {
return fmt.Sprintf("%s/events", k.URI())
}
// EventsQueryURI generates the events query URI for an RRSet with query
func (k RRSetKey) EventsQueryURI(query string, offset int) string {
uri := k.EventsURI()
if query != "" {
return fmt.Sprintf("%s?sort=NAME&query=%s&offset=%d", uri, query, offset)
}
if offset != 0 {
return fmt.Sprintf("%s?offset=%d", uri, offset)
}
return uri
}
// NotificationsURI generates the notifications URI for an RRSet
func (k RRSetKey) NotificationsURI() string {
return fmt.Sprintf("%s/notifications", k.URI())
}
// NotificationsQueryURI generates the notifications query URI for an RRSet with query
func (k RRSetKey) NotificationsQueryURI(query string, offset int) string {
uri := k.NotificationsURI()
if query != "" {
uri = fmt.Sprintf("%s?sort=NAME&query=%s&offset=%d", uri, query, offset)
} else {
uri = fmt.Sprintf("%s?offset=%d", uri, offset)
}
return uri
}
// ProbesURI generates the probes URI for an RRSet
func (k RRSetKey) ProbesURI() string {
return fmt.Sprintf("%s/probes", k.URI())
}
// ProbesQueryURI generates the probes query URI for an RRSet with query
func (k RRSetKey) ProbesQueryURI(query string) string {
uri := k.ProbesURI()
if query != "" {
uri = fmt.Sprintf("%s?sort=NAME&query=%s", uri, query)
}
return uri
}
// Select will list the zone rrsets, paginating through all available results
func (s *RRSetsService) Select(k RRSetKey) ([]RRSet, error) {
// TODO: Sane Configuration for timeouts / retries
maxerrs := 5
waittime := 5 * time.Second
rrsets := []RRSet{}
errcnt := 0
offset := 0
for {
reqRrsets, ri, res, err := s.SelectWithOffset(k, offset)
if err != nil {
if res != nil && res.StatusCode >= 500 {
errcnt = errcnt + 1
if errcnt < maxerrs {
time.Sleep(waittime)
continue
}
}
return rrsets, err
}
log.Printf("ResultInfo: %+v\n", ri)
for _, rrset := range reqRrsets {
rrsets = append(rrsets, rrset)
}
if ri.ReturnedCount+ri.Offset >= ri.TotalCount {
return rrsets, nil
}
offset = ri.ReturnedCount + ri.Offset
continue
}
}
// SelectWithOffset requests zone rrsets by RRSetKey & optional offset
func (s *RRSetsService) SelectWithOffset(k RRSetKey, offset int) ([]RRSet, ResultInfo, *http.Response, error) {
var rrsld RRSetListDTO
uri := k.QueryURI(offset)
res, err := s.client.get(uri, &rrsld)
rrsets := []RRSet{}
for _, rrset := range rrsld.Rrsets {
rrsets = append(rrsets, rrset)
}
return rrsets, rrsld.Resultinfo, res, err
}
// Create creates an rrset with val
func (s *RRSetsService) Create(k RRSetKey, rrset RRSet) (*http.Response, error) {
var ignored interface{}
return s.client.post(k.URI(), rrset, &ignored)
}
// Update updates a RRSet with the provided val
func (s *RRSetsService) Update(k RRSetKey, val RRSet) (*http.Response, error) {
var ignored interface{}
return s.client.put(k.URI(), val, &ignored)
}
// Delete deletes an RRSet
func (s *RRSetsService) Delete(k RRSetKey) (*http.Response, error) {
return s.client.delete(k.URI(), nil)
}