forked from grafana/grafana-api-golang-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasource.go
359 lines (298 loc) · 11.4 KB
/
datasource.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package gapi
import (
"bytes"
"encoding/json"
"fmt"
"regexp"
"strconv"
)
var headerNameRegex = regexp.MustCompile(`^httpHeaderName(\d+)$`)
// DataSource represents a Grafana data source.
type DataSource struct {
ID int64 `json:"id,omitempty"`
UID string `json:"uid,omitempty"`
Name string `json:"name"`
Type string `json:"type"`
URL string `json:"url"`
Access string `json:"access"`
// This is only returned by the API. It can only be set through the `editable` attribute of provisioned data sources.
ReadOnly bool `json:"readOnly"`
Database string `json:"database,omitempty"`
User string `json:"user,omitempty"`
// Deprecated: Use secureJsonData.password instead.
Password string `json:"password,omitempty"`
OrgID int64 `json:"orgId,omitempty"`
IsDefault bool `json:"isDefault"`
BasicAuth bool `json:"basicAuth"`
BasicAuthUser string `json:"basicAuthUser,omitempty"`
// Deprecated: Use secureJsonData.basicAuthPassword instead.
BasicAuthPassword string `json:"basicAuthPassword,omitempty"`
// Helper to read/write http headers
HTTPHeaders map[string]string `json:"-"`
JSONData JSONData `json:"jsonData,omitempty"`
SecureJSONData SecureJSONData `json:"secureJsonData,omitempty"`
}
// Required to avoid recursion during (un)marshal
type _DataSource DataSource
// Marshal DataSource
func (ds *DataSource) MarshalJSON() ([]byte, error) {
dataSource := _DataSource(*ds)
for name, value := range ds.HTTPHeaders {
dataSource.JSONData.httpHeaderNames = append(dataSource.JSONData.httpHeaderNames, name)
dataSource.SecureJSONData.httpHeaderValues = append(dataSource.SecureJSONData.httpHeaderValues, value)
}
// Sentry provider expects this value in the JSON data payload,
// ignoring the url attribute. This hack allows passing the URL as
// an attribute but then sends it in the payload.
if ds.Type == "grafana-sentry-datasource" {
dataSource.JSONData.URL = ds.URL
}
return json.Marshal(dataSource)
}
// Unmarshal DataSource
func (ds *DataSource) UnmarshalJSON(b []byte) (err error) {
dataSource := _DataSource(*ds)
if err = json.Unmarshal(b, &dataSource); err == nil {
*ds = DataSource(dataSource)
}
ds.HTTPHeaders = make(map[string]string)
for _, value := range ds.JSONData.httpHeaderNames {
ds.HTTPHeaders[value] = "true" // HTTP Headers are not returned by the API
}
return err
}
type LokiDerivedField struct {
Name string `json:"name"`
MatcherRegex string `json:"matcherRegex"`
URL string `json:"url"`
DatasourceUID string `json:"datasourceUid,omitempty"`
}
// JSONData is a representation of the datasource `jsonData` property
type JSONData struct {
// Used by all datasources
TLSAuth bool `json:"tlsAuth,omitempty"`
TLSAuthWithCACert bool `json:"tlsAuthWithCACert,omitempty"`
TLSConfigurationMethod string `json:"tlsConfigurationMethod,omitempty"`
TLSSkipVerify bool `json:"tlsSkipVerify,omitempty"`
httpHeaderNames []string
// Used by Athena
Catalog string `json:"catalog,omitempty"`
Database string `json:"database,omitempty"`
OutputLocation string `json:"outputLocation,omitempty"`
Workgroup string `json:"workgroup,omitempty"`
// Used by Github
GitHubURL string `json:"githubUrl,omitempty"`
// Used by Graphite
GraphiteVersion string `json:"graphiteVersion,omitempty"`
// Used by Prometheus, Elasticsearch, InfluxDB, MySQL, PostgreSQL and MSSQL
TimeInterval string `json:"timeInterval,omitempty"`
// Used by Elasticsearch
// From Grafana 8.x esVersion is the semantic version of Elasticsearch.
EsVersion string `json:"esVersion,omitempty"`
TimeField string `json:"timeField,omitempty"`
Interval string `json:"interval,omitempty"`
LogMessageField string `json:"logMessageField,omitempty"`
LogLevelField string `json:"logLevelField,omitempty"`
MaxConcurrentShardRequests int64 `json:"maxConcurrentShardRequests,omitempty"`
XpackEnabled bool `json:"xpack"`
// Used by Cloudwatch
CustomMetricsNamespaces string `json:"customMetricsNamespaces,omitempty"`
TracingDatasourceUID string `json:"tracingDatasourceUid,omitempty"`
// Used by Cloudwatch, Athena
AuthType string `json:"authType,omitempty"`
AssumeRoleArn string `json:"assumeRoleArn,omitempty"`
DefaultRegion string `json:"defaultRegion,omitempty"`
Endpoint string `json:"endpoint,omitempty"`
ExternalID string `json:"externalId,omitempty"`
Profile string `json:"profile,omitempty"`
// Used by Loki
DerivedFields []LokiDerivedField `json:"derivedFields,omitempty"`
MaxLines int `json:"maxLines,omitempty"`
// Used by OpenTSDB
TsdbVersion int64 `json:"tsdbVersion,omitempty"`
TsdbResolution int64 `json:"tsdbResolution,omitempty"`
// Used by MSSQL
Encrypt string `json:"encrypt,omitempty"`
// Used by PostgreSQL
Sslmode string `json:"sslmode,omitempty"`
PostgresVersion int64 `json:"postgresVersion,omitempty"`
Timescaledb bool `json:"timescaledb"`
// Used by MySQL, PostgreSQL and MSSQL
MaxOpenConns int64 `json:"maxOpenConns,omitempty"`
MaxIdleConns int64 `json:"maxIdleConns,omitempty"`
ConnMaxLifetime int64 `json:"connMaxLifetime,omitempty"`
// Used by Prometheus
HTTPMethod string `json:"httpMethod,omitempty"`
QueryTimeout string `json:"queryTimeout,omitempty"`
// Used by Stackdriver
AuthenticationType string `json:"authenticationType,omitempty"`
ClientEmail string `json:"clientEmail,omitempty"`
DefaultProject string `json:"defaultProject,omitempty"`
TokenURI string `json:"tokenUri,omitempty"`
// Used by Prometheus and Elasticsearch
SigV4AssumeRoleArn string `json:"sigV4AssumeRoleArn,omitempty"`
SigV4Auth bool `json:"sigV4Auth"`
SigV4AuthType string `json:"sigV4AuthType,omitempty"`
SigV4ExternalID string `json:"sigV4ExternalID,omitempty"`
SigV4Profile string `json:"sigV4Profile,omitempty"`
SigV4Region string `json:"sigV4Region,omitempty"`
// Used by Prometheus and Loki
ManageAlerts bool `json:"manageAlerts"`
AlertmanagerUID string `json:"alertmanagerUid,omitempty"`
// Used by Alertmanager
Implementation string `json:"implementation,omitempty"`
// Used by Sentry
OrgSlug string `json:"orgSlug,omitempty"`
URL string `json:"url,omitempty"` // Sentry is not using the datasource URL attribute
// Used by InfluxDB
DefaultBucket string `json:"defaultBucket,omitempty"`
Organization string `json:"organization,omitempty"`
Version string `json:"version,omitempty"`
// Used by Azure Monitor
AzureLogAnalyticsSameAs bool `json:"azureLogAnalyticsSameAs"`
ClientID string `json:"clientId,omitempty"`
CloudName string `json:"cloudName,omitempty"`
LogAnalyticsClientID string `json:"logAnalyticsClientId,omitempty"`
LogAnalyticsDefaultWorkspace string `json:"logAnalyticsDefaultWorkspace,omitempty"`
LogAnalyticsTenantID string `json:"logAnalyticsTenantId,omitempty"`
SubscriptionID string `json:"subscriptionId,omitempty"`
TenantID string `json:"tenantId,omitempty"`
}
// Required to avoid recursion during (un)marshal
type _JSONData JSONData
// Marshal JSONData
func (jd JSONData) MarshalJSON() ([]byte, error) {
jsonData := _JSONData(jd)
b, err := json.Marshal(jsonData)
if err != nil {
return nil, err
}
fields := make(map[string]interface{})
if err = json.Unmarshal(b, &fields); err != nil {
return nil, err
}
for index, name := range jd.httpHeaderNames {
fields[fmt.Sprintf("httpHeaderName%d", index+1)] = name
}
return json.Marshal(fields)
}
// Unmarshal JSONData
func (jd *JSONData) UnmarshalJSON(b []byte) (err error) {
jsonData := _JSONData(*jd)
if err = json.Unmarshal(b, &jsonData); err == nil {
*jd = JSONData(jsonData)
}
fields := make(map[string]interface{})
if err = json.Unmarshal(b, &fields); err == nil {
headerCount := 0
for name := range fields {
match := headerNameRegex.FindStringSubmatch(name)
if len(match) > 0 {
headerCount++
}
}
jd.httpHeaderNames = make([]string, headerCount)
for name, value := range fields {
match := headerNameRegex.FindStringSubmatch(name)
if len(match) == 2 {
index, err := strconv.ParseInt(match[1], 10, 64)
if err != nil {
return err
}
jd.httpHeaderNames[index-1] = value.(string)
}
}
}
return err
}
// SecureJSONData is a representation of the datasource `secureJsonData` property
type SecureJSONData struct {
// Used by all datasources
TLSCACert string `json:"tlsCACert,omitempty"`
TLSClientCert string `json:"tlsClientCert,omitempty"`
TLSClientKey string `json:"tlsClientKey,omitempty"`
Password string `json:"password,omitempty"`
BasicAuthPassword string `json:"basicAuthPassword,omitempty"`
httpHeaderValues []string
// Used by Cloudwatch, Athena
AccessKey string `json:"accessKey,omitempty"`
SecretKey string `json:"secretKey,omitempty"`
// Used by Stackdriver
PrivateKey string `json:"privateKey,omitempty"`
// Used by Prometheus and Elasticsearch
SigV4AccessKey string `json:"sigV4AccessKey,omitempty"`
SigV4SecretKey string `json:"sigV4SecretKey,omitempty"`
// Used by GitHub
AccessToken string `json:"accessToken,omitempty"`
// Used by Sentry
AuthToken string `json:"authToken,omitempty"`
// Used by Azure Monitor
ClientSecret string `json:"clientSecret,omitempty"`
}
// Required to avoid recursion during unmarshal
type _SecureJSONData SecureJSONData
// Marshal SecureJSONData
func (sjd SecureJSONData) MarshalJSON() ([]byte, error) {
secureJSONData := _SecureJSONData(sjd)
b, err := json.Marshal(secureJSONData)
if err != nil {
return nil, err
}
fields := make(map[string]interface{})
if err = json.Unmarshal(b, &fields); err != nil {
return nil, err
}
for index, value := range sjd.httpHeaderValues {
fields[fmt.Sprintf("httpHeaderValue%d", index+1)] = value
}
return json.Marshal(fields)
}
// NewDataSource creates a new Grafana data source.
func (c *Client) NewDataSource(s *DataSource) (int64, error) {
data, err := json.Marshal(s)
if err != nil {
return 0, err
}
result := struct {
ID int64 `json:"id"`
}{}
err = c.request("POST", "/api/datasources", nil, bytes.NewBuffer(data), &result)
if err != nil {
return 0, err
}
return result.ID, err
}
// UpdateDataSource updates a Grafana data source.
func (c *Client) UpdateDataSource(s *DataSource) error {
path := fmt.Sprintf("/api/datasources/%d", s.ID)
data, err := json.Marshal(s)
if err != nil {
return err
}
return c.request("PUT", path, nil, bytes.NewBuffer(data), nil)
}
// DataSource fetches and returns the Grafana data source whose ID it's passed.
func (c *Client) DataSource(id int64) (*DataSource, error) {
path := fmt.Sprintf("/api/datasources/%d", id)
result := &DataSource{}
err := c.request("GET", path, nil, nil, result)
if err != nil {
return nil, err
}
return result, err
}
// DataSourceByUID fetches and returns the Grafana data source whose UID is passed.
func (c *Client) DataSourceByUID(uid string) (*DataSource, error) {
path := fmt.Sprintf("/api/datasources/uid/%s", uid)
result := &DataSource{}
err := c.request("GET", path, nil, nil, result)
if err != nil {
return nil, err
}
return result, err
}
// DeleteDataSource deletes the Grafana data source whose ID it's passed.
func (c *Client) DeleteDataSource(id int64) error {
path := fmt.Sprintf("/api/datasources/%d", id)
return c.request("DELETE", path, nil, nil, nil)
}