This repository has been archived by the owner on Jan 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 97
/
datasource_cache.go
72 lines (63 loc) · 2.3 KB
/
datasource_cache.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
package gapi
import (
"encoding/json"
"fmt"
)
type DatasourceCache struct {
Message string `json:"message"`
DatasourceID int64 `json:"dataSourceID"`
DatasourceUID string `json:"dataSourceUID"`
Enabled bool `json:"enabled"`
TTLQueriesMs int64 `json:"ttlQueriesMs"`
TTLResourcesMs int64 `json:"ttlResourcesMs"`
UseDefaultTLS bool `json:"useDefaultTTL"`
DefaultTTLMs int64 `json:"defaultTTLMs"`
Created string `json:"created"`
Updated string `json:"updated"`
}
type DatasourceCachePayload struct {
DatasourceID int64 `json:"dataSourceID"`
DatasourceUID string `json:"dataSourceUID"`
Enabled bool `json:"enabled"`
UseDefaultTLS bool `json:"useDefaultTTL"`
TTLQueriesMs int64 `json:"ttlQueriesMs"`
TTLResourcesMs int64 `json:"ttlResourcesMs"`
}
// EnableDatasourceCache enables the datasource cache (this is a datasource setting)
func (c *Client) EnableDatasourceCache(id int64) error {
path := fmt.Sprintf("/api/datasources/%d/cache/enable", id)
if err := c.request("POST", path, nil, nil, nil); err != nil {
return fmt.Errorf("error enabling cache at %s: %w", path, err)
}
return nil
}
// DisableDatasourceCache disables the datasource cache (this is a datasource setting)
func (c *Client) DisableDatasourceCache(id int64) error {
path := fmt.Sprintf("/api/datasources/%d/cache/disable", id)
if err := c.request("POST", path, nil, nil, nil); err != nil {
return fmt.Errorf("error disabling cache at %s: %w", path, err)
}
return nil
}
// UpdateDatasourceCache updates the cache configurations
func (c *Client) UpdateDatasourceCache(id int64, payload *DatasourceCachePayload) error {
path := fmt.Sprintf("/api/datasources/%d/cache", id)
data, err := json.Marshal(payload)
if err != nil {
return fmt.Errorf("marshal err: %w", err)
}
if err = c.request("POST", path, nil, data, nil); err != nil {
return fmt.Errorf("error updating cache at %s: %w", path, err)
}
return nil
}
// DatasourceCache fetches datasource cache configuration
func (c *Client) DatasourceCache(id int64) (*DatasourceCache, error) {
path := fmt.Sprintf("/api/datasources/%d/cache", id)
cache := &DatasourceCache{}
err := c.request("GET", path, nil, nil, cache)
if err != nil {
return cache, fmt.Errorf("error getting cache at %s: %w", path, err)
}
return cache, nil
}