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

Commit 755d9f0

Browse files
committed
Add datasource caching API
1 parent b291103 commit 755d9f0

File tree

2 files changed

+156
-0
lines changed

2 files changed

+156
-0
lines changed

Diff for: 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+
}

Diff for: 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)