mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
AzureMonitor: Remove simplejson
* tsdb: azuremonitor: Remove usage of simplejson * Review fixes * Fix lint errors; review fix
This commit is contained in:
parent
5e5c751ecd
commit
5e8bd48c3c
@ -17,7 +17,6 @@ import (
|
|||||||
"github.com/grafana/grafana-plugin-sdk-go/backend/instancemgmt"
|
"github.com/grafana/grafana-plugin-sdk-go/backend/instancemgmt"
|
||||||
"github.com/grafana/grafana-plugin-sdk-go/backend/resource/httpadapter"
|
"github.com/grafana/grafana-plugin-sdk-go/backend/resource/httpadapter"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
|
||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
"github.com/grafana/grafana/pkg/infra/tracing"
|
"github.com/grafana/grafana/pkg/infra/tracing"
|
||||||
"github.com/grafana/grafana/pkg/setting"
|
"github.com/grafana/grafana/pkg/setting"
|
||||||
@ -83,23 +82,19 @@ func getDatasourceService(settings *backend.DataSourceInstanceSettings, cfg *set
|
|||||||
|
|
||||||
func NewInstanceSettings(cfg *setting.Cfg, clientProvider *httpclient.Provider, executors map[string]azDatasourceExecutor) datasource.InstanceFactoryFunc {
|
func NewInstanceSettings(cfg *setting.Cfg, clientProvider *httpclient.Provider, executors map[string]azDatasourceExecutor) datasource.InstanceFactoryFunc {
|
||||||
return func(settings backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) {
|
return func(settings backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) {
|
||||||
jsonData, err := simplejson.NewJson(settings.JSONData)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("error reading settings: %w", err)
|
|
||||||
}
|
|
||||||
jsonDataObj := map[string]any{}
|
jsonDataObj := map[string]any{}
|
||||||
err = json.Unmarshal(settings.JSONData, &jsonDataObj)
|
err := json.Unmarshal(settings.JSONData, &jsonDataObj)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error reading settings: %w", err)
|
return nil, fmt.Errorf("error reading settings: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
azMonitorSettings := types.AzureMonitorSettings{}
|
azSettings := types.AzureSettings{}
|
||||||
err = json.Unmarshal(settings.JSONData, &azMonitorSettings)
|
err = json.Unmarshal(settings.JSONData, &azSettings)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error reading settings: %w", err)
|
return nil, fmt.Errorf("error reading settings: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
cloud, err := getAzureCloud(cfg, jsonData)
|
cloud, err := getAzureCloud(cfg, &azSettings.AzureClientSettings)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error getting credentials: %w", err)
|
return nil, fmt.Errorf("error getting credentials: %w", err)
|
||||||
}
|
}
|
||||||
@ -109,7 +104,7 @@ func NewInstanceSettings(cfg *setting.Cfg, clientProvider *httpclient.Provider,
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
credentials, err := getAzureCredentials(cfg, jsonData, settings.DecryptedSecureJSONData)
|
credentials, err := getAzureCredentials(cfg, &azSettings.AzureClientSettings, settings.DecryptedSecureJSONData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error getting credentials: %w", err)
|
return nil, fmt.Errorf("error getting credentials: %w", err)
|
||||||
}
|
}
|
||||||
@ -117,7 +112,7 @@ func NewInstanceSettings(cfg *setting.Cfg, clientProvider *httpclient.Provider,
|
|||||||
model := types.DatasourceInfo{
|
model := types.DatasourceInfo{
|
||||||
Cloud: cloud,
|
Cloud: cloud,
|
||||||
Credentials: credentials,
|
Credentials: credentials,
|
||||||
Settings: azMonitorSettings,
|
Settings: azSettings.AzureMonitorSettings,
|
||||||
JSONData: jsonDataObj,
|
JSONData: jsonDataObj,
|
||||||
DecryptedSecureJSONData: settings.DecryptedSecureJSONData,
|
DecryptedSecureJSONData: settings.DecryptedSecureJSONData,
|
||||||
DatasourceID: settings.ID,
|
DatasourceID: settings.ID,
|
||||||
|
@ -6,8 +6,8 @@ import (
|
|||||||
"github.com/grafana/grafana-azure-sdk-go/azcredentials"
|
"github.com/grafana/grafana-azure-sdk-go/azcredentials"
|
||||||
"github.com/grafana/grafana-azure-sdk-go/azsettings"
|
"github.com/grafana/grafana-azure-sdk-go/azsettings"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
|
||||||
"github.com/grafana/grafana/pkg/setting"
|
"github.com/grafana/grafana/pkg/setting"
|
||||||
|
"github.com/grafana/grafana/pkg/tsdb/azuremonitor/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Azure cloud names specific to Azure Monitor
|
// Azure cloud names specific to Azure Monitor
|
||||||
@ -18,12 +18,12 @@ const (
|
|||||||
azureMonitorCustomized = "customizedazuremonitor"
|
azureMonitorCustomized = "customizedazuremonitor"
|
||||||
)
|
)
|
||||||
|
|
||||||
func getAuthType(cfg *setting.Cfg, jsonData *simplejson.Json) string {
|
func getAuthType(cfg *setting.Cfg, jsonData *types.AzureClientSettings) string {
|
||||||
if azureAuthType := jsonData.Get("azureAuthType").MustString(); azureAuthType != "" {
|
if azureAuthType := jsonData.AzureAuthType; azureAuthType != "" {
|
||||||
return azureAuthType
|
return azureAuthType
|
||||||
} else {
|
} else {
|
||||||
tenantId := jsonData.Get("tenantId").MustString()
|
tenantId := jsonData.TenantId
|
||||||
clientId := jsonData.Get("clientId").MustString()
|
clientId := jsonData.ClientId
|
||||||
|
|
||||||
// If authentication type isn't explicitly specified and datasource has client credentials,
|
// If authentication type isn't explicitly specified and datasource has client credentials,
|
||||||
// then this is existing datasource which is configured for app registration (client secret)
|
// then this is existing datasource which is configured for app registration (client secret)
|
||||||
@ -81,14 +81,14 @@ func normalizeAzureCloud(cloudName string) (string, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getAzureCloud(cfg *setting.Cfg, jsonData *simplejson.Json) (string, error) {
|
func getAzureCloud(cfg *setting.Cfg, jsonData *types.AzureClientSettings) (string, error) {
|
||||||
authType := getAuthType(cfg, jsonData)
|
authType := getAuthType(cfg, jsonData)
|
||||||
switch authType {
|
switch authType {
|
||||||
case azcredentials.AzureAuthManagedIdentity:
|
case azcredentials.AzureAuthManagedIdentity:
|
||||||
// In case of managed identity, the cloud is always same as where Grafana is hosted
|
// In case of managed identity, the cloud is always same as where Grafana is hosted
|
||||||
return getDefaultAzureCloud(cfg)
|
return getDefaultAzureCloud(cfg)
|
||||||
case azcredentials.AzureAuthClientSecret:
|
case azcredentials.AzureAuthClientSecret:
|
||||||
if cloud := jsonData.Get("cloudName").MustString(); cloud != "" {
|
if cloud := jsonData.CloudName; cloud != "" {
|
||||||
return normalizeAzureCloud(cloud)
|
return normalizeAzureCloud(cloud)
|
||||||
} else {
|
} else {
|
||||||
return getDefaultAzureCloud(cfg)
|
return getDefaultAzureCloud(cfg)
|
||||||
@ -99,7 +99,7 @@ func getAzureCloud(cfg *setting.Cfg, jsonData *simplejson.Json) (string, error)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getAzureCredentials(cfg *setting.Cfg, jsonData *simplejson.Json, secureJsonData map[string]string) (azcredentials.AzureCredentials, error) {
|
func getAzureCredentials(cfg *setting.Cfg, jsonData *types.AzureClientSettings, secureJsonData map[string]string) (azcredentials.AzureCredentials, error) {
|
||||||
authType := getAuthType(cfg, jsonData)
|
authType := getAuthType(cfg, jsonData)
|
||||||
|
|
||||||
switch authType {
|
switch authType {
|
||||||
@ -117,8 +117,8 @@ func getAzureCredentials(cfg *setting.Cfg, jsonData *simplejson.Json, secureJson
|
|||||||
}
|
}
|
||||||
credentials := &azcredentials.AzureClientSecretCredentials{
|
credentials := &azcredentials.AzureClientSecretCredentials{
|
||||||
AzureCloud: cloud,
|
AzureCloud: cloud,
|
||||||
TenantId: jsonData.Get("tenantId").MustString(),
|
TenantId: jsonData.TenantId,
|
||||||
ClientId: jsonData.Get("clientId").MustString(),
|
ClientId: jsonData.ClientId,
|
||||||
ClientSecret: secureJsonData["clientSecret"],
|
ClientSecret: secureJsonData["clientSecret"],
|
||||||
}
|
}
|
||||||
return credentials, nil
|
return credentials, nil
|
||||||
|
@ -3,10 +3,11 @@ package azuremonitor
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/grafana/grafana/pkg/tsdb/azuremonitor/types"
|
||||||
|
|
||||||
"github.com/grafana/grafana-azure-sdk-go/azcredentials"
|
"github.com/grafana/grafana-azure-sdk-go/azcredentials"
|
||||||
"github.com/grafana/grafana-azure-sdk-go/azsettings"
|
"github.com/grafana/grafana-azure-sdk-go/azsettings"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
|
||||||
"github.com/grafana/grafana/pkg/setting"
|
"github.com/grafana/grafana/pkg/setting"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
@ -21,9 +22,9 @@ func TestCredentials_getAuthType(t *testing.T) {
|
|||||||
cfg.Azure.ManagedIdentityEnabled = true
|
cfg.Azure.ManagedIdentityEnabled = true
|
||||||
|
|
||||||
t.Run("should be client secret if auth type is set to client secret", func(t *testing.T) {
|
t.Run("should be client secret if auth type is set to client secret", func(t *testing.T) {
|
||||||
jsonData := simplejson.NewFromAny(map[string]interface{}{
|
jsonData := &types.AzureClientSettings{
|
||||||
"azureAuthType": azcredentials.AzureAuthClientSecret,
|
AzureAuthType: azcredentials.AzureAuthClientSecret,
|
||||||
})
|
}
|
||||||
|
|
||||||
authType := getAuthType(cfg, jsonData)
|
authType := getAuthType(cfg, jsonData)
|
||||||
|
|
||||||
@ -31,9 +32,9 @@ func TestCredentials_getAuthType(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("should be managed identity if datasource not configured", func(t *testing.T) {
|
t.Run("should be managed identity if datasource not configured", func(t *testing.T) {
|
||||||
jsonData := simplejson.NewFromAny(map[string]interface{}{
|
jsonData := &types.AzureClientSettings{
|
||||||
"azureAuthType": "",
|
AzureAuthType: "",
|
||||||
})
|
}
|
||||||
|
|
||||||
authType := getAuthType(cfg, jsonData)
|
authType := getAuthType(cfg, jsonData)
|
||||||
|
|
||||||
@ -41,11 +42,11 @@ func TestCredentials_getAuthType(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("should be client secret if auth type not specified but credentials configured", func(t *testing.T) {
|
t.Run("should be client secret if auth type not specified but credentials configured", func(t *testing.T) {
|
||||||
jsonData := simplejson.NewFromAny(map[string]interface{}{
|
jsonData := &types.AzureClientSettings{
|
||||||
"azureAuthType": "",
|
AzureAuthType: "",
|
||||||
"tenantId": "9b9d90ee-a5cc-49c2-b97e-0d1b0f086b5c",
|
TenantId: "9b9d90ee-a5cc-49c2-b97e-0d1b0f086b5c",
|
||||||
"clientId": "849ccbb0-92eb-4226-b228-ef391abd8fe6",
|
ClientId: "849ccbb0-92eb-4226-b228-ef391abd8fe6",
|
||||||
})
|
}
|
||||||
|
|
||||||
authType := getAuthType(cfg, jsonData)
|
authType := getAuthType(cfg, jsonData)
|
||||||
|
|
||||||
@ -57,9 +58,9 @@ func TestCredentials_getAuthType(t *testing.T) {
|
|||||||
cfg.Azure.ManagedIdentityEnabled = false
|
cfg.Azure.ManagedIdentityEnabled = false
|
||||||
|
|
||||||
t.Run("should be managed identity if auth type is set to managed identity", func(t *testing.T) {
|
t.Run("should be managed identity if auth type is set to managed identity", func(t *testing.T) {
|
||||||
jsonData := simplejson.NewFromAny(map[string]interface{}{
|
jsonData := &types.AzureClientSettings{
|
||||||
"azureAuthType": azcredentials.AzureAuthManagedIdentity,
|
AzureAuthType: azcredentials.AzureAuthManagedIdentity,
|
||||||
})
|
}
|
||||||
|
|
||||||
authType := getAuthType(cfg, jsonData)
|
authType := getAuthType(cfg, jsonData)
|
||||||
|
|
||||||
@ -67,9 +68,9 @@ func TestCredentials_getAuthType(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("should be client secret if datasource not configured", func(t *testing.T) {
|
t.Run("should be client secret if datasource not configured", func(t *testing.T) {
|
||||||
jsonData := simplejson.NewFromAny(map[string]interface{}{
|
jsonData := &types.AzureClientSettings{
|
||||||
"azureAuthType": "",
|
AzureAuthType: "",
|
||||||
})
|
}
|
||||||
|
|
||||||
authType := getAuthType(cfg, jsonData)
|
authType := getAuthType(cfg, jsonData)
|
||||||
|
|
||||||
@ -86,10 +87,10 @@ func TestCredentials_getAzureCloud(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
t.Run("when auth type is managed identity", func(t *testing.T) {
|
t.Run("when auth type is managed identity", func(t *testing.T) {
|
||||||
jsonData := simplejson.NewFromAny(map[string]interface{}{
|
jsonData := &types.AzureClientSettings{
|
||||||
"azureAuthType": azcredentials.AzureAuthManagedIdentity,
|
AzureAuthType: azcredentials.AzureAuthManagedIdentity,
|
||||||
"cloudName": azureMonitorUSGovernment,
|
CloudName: azureMonitorUSGovernment,
|
||||||
})
|
}
|
||||||
|
|
||||||
t.Run("should be from server configuration regardless of datasource value", func(t *testing.T) {
|
t.Run("should be from server configuration regardless of datasource value", func(t *testing.T) {
|
||||||
cloud, err := getAzureCloud(cfg, jsonData)
|
cloud, err := getAzureCloud(cfg, jsonData)
|
||||||
@ -114,10 +115,10 @@ func TestCredentials_getAzureCloud(t *testing.T) {
|
|||||||
|
|
||||||
t.Run("when auth type is client secret", func(t *testing.T) {
|
t.Run("when auth type is client secret", func(t *testing.T) {
|
||||||
t.Run("should be from datasource value normalized to known cloud name", func(t *testing.T) {
|
t.Run("should be from datasource value normalized to known cloud name", func(t *testing.T) {
|
||||||
jsonData := simplejson.NewFromAny(map[string]interface{}{
|
jsonData := &types.AzureClientSettings{
|
||||||
"azureAuthType": azcredentials.AzureAuthClientSecret,
|
AzureAuthType: azcredentials.AzureAuthClientSecret,
|
||||||
"cloudName": azureMonitorUSGovernment,
|
CloudName: azureMonitorUSGovernment,
|
||||||
})
|
}
|
||||||
|
|
||||||
cloud, err := getAzureCloud(cfg, jsonData)
|
cloud, err := getAzureCloud(cfg, jsonData)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
@ -126,10 +127,10 @@ func TestCredentials_getAzureCloud(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("should be from server configuration if not set in datasource", func(t *testing.T) {
|
t.Run("should be from server configuration if not set in datasource", func(t *testing.T) {
|
||||||
jsonData := simplejson.NewFromAny(map[string]interface{}{
|
jsonData := &types.AzureClientSettings{
|
||||||
"azureAuthType": azcredentials.AzureAuthClientSecret,
|
AzureAuthType: azcredentials.AzureAuthClientSecret,
|
||||||
"cloudName": "",
|
CloudName: "",
|
||||||
})
|
}
|
||||||
|
|
||||||
cloud, err := getAzureCloud(cfg, jsonData)
|
cloud, err := getAzureCloud(cfg, jsonData)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
@ -151,12 +152,12 @@ func TestCredentials_getAzureCredentials(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
t.Run("when auth type is managed identity", func(t *testing.T) {
|
t.Run("when auth type is managed identity", func(t *testing.T) {
|
||||||
jsonData := simplejson.NewFromAny(map[string]interface{}{
|
jsonData := &types.AzureClientSettings{
|
||||||
"azureAuthType": azcredentials.AzureAuthManagedIdentity,
|
AzureAuthType: azcredentials.AzureAuthManagedIdentity,
|
||||||
"cloudName": azureMonitorUSGovernment,
|
CloudName: azureMonitorUSGovernment,
|
||||||
"tenantId": "9b9d90ee-a5cc-49c2-b97e-0d1b0f086b5c",
|
TenantId: "9b9d90ee-a5cc-49c2-b97e-0d1b0f086b5c",
|
||||||
"clientId": "849ccbb0-92eb-4226-b228-ef391abd8fe6",
|
ClientId: "849ccbb0-92eb-4226-b228-ef391abd8fe6",
|
||||||
})
|
}
|
||||||
|
|
||||||
t.Run("should return managed identity credentials", func(t *testing.T) {
|
t.Run("should return managed identity credentials", func(t *testing.T) {
|
||||||
credentials, err := getAzureCredentials(cfg, jsonData, secureJsonData)
|
credentials, err := getAzureCredentials(cfg, jsonData, secureJsonData)
|
||||||
@ -170,26 +171,22 @@ func TestCredentials_getAzureCredentials(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("when auth type is client secret", func(t *testing.T) {
|
t.Run("when auth type is client secret", func(t *testing.T) {
|
||||||
jsonData := simplejson.NewFromAny(map[string]interface{}{
|
jsonData := &types.AzureClientSettings{
|
||||||
"azureAuthType": azcredentials.AzureAuthClientSecret,
|
AzureAuthType: azcredentials.AzureAuthClientSecret,
|
||||||
"cloudName": azUSGovManagement,
|
CloudName: azureMonitorUSGovernment,
|
||||||
"tenantId": "9b9d90ee-a5cc-49c2-b97e-0d1b0f086b5c",
|
TenantId: "9b9d90ee-a5cc-49c2-b97e-0d1b0f086b5c",
|
||||||
"clientId": "849ccbb0-92eb-4226-b228-ef391abd8fe6",
|
ClientId: "849ccbb0-92eb-4226-b228-ef391abd8fe6",
|
||||||
})
|
}
|
||||||
|
|
||||||
t.Run("should return client secret credentials", func(t *testing.T) {
|
t.Run("should return client secret credentials", func(t *testing.T) {
|
||||||
cfg := &setting.Cfg{
|
cfg := &setting.Cfg{}
|
||||||
Azure: &azsettings.AzureSettings{
|
|
||||||
Cloud: azsettings.AzureChina,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
credentials, err := getAzureCredentials(cfg, jsonData, secureJsonData)
|
credentials, err := getAzureCredentials(cfg, jsonData, secureJsonData)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.IsType(t, &azcredentials.AzureClientSecretCredentials{}, credentials)
|
require.IsType(t, &azcredentials.AzureClientSecretCredentials{}, credentials)
|
||||||
clientSecretCredentials := credentials.(*azcredentials.AzureClientSecretCredentials)
|
clientSecretCredentials := credentials.(*azcredentials.AzureClientSecretCredentials)
|
||||||
|
|
||||||
assert.Equal(t, azsettings.AzureChina, clientSecretCredentials.AzureCloud)
|
assert.Equal(t, azsettings.AzureUSGovernment, clientSecretCredentials.AzureCloud)
|
||||||
assert.Equal(t, "9b9d90ee-a5cc-49c2-b97e-0d1b0f086b5c", clientSecretCredentials.TenantId)
|
assert.Equal(t, "9b9d90ee-a5cc-49c2-b97e-0d1b0f086b5c", clientSecretCredentials.TenantId)
|
||||||
assert.Equal(t, "849ccbb0-92eb-4226-b228-ef391abd8fe6", clientSecretCredentials.ClientId)
|
assert.Equal(t, "849ccbb0-92eb-4226-b228-ef391abd8fe6", clientSecretCredentials.ClientId)
|
||||||
assert.Equal(t, "59e3498f-eb12-4943-b8f0-a5aa42640058", clientSecretCredentials.ClientSecret)
|
assert.Equal(t, "59e3498f-eb12-4943-b8f0-a5aa42640058", clientSecretCredentials.ClientSecret)
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package macros
|
package macros
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
@ -8,12 +9,10 @@ import (
|
|||||||
|
|
||||||
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
|
||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
"github.com/grafana/grafana/pkg/services/datasources"
|
|
||||||
"github.com/grafana/grafana/pkg/tsdb/azuremonitor/kinds/dataquery"
|
"github.com/grafana/grafana/pkg/tsdb/azuremonitor/kinds/dataquery"
|
||||||
"github.com/grafana/grafana/pkg/tsdb/azuremonitor/types"
|
"github.com/grafana/grafana/pkg/tsdb/azuremonitor/types"
|
||||||
"github.com/grafana/grafana/pkg/tsdb/legacydata/interval"
|
"github.com/grafana/grafana/pkg/tsdb/intervalv2"
|
||||||
)
|
)
|
||||||
|
|
||||||
const rsIdentifier = `__(timeFilter|timeFrom|timeTo|interval|contains|escapeMulti)`
|
const rsIdentifier = `__(timeFilter|timeFrom|timeTo|interval|contains|escapeMulti)`
|
||||||
@ -90,6 +89,11 @@ func (m *kqlMacroEngine) Interpolate(logger log.Logger, query backend.DataQuery,
|
|||||||
return kql, nil
|
return kql, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type interval struct {
|
||||||
|
IntervalMs int64
|
||||||
|
Interval string
|
||||||
|
}
|
||||||
|
|
||||||
func (m *kqlMacroEngine) evaluateMacro(logger log.Logger, name string, defaultTimeField string, args []string, dsInfo types.DatasourceInfo) (string, error) {
|
func (m *kqlMacroEngine) evaluateMacro(logger log.Logger, name string, defaultTimeField string, args []string, dsInfo types.DatasourceInfo) (string, error) {
|
||||||
switch name {
|
switch name {
|
||||||
case "timeFilter":
|
case "timeFilter":
|
||||||
@ -111,16 +115,22 @@ func (m *kqlMacroEngine) evaluateMacro(logger log.Logger, name string, defaultTi
|
|||||||
from := m.timeRange.From.UnixNano()
|
from := m.timeRange.From.UnixNano()
|
||||||
// default to "100 datapoints" if nothing in the query is more specific
|
// default to "100 datapoints" if nothing in the query is more specific
|
||||||
defaultInterval := time.Duration((to - from) / 60)
|
defaultInterval := time.Duration((to - from) / 60)
|
||||||
model, err := simplejson.NewJson(m.query.JSON)
|
var queryInterval interval
|
||||||
|
err := json.Unmarshal(m.query.JSON, &queryInterval)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Warn("Unable to parse model from query", "JSON", m.query.JSON)
|
logger.Warn("Unable to parse model from query", "JSON", m.query.JSON)
|
||||||
it = defaultInterval
|
it = defaultInterval
|
||||||
} else {
|
} else {
|
||||||
it, err = interval.GetIntervalFrom(&datasources.DataSource{
|
var (
|
||||||
JsonData: simplejson.NewFromAny(dsInfo.JSONData),
|
dsInterval string
|
||||||
}, model, defaultInterval)
|
ok bool
|
||||||
|
)
|
||||||
|
if dsInterval, ok = dsInfo.JSONData["interval"].(string); !ok {
|
||||||
|
dsInterval = ""
|
||||||
|
}
|
||||||
|
it, err = intervalv2.GetIntervalFrom(dsInterval, queryInterval.Interval, queryInterval.IntervalMs, defaultInterval)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Warn("Unable to get interval from query", "model", model)
|
logger.Warn("Unable to get interval from query", "model", queryInterval)
|
||||||
it = defaultInterval
|
it = defaultInterval
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,6 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
|
||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
"github.com/grafana/grafana/pkg/tsdb/azuremonitor/testdata"
|
"github.com/grafana/grafana/pkg/tsdb/azuremonitor/testdata"
|
||||||
azTime "github.com/grafana/grafana/pkg/tsdb/azuremonitor/time"
|
azTime "github.com/grafana/grafana/pkg/tsdb/azuremonitor/time"
|
||||||
@ -326,7 +325,7 @@ func TestAzureMonitorBuildQueries(t *testing.T) {
|
|||||||
azureMonitorQuery.URL = "/subscriptions/12345678-aaaa-bbbb-cccc-123456789abc/resourceGroups/grafanastaging/providers/Microsoft.Compute/virtualMachines/grafana/providers/microsoft.insights/metrics"
|
azureMonitorQuery.URL = "/subscriptions/12345678-aaaa-bbbb-cccc-123456789abc/resourceGroups/grafanastaging/providers/Microsoft.Compute/virtualMachines/grafana/providers/microsoft.insights/metrics"
|
||||||
}
|
}
|
||||||
|
|
||||||
if diff := cmp.Diff(azureMonitorQuery, queries[0], cmpopts.IgnoreUnexported(simplejson.Json{}), cmpopts.IgnoreFields(types.AzureMonitorQuery{}, "Params", "Dimensions")); diff != "" {
|
if diff := cmp.Diff(azureMonitorQuery, queries[0], cmpopts.IgnoreUnexported(struct{}{}), cmpopts.IgnoreFields(types.AzureMonitorQuery{}, "Params", "Dimensions")); diff != "" {
|
||||||
t.Errorf("Result mismatch (-want +got):\n%s", diff)
|
t.Errorf("Result mismatch (-want +got):\n%s", diff)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -353,7 +352,7 @@ func TestCustomNamespace(t *testing.T) {
|
|||||||
{
|
{
|
||||||
JSON: []byte(`{
|
JSON: []byte(`{
|
||||||
"azureMonitor": {
|
"azureMonitor": {
|
||||||
"customNamespace": "custom/namespace"
|
"customNamespace": "custom/namespace"
|
||||||
}
|
}
|
||||||
}`),
|
}`),
|
||||||
},
|
},
|
||||||
|
@ -5,7 +5,6 @@ import (
|
|||||||
|
|
||||||
"github.com/google/go-cmp/cmp"
|
"github.com/google/go-cmp/cmp"
|
||||||
"github.com/google/go-cmp/cmp/cmpopts"
|
"github.com/google/go-cmp/cmp/cmpopts"
|
||||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
|
||||||
"github.com/grafana/grafana/pkg/tsdb/azuremonitor/types"
|
"github.com/grafana/grafana/pkg/tsdb/azuremonitor/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -54,7 +53,7 @@ func TestDimensionFiltersMigration(t *testing.T) {
|
|||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
filters := MigrateDimensionFilters(tt.dimensionFilters)
|
filters := MigrateDimensionFilters(tt.dimensionFilters)
|
||||||
|
|
||||||
if diff := cmp.Diff(tt.expectedDimensionFilters, filters, cmpopts.IgnoreUnexported(simplejson.Json{})); diff != "" {
|
if diff := cmp.Diff(tt.expectedDimensionFilters, filters, cmpopts.IgnoreUnexported(struct{}{})); diff != "" {
|
||||||
t.Errorf("Result mismatch (-want +got):\n%s", diff)
|
t.Errorf("Result mismatch (-want +got):\n%s", diff)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -15,7 +15,6 @@ import (
|
|||||||
"github.com/grafana/grafana-plugin-sdk-go/data"
|
"github.com/grafana/grafana-plugin-sdk-go/data"
|
||||||
"go.opentelemetry.io/otel/attribute"
|
"go.opentelemetry.io/otel/attribute"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
|
||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
"github.com/grafana/grafana/pkg/infra/tracing"
|
"github.com/grafana/grafana/pkg/infra/tracing"
|
||||||
"github.com/grafana/grafana/pkg/tsdb/azuremonitor/kinds/dataquery"
|
"github.com/grafana/grafana/pkg/tsdb/azuremonitor/kinds/dataquery"
|
||||||
@ -139,15 +138,15 @@ func (e *AzureResourceGraphDatasource) executeQuery(ctx context.Context, logger
|
|||||||
dataResponse.Frames = frames
|
dataResponse.Frames = frames
|
||||||
return dataResponse
|
return dataResponse
|
||||||
}
|
}
|
||||||
|
var model dataquery.AzureMonitorQuery
|
||||||
model, err := simplejson.NewJson(query.JSON)
|
err := json.Unmarshal(query.JSON, &model)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
dataResponse.Error = err
|
dataResponse.Error = err
|
||||||
return dataResponse
|
return dataResponse
|
||||||
}
|
}
|
||||||
|
|
||||||
reqBody, err := json.Marshal(map[string]interface{}{
|
reqBody, err := json.Marshal(map[string]interface{}{
|
||||||
"subscriptions": model.Get("subscriptions").MustStringArray(),
|
"subscriptions": model.Subscriptions,
|
||||||
"query": query.InterpolatedQuery,
|
"query": query.InterpolatedQuery,
|
||||||
"options": map[string]string{"resultFormat": "table"},
|
"options": map[string]string{"resultFormat": "table"},
|
||||||
})
|
})
|
||||||
|
@ -17,7 +17,6 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
|
||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
"github.com/grafana/grafana/pkg/tsdb/azuremonitor/loganalytics"
|
"github.com/grafana/grafana/pkg/tsdb/azuremonitor/loganalytics"
|
||||||
"github.com/grafana/grafana/pkg/tsdb/azuremonitor/types"
|
"github.com/grafana/grafana/pkg/tsdb/azuremonitor/types"
|
||||||
@ -77,7 +76,7 @@ func TestBuildingAzureResourceGraphQueries(t *testing.T) {
|
|||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
queries, err := datasource.buildQueries(logger, tt.queryModel, types.DatasourceInfo{})
|
queries, err := datasource.buildQueries(logger, tt.queryModel, types.DatasourceInfo{})
|
||||||
tt.Err(t, err)
|
tt.Err(t, err)
|
||||||
if diff := cmp.Diff(tt.azureResourceGraphQueries, queries, cmpopts.IgnoreUnexported(simplejson.Json{})); diff != "" {
|
if diff := cmp.Diff(tt.azureResourceGraphQueries, queries, cmpopts.IgnoreUnexported(struct{}{})); diff != "" {
|
||||||
t.Errorf("Result mismatch (-want +got):\n%s", diff)
|
t.Errorf("Result mismatch (-want +got):\n%s", diff)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -30,12 +30,24 @@ type AzRoute struct {
|
|||||||
Headers map[string]string
|
Headers map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type AzureSettings struct {
|
||||||
|
AzureMonitorSettings
|
||||||
|
AzureClientSettings
|
||||||
|
}
|
||||||
|
|
||||||
type AzureMonitorSettings struct {
|
type AzureMonitorSettings struct {
|
||||||
SubscriptionId string `json:"subscriptionId"`
|
SubscriptionId string `json:"subscriptionId"`
|
||||||
LogAnalyticsDefaultWorkspace string `json:"logAnalyticsDefaultWorkspace"`
|
LogAnalyticsDefaultWorkspace string `json:"logAnalyticsDefaultWorkspace"`
|
||||||
AppInsightsAppId string `json:"appInsightsAppId"`
|
AppInsightsAppId string `json:"appInsightsAppId"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type AzureClientSettings struct {
|
||||||
|
AzureAuthType string
|
||||||
|
CloudName string
|
||||||
|
TenantId string
|
||||||
|
ClientId string
|
||||||
|
}
|
||||||
|
|
||||||
// AzureMonitorCustomizedCloudSettings is the extended Azure Monitor settings for customized cloud
|
// AzureMonitorCustomizedCloudSettings is the extended Azure Monitor settings for customized cloud
|
||||||
type AzureMonitorCustomizedCloudSettings struct {
|
type AzureMonitorCustomizedCloudSettings struct {
|
||||||
CustomizedRoutes map[string]AzRoute `json:"customizedRoutes"`
|
CustomizedRoutes map[string]AzRoute `json:"customizedRoutes"`
|
||||||
|
Loading…
Reference in New Issue
Block a user