Skip to content
This repository was archived by the owner on Jan 15, 2024. It is now read-only.

Commit c9d649a

Browse files
Merge branch 'master' into fix-logging
2 parents 4c2b54f + 1a12415 commit c9d649a

5 files changed

+251
-308
lines changed

datasource.go

+47-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package gapi
33
import (
44
"encoding/json"
55
"fmt"
6+
"strings"
67
)
78

89
// DataSource represents a Grafana data source.
@@ -19,16 +20,12 @@ type DataSource struct {
1920

2021
Database string `json:"database,omitempty"`
2122
User string `json:"user,omitempty"`
22-
// Deprecated: Use secureJsonData.password instead.
23-
Password string `json:"password,omitempty"`
2423

2524
OrgID int64 `json:"orgId,omitempty"`
2625
IsDefault bool `json:"isDefault"`
2726

2827
BasicAuth bool `json:"basicAuth"`
2928
BasicAuthUser string `json:"basicAuthUser,omitempty"`
30-
// Deprecated: Use secureJsonData.basicAuthPassword instead.
31-
BasicAuthPassword string `json:"basicAuthPassword,omitempty"`
3229

3330
JSONData map[string]interface{} `json:"jsonData,omitempty"`
3431
SecureJSONData map[string]interface{} `json:"secureJsonData,omitempty"`
@@ -138,3 +135,49 @@ func (c *Client) DeleteDataSourceByName(name string) error {
138135

139136
return c.request("DELETE", path, nil, nil, nil)
140137
}
138+
139+
func cloneMap(m map[string]interface{}) map[string]interface{} {
140+
clone := make(map[string]interface{})
141+
for k, v := range m {
142+
clone[k] = v
143+
}
144+
return clone
145+
}
146+
147+
func JSONDataWithHeaders(jsonData, secureJSONData map[string]interface{}, headers map[string]string) (map[string]interface{}, map[string]interface{}) {
148+
// Clone the maps so we don't modify the original
149+
jsonData = cloneMap(jsonData)
150+
secureJSONData = cloneMap(secureJSONData)
151+
152+
idx := 1
153+
for name, value := range headers {
154+
jsonData[fmt.Sprintf("httpHeaderName%d", idx)] = name
155+
secureJSONData[fmt.Sprintf("httpHeaderValue%d", idx)] = value
156+
idx += 1
157+
}
158+
159+
return jsonData, secureJSONData
160+
}
161+
162+
func ExtractHeadersFromJSONData(jsonData, secureJSONData map[string]interface{}) (map[string]interface{}, map[string]interface{}, map[string]string) {
163+
// Clone the maps so we don't modify the original
164+
jsonData = cloneMap(jsonData)
165+
secureJSONData = cloneMap(secureJSONData)
166+
headers := make(map[string]string)
167+
168+
for dataName, dataValue := range jsonData {
169+
if strings.HasPrefix(dataName, "httpHeaderName") {
170+
// Remove the header name from JSON data
171+
delete(jsonData, dataName)
172+
173+
// Remove the header value from secure JSON data
174+
secureDataName := strings.Replace(dataName, "httpHeaderName", "httpHeaderValue", 1)
175+
delete(secureJSONData, secureDataName)
176+
177+
headerName := dataValue.(string)
178+
headers[headerName] = "true" // We can't retrieve the headers, so we just set a dummy value
179+
}
180+
}
181+
182+
return jsonData, secureJSONData, headers
183+
}

datasource_cache.go

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package gapi
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
)
7+
8+
type DatasourceCache struct {
9+
Message string `json:"message"`
10+
DatasourceID int64 `json:"dataSourceID"`
11+
DatasourceUID string `json:"dataSourceUID"`
12+
Enabled bool `json:"enabled"`
13+
TTLQueriesMs int64 `json:"ttlQueriesMs"`
14+
TTLResourcesMs int64 `json:"ttlResourcesMs"`
15+
UseDefaultTLS bool `json:"useDefaultTTL"`
16+
DefaultTTLMs int64 `json:"defaultTTLMs"`
17+
Created string `json:"created"`
18+
Updated string `json:"updated"`
19+
}
20+
21+
type DatasourceCachePayload struct {
22+
DatasourceID int64 `json:"dataSourceID"`
23+
DatasourceUID string `json:"dataSourceUID"`
24+
Enabled bool `json:"enabled"`
25+
UseDefaultTLS bool `json:"useDefaultTTL"`
26+
TTLQueriesMs int64 `json:"ttlQueriesMs"`
27+
TTLResourcesMs int64 `json:"ttlResourcesMs"`
28+
}
29+
30+
// EnableDatasourceCache enables the datasource cache (this is a datasource setting)
31+
func (c *Client) EnableDatasourceCache(id int64) error {
32+
path := fmt.Sprintf("/api/datasources/%d/cache/enable", id)
33+
if err := c.request("POST", path, nil, nil, nil); err != nil {
34+
return fmt.Errorf("error enabling cache at %s: %w", path, err)
35+
}
36+
return nil
37+
}
38+
39+
// DisableDatasourceCache disables the datasource cache (this is a datasource setting)
40+
func (c *Client) DisableDatasourceCache(id int64) error {
41+
path := fmt.Sprintf("/api/datasources/%d/cache/disable", id)
42+
if err := c.request("POST", path, nil, nil, nil); err != nil {
43+
return fmt.Errorf("error disabling cache at %s: %w", path, err)
44+
}
45+
return nil
46+
}
47+
48+
// UpdateDatasourceCache updates the cache configurations
49+
func (c *Client) UpdateDatasourceCache(id int64, payload *DatasourceCachePayload) error {
50+
path := fmt.Sprintf("/api/datasources/%d/cache", id)
51+
data, err := json.Marshal(payload)
52+
if err != nil {
53+
return fmt.Errorf("marshal err: %w", err)
54+
}
55+
56+
if err = c.request("POST", path, nil, data, nil); err != nil {
57+
return fmt.Errorf("error updating cache at %s: %w", path, err)
58+
}
59+
60+
return nil
61+
}
62+
63+
// DatasourceCache fetches datasource cache configuration
64+
func (c *Client) DatasourceCache(id int64) (*DatasourceCache, error) {
65+
path := fmt.Sprintf("/api/datasources/%d/cache", id)
66+
cache := &DatasourceCache{}
67+
err := c.request("GET", path, nil, nil, cache)
68+
if err != nil {
69+
return cache, fmt.Errorf("error getting cache at %s: %w", path, err)
70+
}
71+
return cache, nil
72+
}

