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

Commit

Permalink
Merge branch 'master' into fix-logging
Browse files Browse the repository at this point in the history
  • Loading branch information
yuri-tceretian committed Aug 24, 2023
2 parents 4c2b54f + 1a12415 commit c9d649a
Show file tree
Hide file tree
Showing 5 changed files with 251 additions and 308 deletions.
51 changes: 47 additions & 4 deletions datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gapi
import (
"encoding/json"
"fmt"
"strings"
)

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

Database string `json:"database,omitempty"`
User string `json:"user,omitempty"`
// Deprecated: Use secureJsonData.password instead.
Password string `json:"password,omitempty"`

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

BasicAuth bool `json:"basicAuth"`
BasicAuthUser string `json:"basicAuthUser,omitempty"`
// Deprecated: Use secureJsonData.basicAuthPassword instead.
BasicAuthPassword string `json:"basicAuthPassword,omitempty"`

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

return c.request("DELETE", path, nil, nil, nil)
}

func cloneMap(m map[string]interface{}) map[string]interface{} {
clone := make(map[string]interface{})
for k, v := range m {
clone[k] = v
}
return clone
}

func JSONDataWithHeaders(jsonData, secureJSONData map[string]interface{}, headers map[string]string) (map[string]interface{}, map[string]interface{}) {
// Clone the maps so we don't modify the original
jsonData = cloneMap(jsonData)
secureJSONData = cloneMap(secureJSONData)

idx := 1
for name, value := range headers {
jsonData[fmt.Sprintf("httpHeaderName%d", idx)] = name
secureJSONData[fmt.Sprintf("httpHeaderValue%d", idx)] = value
idx += 1
}

return jsonData, secureJSONData
}

func ExtractHeadersFromJSONData(jsonData, secureJSONData map[string]interface{}) (map[string]interface{}, map[string]interface{}, map[string]string) {
// Clone the maps so we don't modify the original
jsonData = cloneMap(jsonData)
secureJSONData = cloneMap(secureJSONData)
headers := make(map[string]string)

for dataName, dataValue := range jsonData {
if strings.HasPrefix(dataName, "httpHeaderName") {
// Remove the header name from JSON data
delete(jsonData, dataName)

// Remove the header value from secure JSON data
secureDataName := strings.Replace(dataName, "httpHeaderName", "httpHeaderValue", 1)
delete(secureJSONData, secureDataName)

headerName := dataValue.(string)
headers[headerName] = "true" // We can't retrieve the headers, so we just set a dummy value
}
}

return jsonData, secureJSONData, headers
}
72 changes: 72 additions & 0 deletions datasource_cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
}
84 changes: 84 additions & 0 deletions datasource_cache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
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"
}`
updateDatasourceCacheJSON = `
{
"message": "Data source cache settings updated",
"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",
}

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")
}
}

func TestUpdateDatasourceCache(t *testing.T) {
client := gapiTestTools(t, 200, updateDatasourceCacheJSON)
payload := &DatasourceCachePayload{
DatasourceID: 1,
DatasourceUID: "jZrmlLCGka",
Enabled: true,
UseDefaultTLS: true,
TTLQueriesMs: 6000,
TTLResourcesMs: 30000,
}
err := client.UpdateDatasourceCache(1, payload)
if err != nil {
t.Error(err)
}
}

0 comments on commit c9d649a

Please sign in to comment.