forked from signalfx/signalfx-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
integration.go
81 lines (64 loc) · 2.02 KB
/
integration.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
/*
* Integrations API
*
* https://dev.splunk.com/observability/reference/api/integrations/latest
*/
package signalfx
import (
"bytes"
"context"
"encoding/json"
"io"
"io/ioutil"
"net/http"
)
// IntegrationAPIURL is the base URL for interacting with integrations.
const IntegrationAPIURL = "/v2/integration"
// GetIntegration gets an integration as map.
func (c *Client) GetIntegration(ctx context.Context, id string) (map[string]interface{}, error) {
out := make(map[string]interface{})
err := c.getIntegration(ctx, id, &out)
if err != nil {
return nil, err
}
return out, nil
}
// DeleteIntegration deletes an integration.
func (c *Client) DeleteIntegration(ctx context.Context, id string) error {
return c.doIntegrationRequest(ctx, IntegrationAPIURL+"/"+id, "DELETE", http.StatusNoContent, nil, nil)
}
func (c *Client) createIntegration(ctx context.Context, in interface{}, out interface{}) error {
return c.doIntegrationRequest(ctx, IntegrationAPIURL, "POST", http.StatusOK, in, out)
}
func (c *Client) getIntegration(ctx context.Context, id string, out interface{}) error {
return c.doIntegrationRequest(ctx, IntegrationAPIURL+"/"+id, "GET", http.StatusOK, nil, out)
}
func (c *Client) updateIntegration(ctx context.Context, id string, in interface{}, out interface{}) error {
return c.doIntegrationRequest(ctx, IntegrationAPIURL+"/"+id, "PUT", http.StatusOK, in, out)
}
func (c *Client) doIntegrationRequest(ctx context.Context, url string, method string, status int, in interface{}, out interface{}) error {
var body io.Reader
if in != nil {
payload, err := json.Marshal(in)
if err != nil {
return err
}
body = bytes.NewReader(payload)
}
resp, err := c.doRequest(ctx, method, url, nil, body)
if resp != nil {
//noinspection GoUnhandledErrorResult
defer resp.Body.Close()
}
if err != nil {
return err
}
if err = newResponseError(resp, status); err != nil {
return err
}
if out != nil {
err = json.NewDecoder(resp.Body).Decode(out)
}
_, _ = io.Copy(ioutil.Discard, resp.Body)
return err
}