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

Commit

Permalink
Add datasource caching API
Browse files Browse the repository at this point in the history
  • Loading branch information
cindy committed Aug 1, 2023
1 parent b291103 commit ae8a33d
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
29 changes: 29 additions & 0 deletions datasource_cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package gapi

import (
"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"`
}

// DatasourceCache fetch 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
}
56 changes: 56 additions & 0 deletions datasource_cache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package gapi

import (
"testing"

"github.com/gobs/pretty"
)

const (
getDatasourceCacheJSON = `{
"message": "Data source cache settings loaded",
"dataSourceID": 1,
"dataSourceUID": "jZrmlLCGka",
"enabled": true,
"useDefaultTTL": false,
"ttlQueriesMs": 60000,
"ttlResourcesMs": 300000,
"defaultTTLMs": 300000,
"created": "2023-04-21T11:49:22-04:00",
"updated": "2023-04-24T17:03:40-04:00"
}`
)

func TestDatasourceCache(t *testing.T) {
client := gapiTestTools(t, 200, getDatasourceCacheJSON)
resp, err := client.DatasourceCache(1)
if err != nil {
t.Fatal(err)
}

t.Log(pretty.PrettyFormat(resp))

expects := DatasourceCache{
Message: "Data source cache settings loaded",
DatasourceID: 1,
DatasourceUID: "jZrmlLCGka",
Enabled: true,
UseDefaultTLS: false,
TTLQueriesMs: 60000,
TTLResourcesMs: 300000,
DefaultTTLMs: 300000,
Created: "2023-04-21T11:49:22-04:00",
Updated: "2023-04-24T17:03:40-04:00",
}

t.Run("check data", func(t *testing.T) {
if resp.Enabled != expects.Enabled ||
resp.DatasourceUID != expects.DatasourceUID ||
resp.UseDefaultTLS != expects.UseDefaultTLS ||
resp.TTLQueriesMs != expects.TTLQueriesMs ||
resp.TTLResourcesMs != expects.TTLResourcesMs ||
resp.DefaultTTLMs != expects.DefaultTTLMs {
t.Error("Not correctly parsing returned datasource cache")
}
})
}

0 comments on commit ae8a33d

Please sign in to comment.