|
| 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 | +} |
0 commit comments