forked from grafana-tools/sdk
-
Notifications
You must be signed in to change notification settings - Fork 2
/
rest-snapshot.go
34 lines (30 loc) · 847 Bytes
/
rest-snapshot.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
package sdk
import (
"context"
"encoding/json"
"fmt"
"github.com/pkg/errors"
)
// https://grafana.com/docs/grafana/latest/http_api/snapshot/
// CreateAnnotation creates a new snapshot.
func (r *Client) CreateSnapshot(ctx context.Context, a CreateSnapshotRequest) (StatusMessage, error) {
var (
raw []byte
resp StatusMessage
err error
code int
)
if raw, err = json.Marshal(a); err != nil {
return StatusMessage{}, errors.Wrap(err, "marshal request")
}
if raw, code, err = r.post(ctx, "api/snapshots", nil, raw); err != nil {
return StatusMessage{}, errors.Wrap(err, "create snapshot")
}
if code/100 != 2 {
return StatusMessage{}, fmt.Errorf("bad response: %d", code)
}
if err = json.Unmarshal(raw, &resp); err != nil {
return StatusMessage{}, errors.Wrap(err, "unmarshal response message")
}
return resp, nil
}