forked from terra-farm/udnssdk
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathalert.go
91 lines (79 loc) · 2.38 KB
/
alert.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
package udnssdk
import (
"log"
"net/http"
"time"
)
// AlertsService manages Alerts
type AlertsService struct {
client *Client
}
// ProbeAlertDataDTO wraps a probe alert response
type ProbeAlertDataDTO struct {
PoolRecord string `json:"poolRecord"`
ProbeType string `json:"probeType"`
ProbeStatus string `json:"probeStatus"`
AlertDate time.Time `json:"alertDate"`
FailoverOccured bool `json:"failoverOccured"`
OwnerName string `json:"ownerName"`
Status string `json:"status"`
}
// Equal compares to another ProbeAlertDataDTO, but uses time.Equals to compare semantic equvalance of AlertDate
func (a ProbeAlertDataDTO) Equal(b ProbeAlertDataDTO) bool {
return a.PoolRecord == b.PoolRecord &&
a.ProbeType == b.ProbeType &&
a.ProbeStatus == b.ProbeStatus &&
a.AlertDate.Equal(b.AlertDate) &&
a.FailoverOccured == b.FailoverOccured &&
a.OwnerName == b.OwnerName &&
a.Status == b.Status
}
// ProbeAlertDataListDTO wraps the response for an index of probe alerts
type ProbeAlertDataListDTO struct {
Alerts []ProbeAlertDataDTO `json:"alerts"`
Queryinfo QueryInfo `json:"queryInfo"`
Resultinfo ResultInfo `json:"resultInfo"`
}
// Select returns all probe alerts with a RRSetKey
func (s *AlertsService) Select(k RRSetKey) ([]ProbeAlertDataDTO, error) {
// TODO: Sane Configuration for timeouts / retries
maxerrs := 5
waittime := 5 * time.Second
// init accumulators
as := []ProbeAlertDataDTO{}
offset := 0
errcnt := 0
for {
reqAlerts, 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 as, err
}
log.Printf("ResultInfo: %+v\n", ri)
for _, a := range reqAlerts {
as = append(as, a)
}
if ri.ReturnedCount+ri.Offset >= ri.TotalCount {
return as, nil
}
offset = ri.ReturnedCount + ri.Offset
continue
}
}
// SelectWithOffset returns the probe alerts with a RRSetKey, accepting an offset
func (s *AlertsService) SelectWithOffset(k RRSetKey, offset int) ([]ProbeAlertDataDTO, ResultInfo, *http.Response, error) {
var ald ProbeAlertDataListDTO
uri := k.AlertsQueryURI(offset)
res, err := s.client.get(uri, &ald)
as := []ProbeAlertDataDTO{}
for _, a := range ald.Alerts {
as = append(as, a)
}
return as, ald.Resultinfo, res, err
}