forked from grafana/grafana-api-golang-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasource.go
164 lines (133 loc) · 5.04 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
package gapi
import (
"bytes"
"encoding/json"
"fmt"
)
// DataSource represents a Grafana data source.
type DataSource struct {
Id int64 `json:"id,omitempty"`
Name string `json:"name"`
Type string `json:"type"`
URL string `json:"url"`
Access string `json:"access"`
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"`
JSONData JSONData `json:"jsonData,omitempty"`
SecureJSONData SecureJSONData `json:"secureJsonData,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"`
TlsSkipVerify bool `json:"tlsSkipVerify,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
EsVersion int64 `json:"esVersion,omitempty"`
TimeField string `json:"timeField,omitempty"`
Interval string `json:"inteval,omitempty"`
LogMessageField string `json:"logMessageField,omitempty"`
LogLevelField string `json:"logLevelField,omitempty"`
// Used by Cloudwatch
AuthType string `json:"authType,omitempty"`
AssumeRoleArn string `json:"assumeRoleArn,omitempty"`
DefaultRegion string `json:"defaultRegion,omitempty"`
CustomMetricsNamespaces string `json:"customMetricsNamespaces,omitempty"`
// Used by OpenTSDB
TsdbVersion int8 `json:"tsdbVersion,omitempty"`
TsdbResolution int8 `json:"tsdbResolution,omitempty"`
LookupLimit string `json:"lookupLimit,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,omitempty"`
// 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"`
}
// 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"`
// Used by Cloudwatch
AccessKey string `json:"accessKey,omitempty"`
SecretKey string `json:"secretKey,omitempty"`
// Used by Stackdriver
PrivateKey string `json:"privateKey,omitempty"`
}
// 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
}
// DataSources fetches and returns all the Grafana data sources.
func (c *Client) DataSources() (*[]DataSource, error) {
path := "/api/datasources/"
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)
}