This repository has been archived by the owner on Jan 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 97
/
folder.go
130 lines (110 loc) · 2.87 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package gapi
import (
"encoding/json"
"fmt"
"net/url"
)
// Folder represents a Grafana folder.
type Folder struct {
ID int64 `json:"id"`
UID string `json:"uid"`
Title string `json:"title"`
URL string `json:"url"`
}
type FolderPayload struct {
Title string `json:"title"`
UID string `json:"uid,omitempty"`
Overwrite bool `json:"overwrite,omitempty"`
}
// Folders fetches and returns Grafana folders.
func (c *Client) Folders() ([]Folder, error) {
const limit = 1000
var (
page = 0
newFolders []Folder
folders []Folder
query = make(url.Values)
)
query.Set("limit", fmt.Sprint(limit))
for {
page++
query.Set("page", fmt.Sprint(page))
if err := c.request("GET", "/api/folders/", query, nil, &newFolders); err != nil {
return nil, err
}
folders = append(folders, newFolders...)
if len(newFolders) < limit {
return folders, nil
}
}
}
// 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
}
// Folder fetches and returns the Grafana folder whose UID it's passed.
func (c *Client) FolderByUID(uid string) (*Folder, error) {
folder := &Folder{}
err := c.request("GET", fmt.Sprintf("/api/folders/%s", uid), nil, nil, folder)
if err != nil {
return folder, err
}
return folder, err
}
// NewFolder creates a new Grafana folder.
func (c *Client) NewFolder(title string, uid ...string) (Folder, error) {
if len(uid) > 1 {
return Folder{}, fmt.Errorf("too many arguments. Expected 1 or 2")
}
folder := Folder{}
payload := FolderPayload{
Title: title,
}
if len(uid) == 1 {
payload.UID = uid[0]
}
data, err := json.Marshal(payload)
if err != nil {
return folder, err
}
err = c.request("POST", "/api/folders", nil, data, &folder)
if err != nil {
return folder, err
}
return folder, err
}
// UpdateFolder updates the folder whose UID it's passed.
func (c *Client) UpdateFolder(uid string, title string, newUID ...string) error {
payload := FolderPayload{
Title: title,
Overwrite: true,
}
if len(newUID) == 1 {
payload.UID = newUID[0]
}
data, err := json.Marshal(payload)
if err != nil {
return err
}
return c.request("PUT", fmt.Sprintf("/api/folders/%s", uid), nil, data, nil)
}
func ForceDeleteFolderRules() url.Values {
query := make(url.Values)
query.Set("forceDeleteRules", "true")
return query
}
// DeleteFolder deletes the folder whose ID it's passed.
func (c *Client) DeleteFolder(id string, optionalQueryParams ...url.Values) error {
query := url.Values{}
for _, param := range optionalQueryParams {
for paramKey := range param {
query.Set(paramKey, param.Get(paramKey))
}
}
return c.request("DELETE", fmt.Sprintf("/api/folders/%s", id), query, nil, nil)
}