forked from K-Phoen/grabana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasources.go
161 lines (126 loc) · 3.59 KB
/
datasources.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package grabana
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"github.com/K-Phoen/grabana/datasource"
)
// ErrDatasourceNotFound is returned when the given datasource can not be found.
var ErrDatasourceNotFound = errors.New("datasource not found")
const defaultDatasourceKey = "$grabana_default_datasource_key$"
// UpsertDatasource creates or replaces a datasource.
func (client *Client) UpsertDatasource(ctx context.Context, datasource datasource.Datasource) error {
buf, err := json.Marshal(datasource)
if err != nil {
return err
}
id, err := client.getDatasourceIDByName(ctx, datasource.Name())
if err != nil && err != ErrDatasourceNotFound {
return err
}
method := http.MethodPost
url := "/api/datasources"
if id != 0 {
method = http.MethodPut
url = fmt.Sprintf("/api/datasources/%d", id)
}
resp, err := client.sendJSON(ctx, method, url, buf)
if err != nil {
return err
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
return client.httpError(resp)
}
return nil
}
// DeleteDatasource deletes a datasource given its name.
func (client *Client) DeleteDatasource(ctx context.Context, name string) error {
id, err := client.getDatasourceIDByName(ctx, name)
if err != nil {
return err
}
resp, err := client.delete(ctx, fmt.Sprintf("/api/datasources/%d", id))
if err != nil {
return err
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode == http.StatusNotFound {
return ErrDatasourceNotFound
}
if resp.StatusCode != http.StatusOK {
return client.httpError(resp)
}
return nil
}
// GetDatasourceUIDByName finds a datasource UID given its name.
func (client *Client) GetDatasourceUIDByName(ctx context.Context, name string) (string, error) {
resp, err := client.get(ctx, "/api/datasources/name/"+name)
if err != nil {
return "", err
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode == http.StatusNotFound {
return "", ErrDatasourceNotFound
}
if resp.StatusCode != http.StatusOK {
return "", client.httpError(resp)
}
response := struct {
UID string `json:"uid"`
}{}
if err := decodeJSON(resp.Body, &response); err != nil {
return "", err
}
return response.UID, nil
}
// datasourcesUIDMap builds a map of datasources UIDs indexed by their name.
func (client *Client) datasourcesUIDMap(ctx context.Context) (map[string]string, error) {
resp, err := client.get(ctx, "/api/datasources")
if err != nil {
return nil, err
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
return nil, client.httpError(resp)
}
var datasources []struct {
UID string `json:"uid"`
Name string `json:"name"`
IsDefault bool `json:"isDefault"`
}
if err := decodeJSON(resp.Body, &datasources); err != nil {
return nil, err
}
datasourcesMap := make(map[string]string, len(datasources))
for _, ds := range datasources {
datasourcesMap[ds.Name] = ds.UID
if ds.IsDefault {
datasourcesMap[defaultDatasourceKey] = ds.UID
}
}
return datasourcesMap, nil
}
// getDatasourceIDByName finds a datasource, given its name.
func (client *Client) getDatasourceIDByName(ctx context.Context, name string) (int, error) {
resp, err := client.get(ctx, "/api/datasources/id/"+name)
if err != nil {
return 0, err
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode == http.StatusNotFound {
return 0, ErrDatasourceNotFound
}
if resp.StatusCode != http.StatusOK {
return 0, client.httpError(resp)
}
response := struct {
ID int `json:"id"`
}{}
if err := decodeJSON(resp.Body, &response); err != nil {
return 0, err
}
return response.ID, nil
}