-
Notifications
You must be signed in to change notification settings - Fork 51
/
incident.go
62 lines (51 loc) · 1.53 KB
/
incident.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
package signalfx
import (
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"github.com/signalfx/signalfx-go/detector"
)
// IncidentAPIURL is the base URL for interacting with alert muting rules.
const IncidentAPIURL = "/v2/incident"
// Get incident with the given id
func (c *Client) GetIncident(ctx context.Context, id string) (*detector.Incident, error) {
resp, err := c.doRequest(ctx, "GET", IncidentAPIURL+"/"+id, nil, nil)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
message, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("Bad status %d: %s", resp.StatusCode, message)
}
finalIncident := &detector.Incident{}
err = json.NewDecoder(resp.Body).Decode(finalIncident)
_, _ = io.Copy(ioutil.Discard, resp.Body)
return finalIncident, err
}
// Get all incidents
func (c *Client) GetIncidents(ctx context.Context, includeResolved bool, limit int, query string, offset int) ([]*detector.Incident, error) {
params := url.Values{}
params.Add("includeResolved", strconv.FormatBool(includeResolved))
params.Add("limit", strconv.Itoa(limit))
params.Add("query", query)
params.Add("offset", strconv.Itoa(offset))
resp, err := c.doRequest(ctx, "GET", IncidentAPIURL, params, nil)
if resp != nil {
defer resp.Body.Close()
}
if 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
}