diff --git a/go.mod b/go.mod index 46db1203e84..72f75eee7b7 100644 --- a/go.mod +++ b/go.mod @@ -56,7 +56,7 @@ require ( github.com/gosimple/slug v1.12.0 github.com/grafana/cuetsy v0.1.1 github.com/grafana/grafana-aws-sdk v0.11.0 - github.com/grafana/grafana-azure-sdk-go v1.3.0 + github.com/grafana/grafana-azure-sdk-go v1.3.1 github.com/grafana/grafana-plugin-sdk-go v0.139.0 github.com/grafana/thema v0.0.0-20220817114012-ebeee841c104 github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 diff --git a/go.sum b/go.sum index 519b3771fb6..578cd6272d7 100644 --- a/go.sum +++ b/go.sum @@ -1378,6 +1378,8 @@ github.com/grafana/grafana-aws-sdk v0.11.0 h1:ncPD/UN0wNcKq3kEU90RdvrnK/6R4VW2Lo github.com/grafana/grafana-aws-sdk v0.11.0/go.mod h1:5Iw3xY7iXJfNaYHrRHMXa/kaB2lWoyntg71PPLGvSs8= github.com/grafana/grafana-azure-sdk-go v1.3.0 h1:zboQpq/ljBjqHo/6UQNZAUwqGTtnEGRYSEnqIQvLuAo= github.com/grafana/grafana-azure-sdk-go v1.3.0/go.mod h1:rgrnK9m6CgKlgx4rH3FFP/6dTdyRO6LYC2mVZov35yo= +github.com/grafana/grafana-azure-sdk-go v1.3.1 h1:xTgBmbDxUPj3X9Pl9vgIOgZoDdtxWl0fYDuHrHr79jM= +github.com/grafana/grafana-azure-sdk-go v1.3.1/go.mod h1:rgrnK9m6CgKlgx4rH3FFP/6dTdyRO6LYC2mVZov35yo= github.com/grafana/grafana-google-sdk-go v0.0.0-20211104130251-b190293eaf58 h1:2ud7NNM7LrGPO4x0NFR8qLq68CqI4SmB7I2yRN2w9oE= github.com/grafana/grafana-google-sdk-go v0.0.0-20211104130251-b190293eaf58/go.mod h1:Vo2TKWfDVmNTELBUM+3lkrZvFtBws0qSZdXhQxRdJrE= github.com/grafana/grafana-plugin-sdk-go v0.114.0/go.mod h1:D7x3ah+1d4phNXpbnOaxa/osSaZlwh9/ZUnGGzegRbk= diff --git a/pkg/tsdb/azuremonitor/azuremonitor.go b/pkg/tsdb/azuremonitor/azuremonitor.go index 24836ac0828..35c90ab33da 100644 --- a/pkg/tsdb/azuremonitor/azuremonitor.go +++ b/pkg/tsdb/azuremonitor/azuremonitor.go @@ -10,6 +10,7 @@ import ( "net/http" "strconv" + "github.com/grafana/grafana-azure-sdk-go/azsettings" "github.com/grafana/grafana-plugin-sdk-go/backend" "github.com/grafana/grafana-plugin-sdk-go/backend/datasource" "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient" @@ -99,6 +100,11 @@ func NewInstanceSettings(cfg *setting.Cfg, clientProvider *httpclient.Provider, return nil, fmt.Errorf("error getting credentials: %w", err) } + routesForModel, err := getAzureRoutes(cloud, settings.JSONData) + if err != nil { + return nil, err + } + credentials, err := getAzureCredentials(cfg, jsonData, settings.DecryptedSecureJSONData) if err != nil { return nil, fmt.Errorf("error getting credentials: %w", err) @@ -111,7 +117,7 @@ func NewInstanceSettings(cfg *setting.Cfg, clientProvider *httpclient.Provider, JSONData: jsonDataObj, DecryptedSecureJSONData: settings.DecryptedSecureJSONData, DatasourceID: settings.ID, - Routes: routes[cloud], + Routes: routesForModel, Services: map[string]types.DatasourceService{}, } @@ -127,6 +133,31 @@ func NewInstanceSettings(cfg *setting.Cfg, clientProvider *httpclient.Provider, } } +func getCustomizedCloudSettings(cloud string, jsonData json.RawMessage) (types.AzureMonitorCustomizedCloudSettings, error) { + customizedCloudSettings := types.AzureMonitorCustomizedCloudSettings{} + err := json.Unmarshal(jsonData, &customizedCloudSettings) + if err != nil { + return types.AzureMonitorCustomizedCloudSettings{}, fmt.Errorf("error getting customized cloud settings: %w", err) + } + return customizedCloudSettings, nil +} + +func getAzureRoutes(cloud string, jsonData json.RawMessage) (map[string]types.AzRoute, error) { + if cloud == azsettings.AzureCustomized { + customizedCloudSettings, err := getCustomizedCloudSettings(cloud, jsonData) + if err != nil { + return nil, err + } + if customizedCloudSettings.CustomizedRoutes == nil { + return nil, fmt.Errorf("unable to instantiate routes, customizedRoutes must be set") + } + azureRoutes := customizedCloudSettings.CustomizedRoutes + return azureRoutes, nil + } else { + return routes[cloud], nil + } +} + type azDatasourceExecutor interface { ExecuteTimeSeriesQuery(ctx context.Context, originalQueries []backend.DataQuery, dsInfo types.DatasourceInfo, client *http.Client, url string, tracer tracing.Tracer) (*backend.QueryDataResponse, error) ResourceRequest(rw http.ResponseWriter, req *http.Request, cli *http.Client) diff --git a/pkg/tsdb/azuremonitor/azuremonitor_test.go b/pkg/tsdb/azuremonitor/azuremonitor_test.go index 98621dfb0f0..8f9218bec95 100644 --- a/pkg/tsdb/azuremonitor/azuremonitor_test.go +++ b/pkg/tsdb/azuremonitor/azuremonitor_test.go @@ -51,6 +51,40 @@ func TestNewInstanceSettings(t *testing.T) { }, Err: require.NoError, }, + { + name: "creates an instance for customized cloud", + settings: backend.DataSourceInstanceSettings{ + JSONData: []byte(`{"cloudName":"customizedazuremonitor","customizedRoutes":{"Route":{"URL":"url"}},"azureAuthType":"clientsecret"}`), + DecryptedSecureJSONData: map[string]string{"clientSecret": "secret"}, + ID: 50, + }, + expectedModel: types.DatasourceInfo{ + Cloud: "AzureCustomizedCloud", + Credentials: &azcredentials.AzureClientSecretCredentials{ + AzureCloud: "AzureCustomizedCloud", + ClientSecret: "secret", + }, + Settings: types.AzureMonitorSettings{}, + Routes: map[string]types.AzRoute{ + "Route": { + URL: "url", + }, + }, + JSONData: map[string]interface{}{ + "azureAuthType": "clientsecret", + "cloudName": "customizedazuremonitor", + "customizedRoutes": map[string]interface{}{ + "Route": map[string]interface{}{ + "URL": "url", + }, + }, + }, + DatasourceID: 50, + DecryptedSecureJSONData: map[string]string{"clientSecret": "secret"}, + Services: map[string]types.DatasourceService{}, + }, + Err: require.NoError, + }, } cfg := &setting.Cfg{ diff --git a/pkg/tsdb/azuremonitor/credentials.go b/pkg/tsdb/azuremonitor/credentials.go index b60b3733467..5a227accc22 100644 --- a/pkg/tsdb/azuremonitor/credentials.go +++ b/pkg/tsdb/azuremonitor/credentials.go @@ -16,6 +16,7 @@ const ( azureMonitorChina = "chinaazuremonitor" azureMonitorUSGovernment = "govazuremonitor" azureMonitorGermany = "germanyazuremonitor" + azureMonitorCustomized = "customizedazuremonitor" ) func getAuthType(cfg *setting.Cfg, jsonData *simplejson.Json) string { @@ -53,6 +54,8 @@ func getDefaultAzureCloud(cfg *setting.Cfg) (string, error) { return azsettings.AzureUSGovernment, nil case azsettings.AzureGermany: return azsettings.AzureGermany, nil + case azsettings.AzureCustomized: + return azsettings.AzureCustomized, nil case "": // Not set cloud defaults to public return azsettings.AzurePublic, nil @@ -72,6 +75,8 @@ func normalizeAzureCloud(cloudName string) (string, error) { return azsettings.AzureUSGovernment, nil case azureMonitorGermany: return azsettings.AzureGermany, nil + case azureMonitorCustomized: + return azsettings.AzureCustomized, nil default: err := fmt.Errorf("the cloud '%s' not supported", cloudName) return "", err diff --git a/pkg/tsdb/azuremonitor/types/types.go b/pkg/tsdb/azuremonitor/types/types.go index ff0d868991d..8193a7b3f7d 100644 --- a/pkg/tsdb/azuremonitor/types/types.go +++ b/pkg/tsdb/azuremonitor/types/types.go @@ -33,6 +33,11 @@ type AzureMonitorSettings struct { AppInsightsAppId string `json:"appInsightsAppId"` } +// AzureMonitorCustomizedCloudSettings is the extended Azure Monitor settings for customized cloud +type AzureMonitorCustomizedCloudSettings struct { + CustomizedRoutes map[string]AzRoute `json:"customizedRoutes"` +} + type DatasourceService struct { URL string HTTPClient *http.Client