forked from signalfx/signalfx-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slo.go
68 lines (53 loc) · 1.88 KB
/
slo.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
package signalfx
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"github.com/signalfx/signalfx-go/slo"
)
const SloAPIURL = "/v2/slo"
func (c *Client) GetSlo(ctx context.Context, id string) (*slo.SloObject, error) {
return c.executeSloRequest(ctx, SloAPIURL+"/"+id, http.MethodGet, http.StatusOK, nil)
}
func (c *Client) CreateSlo(ctx context.Context, sloRequest *slo.SloObject) (*slo.SloObject, error) {
return c.executeSloRequest(ctx, SloAPIURL, http.MethodPost, http.StatusOK, sloRequest)
}
func (c *Client) ValidateSlo(ctx context.Context, sloRequest *slo.SloObject) error {
_, err := c.executeSloRequest(ctx, SloAPIURL+"/validate", http.MethodPost, http.StatusNoContent, sloRequest)
return err
}
func (c *Client) UpdateSlo(ctx context.Context, id string, sloRequest *slo.SloObject) (*slo.SloObject, error) {
return c.executeSloRequest(ctx, SloAPIURL+"/"+id, http.MethodPut, http.StatusOK, sloRequest)
}
func (c *Client) DeleteSlo(ctx context.Context, id string) error {
_, err := c.executeSloRequest(ctx, SloAPIURL+"/"+id, http.MethodDelete, http.StatusNoContent, nil)
return err
}
func (c *Client) executeSloRequest(ctx context.Context, url string, method string, expectedValidStatus int, sloRequest *slo.SloObject) (*slo.SloObject, error) {
var body io.Reader
if sloRequest != nil {
payload, err := json.Marshal(sloRequest)
if err != nil {
return nil, err
}
body = bytes.NewReader(payload)
}
resp, err := c.doRequest(ctx, method, url, nil, body)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err = newResponseError(resp, expectedValidStatus); err != nil {
return nil, err
}
if expectedValidStatus == http.StatusNoContent {
_, _ = io.Copy(io.Discard, resp.Body)
return nil, nil
}
returnedSlo := &slo.SloObject{}
err = json.NewDecoder(resp.Body).Decode(returnedSlo)
_, _ = io.Copy(io.Discard, resp.Body)
return returnedSlo, err
}