-
Notifications
You must be signed in to change notification settings - Fork 52
/
detector.go
242 lines (191 loc) · 6.15 KB
/
detector.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
package signalfx
import (
"bytes"
"context"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"github.com/signalfx/signalfx-go/detector"
)
// DetectorAPIURL is the base URL for interacting with detectors.
const DetectorAPIURL = "/v2/detector"
// CreateDetector creates a detector.
func (c *Client) CreateDetector(ctx context.Context, detectorRequest *detector.CreateUpdateDetectorRequest) (*detector.Detector, error) {
payload, err := json.Marshal(detectorRequest)
if err != nil {
return nil, err
}
resp, err := c.doRequest(ctx, "POST", DetectorAPIURL, nil, bytes.NewReader(payload))
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err = newResponseError(resp, http.StatusOK); err != nil {
return nil, err
}
finalDetector := &detector.Detector{}
err = json.NewDecoder(resp.Body).Decode(finalDetector)
_, _ = io.Copy(ioutil.Discard, resp.Body)
return finalDetector, err
}
// DeleteDetector deletes a detector.
func (c *Client) DeleteDetector(ctx context.Context, id string) error {
resp, err := c.doRequest(ctx, "DELETE", DetectorAPIURL+"/"+id, nil, nil)
if err != nil {
return err
}
defer resp.Body.Close()
if err = newResponseError(resp, http.StatusNoContent); err != nil {
return err
}
_, _ = io.Copy(ioutil.Discard, resp.Body)
return nil
}
// DisableDetector disables a detector.
func (c *Client) DisableDetector(ctx context.Context, id string, labels []string) error {
payload, err := json.Marshal(labels)
if err != nil {
return err
}
resp, err := c.doRequest(ctx, "PUT", DetectorAPIURL+"/"+id+"/disable", nil, bytes.NewReader(payload))
if err != nil {
return err
}
defer resp.Body.Close()
if err = newResponseError(resp, http.StatusNoContent); err != nil {
return err
}
_, _ = io.Copy(ioutil.Discard, resp.Body)
return nil
}
// EnableDetector enables a detector.
func (c *Client) EnableDetector(ctx context.Context, id string, labels []string) error {
payload, err := json.Marshal(labels)
if err != nil {
return err
}
resp, err := c.doRequest(ctx, "PUT", DetectorAPIURL+"/"+id+"/enable", nil, bytes.NewReader(payload))
if err != nil {
return err
}
defer resp.Body.Close()
if err = newResponseError(resp, http.StatusNoContent); err != nil {
return err
}
_, _ = io.Copy(ioutil.Discard, resp.Body)
return nil
}
// GetDetector gets a detector.
func (c *Client) GetDetector(ctx context.Context, id string) (*detector.Detector, error) {
resp, err := c.doRequest(ctx, "GET", DetectorAPIURL+"/"+id, nil, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err = newResponseError(resp, http.StatusOK); err != nil {
return nil, err
}
finalDetector := &detector.Detector{}
err = json.NewDecoder(resp.Body).Decode(finalDetector)
_, _ = io.Copy(ioutil.Discard, resp.Body)
return finalDetector, err
}
// UpdateDetector updates a detector.
func (c *Client) UpdateDetector(ctx context.Context, id string, detectorRequest *detector.CreateUpdateDetectorRequest) (*detector.Detector, error) {
payload, err := json.Marshal(detectorRequest)
if err != nil {
return nil, err
}
resp, err := c.doRequest(ctx, "PUT", DetectorAPIURL+"/"+id, nil, bytes.NewReader(payload))
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err = newResponseError(resp, http.StatusOK); err != nil {
return nil, err
}
finalDetector := &detector.Detector{}
err = json.NewDecoder(resp.Body).Decode(finalDetector)
_, _ = io.Copy(ioutil.Discard, resp.Body)
return finalDetector, err
}
// SearchDetectors searches for detectors, given a query string in `name`.
func (c *Client) SearchDetectors(ctx context.Context, limit int, name string, offset int, tags string) (*detector.SearchResults, error) {
params := url.Values{}
params.Add("limit", strconv.Itoa(limit))
params.Add("name", name)
params.Add("offset", strconv.Itoa(offset))
if tags != "" {
params.Add("tags", tags)
}
resp, err := c.doRequest(ctx, "GET", DetectorAPIURL, params, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err = newResponseError(resp, http.StatusOK); err != nil {
return nil, err
}
finalDetectors := &detector.SearchResults{}
err = json.NewDecoder(resp.Body).Decode(finalDetectors)
_, _ = io.Copy(ioutil.Discard, resp.Body)
return finalDetectors, err
}
// GetDetectorEvents gets a detector's events.
func (c *Client) GetDetectorEvents(ctx context.Context, id string, from int, to int, offset int, limit int) ([]*detector.Event, error) {
params := url.Values{}
params.Add("from", strconv.Itoa(from))
params.Add("to", strconv.Itoa(to))
params.Add("offset", strconv.Itoa(offset))
params.Add("limit", strconv.Itoa(limit))
resp, err := c.doRequest(ctx, "GET", DetectorAPIURL+"/"+id+"/events", params, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err = newResponseError(resp, http.StatusOK); err != nil {
return nil, err
}
var events []*detector.Event
err = json.NewDecoder(resp.Body).Decode(&events)
_, _ = io.Copy(ioutil.Discard, resp.Body)
return events, err
}
// GetDetectorIncidents gets a detector's incidents.
func (c *Client) GetDetectorIncidents(ctx context.Context, id string, offset int, limit int) ([]*detector.Incident, error) {
params := url.Values{}
params.Add("offset", strconv.Itoa(offset))
params.Add("limit", strconv.Itoa(limit))
resp, err := c.doRequest(ctx, "GET", DetectorAPIURL+"/"+id+"/incidents", params, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err = newResponseError(resp, http.StatusOK); err != nil {
return nil, err
}
var incidents []*detector.Incident
err = json.NewDecoder(resp.Body).Decode(&incidents)
_, _ = io.Copy(ioutil.Discard, resp.Body)
return incidents, err
}
// ValidateDetector validates a detector.
func (c *Client) ValidateDetector(ctx context.Context, detectorRequest *detector.ValidateDetectorRequestModel) error {
payload, err := json.Marshal(detectorRequest)
if err != nil {
return err
}
resp, err := c.doRequest(ctx, "POST", DetectorAPIURL+"/validate", nil, bytes.NewReader(payload))
if err != nil {
return err
}
defer resp.Body.Close()
if err = newResponseError(resp, http.StatusNoContent); err != nil {
return err
}
_, _ = io.Copy(ioutil.Discard, resp.Body)
return nil
}