-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
folders.go
97 lines (80 loc) · 2.35 KB
/
folders.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
package grabana
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strings"
)
// ErrFolderNotFound is returned when the given folder can not be found.
var ErrFolderNotFound = errors.New("folder not found")
// Folder represents a dashboard folder.
// See https://grafana.com/docs/grafana/latest/reference/dashboard_folders/
type Folder struct {
ID uint `json:"id"`
UID string `json:"uid"`
ParentUID string `json:"parentUid"`
Title string `json:"title"`
}
// FindOrCreateFolder returns the folder by its name or creates it if it doesn't exist.
func (client *Client) FindOrCreateFolder(ctx context.Context, name string) (*Folder, error) {
folder, err := client.GetFolderByTitle(ctx, name)
if err != nil && !errors.Is(err, ErrFolderNotFound) {
return nil, fmt.Errorf("could not find or create folder: %w", err)
}
if folder == nil {
folder, err = client.CreateFolder(ctx, name)
if err != nil {
return nil, fmt.Errorf("could not find create folder: %w", err)
}
}
return folder, nil
}
// CreateFolder creates a dashboard folder.
// See https://grafana.com/docs/grafana/latest/reference/dashboard_folders/
func (client *Client) CreateFolder(ctx context.Context, name string) (*Folder, error) {
buf, err := json.Marshal(struct {
Title string `json:"title"`
}{
Title: name,
})
if err != nil {
return nil, err
}
resp, err := client.sendJSON(ctx, http.MethodPost, "/api/folders", buf)
if err != nil {
return nil, err
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
return nil, client.httpError(resp)
}
var folder Folder
if err := decodeJSON(resp.Body, &folder); err != nil {
return nil, err
}
return &folder, nil
}
// GetFolderByTitle finds a folder, given its title.
func (client *Client) GetFolderByTitle(ctx context.Context, title string) (*Folder, error) {
resp, err := client.get(ctx, fmt.Sprintf("/api/search?type=dash-folder&query=%s", url.QueryEscape(title)))
if err != nil {
return nil, err
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
return nil, client.httpError(resp)
}
var folders []Folder
if err := decodeJSON(resp.Body, &folders); err != nil {
return nil, err
}
for i := range folders {
if strings.EqualFold(folders[i].Title, title) {
return &folders[i], nil
}
}
return nil, ErrFolderNotFound
}