datasource_cache_test.go

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package gapi
2+
3+
import (
4+
"testing"
5+
6+
"github.com/gobs/pretty"
7+
)
8+
9+
const (
10+
getDatasourceCacheJSON = `
11+
{
12+
"message": "Data source cache settings loaded",
13+
"dataSourceID": 1,
14+
"dataSourceUID": "jZrmlLCGka",
15+
"enabled": true,
16+
"useDefaultTTL": false,
17+
"ttlQueriesMs": 60000,
18+
"ttlResourcesMs": 300000,
19+
"defaultTTLMs": 300000,
20+
"created": "2023-04-21T11:49:22-04:00",
21+
"updated": "2023-04-24T17:03:40-04:00"
22+
}`
23+
updateDatasourceCacheJSON = `
24+
{
25+
"message": "Data source cache settings updated",
26+
"dataSourceID": 1,
27+
"dataSourceUID": "jZrmlLCGka",
28+
"enabled": true,
29+
"useDefaultTTL": false,
30+
"ttlQueriesMs": 60000,
31+
"ttlResourcesMs": 300000,
32+
"defaultTTLMs": 300000,
33+
"created": "2023-04-21T11:49:22-04:00",
34+
"updated": "2023-04-24T17:03:40-04:00"
35+
}`
36+
)
37+
38+
func TestDatasourceCache(t *testing.T) {
39+
client := gapiTestTools(t, 200, getDatasourceCacheJSON)
40+
resp, err := client.DatasourceCache(1)
41+
if err != nil {
42+
t.Fatal(err)
43+
}
44+
45+
t.Log(pretty.PrettyFormat(resp))
46+
47+
expects := DatasourceCache{
48+
Message: "Data source cache settings loaded",
49+
DatasourceID: 1,
50+
DatasourceUID: "jZrmlLCGka",
51+
Enabled: true,
52+
UseDefaultTLS: false,
53+
TTLQueriesMs: 60000,
54+
TTLResourcesMs: 300000,
55+
DefaultTTLMs: 300000,
56+
Created: "2023-04-21T11:49:22-04:00",
57+
Updated: "2023-04-24T17:03:40-04:00",
58+
}
59+
60+
if resp.Enabled != expects.Enabled ||
61+
resp.DatasourceUID != expects.DatasourceUID ||
62+
resp.UseDefaultTLS != expects.UseDefaultTLS ||
63+
resp.TTLQueriesMs != expects.TTLQueriesMs ||
64+
resp.TTLResourcesMs != expects.TTLResourcesMs ||
65+
resp.DefaultTTLMs != expects.DefaultTTLMs {
66+
t.Error("Not correctly parsing returned datasource cache")
67+
}
68+
}
69+
70+
func TestUpdateDatasourceCache(t *testing.T) {
71+
client := gapiTestTools(t, 200, updateDatasourceCacheJSON)
72+
payload := &DatasourceCachePayload{
73+
DatasourceID: 1,
74+
DatasourceUID: "jZrmlLCGka",
75+
Enabled: true,
76+
UseDefaultTLS: true,
77+
TTLQueriesMs: 6000,
78+
TTLResourcesMs: 30000,
79+
}
80+
err := client.UpdateDatasourceCache(1, payload)
81+
if err != nil {
82+
t.Error(err)
83+
}
84+
}

0 commit comments

Comments
 (0)