This repository has been archived by the owner on Mar 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
folder.go
73 lines (61 loc) · 1.63 KB
/
folder.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
package gapi
import (
"bytes"
"encoding/json"
"fmt"
)
// Folder represents a Grafana folder.
type Folder struct {
Id int64 `json:"id"`
Uid string `json:"uid"`
Title string `json:"title"`
}
// Folders fetches and returns Grafana folders.
func (c *Client) Folders() ([]Folder, error) {
folders := make([]Folder, 0)
err := c.request("GET", "/api/folders/", nil, nil, &folders)
if err != nil {
return folders, err
}
return folders, err
}
// Folder fetches and returns the Grafana folder whose ID it's passed.
func (c *Client) Folder(id int64) (*Folder, error) {
folder := &Folder{}
err := c.request("GET", fmt.Sprintf("/api/folders/id/%d", id), nil, nil, folder)
if err != nil {
return folder, err
}
return folder, err
}
// NewFolder creates a new Grafana folder.
func (c *Client) NewFolder(title string) (Folder, error) {
folder := Folder{}
dataMap := map[string]string{
"title": title,
}
data, err := json.Marshal(dataMap)
if err != nil {
return folder, err
}
err = c.request("POST", "/api/folders", nil, bytes.NewBuffer(data), &folder)
if err != nil {
return folder, err
}
return folder, err
}
// UpdateFolder updates the folder whose ID it's passed.
func (c *Client) UpdateFolder(id string, name string) error {
dataMap := map[string]string{
"name": name,
}
data, err := json.Marshal(dataMap)
if err != nil {
return err
}
return c.request("PUT", fmt.Sprintf("/api/folders/%s", id), nil, bytes.NewBuffer(data), nil)
}
// DeleteFolder deletes the folder whose ID it's passed.
func (c *Client) DeleteFolder(id string) error {
return c.request("DELETE", fmt.Sprintf("/api/folders/%s", id), nil, nil, nil)
}