mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
AzureMonitor: Fix mishandled resources vs workspaces (#83184)
This commit is contained in:
parent
eb25b669c6
commit
facb19fefb
@ -485,11 +485,19 @@ func (e *AzureLogAnalyticsDatasource) createRequest(ctx context.Context, queryUR
|
||||
}
|
||||
|
||||
if len(query.Resources) > 1 && query.QueryType == dataquery.AzureQueryTypeAzureLogAnalytics && !query.AppInsightsQuery {
|
||||
body["workspaces"] = query.Resources
|
||||
str := strings.ToLower(query.Resources[0])
|
||||
|
||||
if strings.Contains(str, "microsoft.operationalinsights/workspaces") {
|
||||
body["workspaces"] = query.Resources
|
||||
} else {
|
||||
body["resources"] = query.Resources
|
||||
}
|
||||
}
|
||||
|
||||
if query.AppInsightsQuery {
|
||||
body["applications"] = query.Resources
|
||||
}
|
||||
|
||||
jsonValue, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%v: %w", "failed to create request", err)
|
||||
@ -499,6 +507,7 @@ func (e *AzureLogAnalyticsDatasource) createRequest(ctx context.Context, queryUR
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%v: %w", "failed to create request", err)
|
||||
}
|
||||
|
||||
req.URL.Path = "/"
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.URL.Path = path.Join(req.URL.Path, query.URL)
|
||||
|
@ -1553,6 +1553,42 @@ func TestLogAnalyticsCreateRequest(t *testing.T) {
|
||||
t.Errorf("Unexpected Body: %v", cmp.Diff(string(body), expectedBody))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("correctly classifies resources as workspaces when matching criteria", func(t *testing.T) {
|
||||
ds := AzureLogAnalyticsDatasource{}
|
||||
req, err := ds.createRequest(ctx, url, &AzureLogAnalyticsQuery{
|
||||
Resources: []string{"/subscriptions/test-sub/resourceGroups/test-rg/providers/microsoft.operationalInsights/workSpaces/ws1", "microsoft.operationalInsights/workspaces/ws2"}, // Note different casings and partial paths
|
||||
Query: "Perf",
|
||||
QueryType: dataquery.AzureQueryTypeAzureLogAnalytics,
|
||||
AppInsightsQuery: false,
|
||||
DashboardTime: false,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
expectedBody := `{"query":"Perf","workspaces":["/subscriptions/test-sub/resourceGroups/test-rg/providers/microsoft.operationalInsights/workSpaces/ws1","microsoft.operationalInsights/workspaces/ws2"]}` // Expecting resources to be classified as workspaces
|
||||
body, err := io.ReadAll(req.Body)
|
||||
require.NoError(t, err)
|
||||
if !cmp.Equal(string(body), expectedBody) {
|
||||
t.Errorf("Unexpected Body: %v", cmp.Diff(string(body), expectedBody))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("correctly passes multiple resources not classified as workspaces", func(t *testing.T) {
|
||||
ds := AzureLogAnalyticsDatasource{}
|
||||
req, err := ds.createRequest(ctx, url, &AzureLogAnalyticsQuery{
|
||||
Resources: []string{"/subscriptions/test-sub/resourceGroups/test-rg/providers/SomeOtherService/serviceInstances/r1", "/subscriptions/test-sub/resourceGroups/test-rg/providers/SomeOtherService/serviceInstances/r2"},
|
||||
Query: "Perf",
|
||||
QueryType: dataquery.AzureQueryTypeAzureLogAnalytics,
|
||||
AppInsightsQuery: false,
|
||||
DashboardTime: false,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
expectedBody := `{"query":"Perf","resources":["/subscriptions/test-sub/resourceGroups/test-rg/providers/SomeOtherService/serviceInstances/r1","/subscriptions/test-sub/resourceGroups/test-rg/providers/SomeOtherService/serviceInstances/r2"]}`
|
||||
body, err := io.ReadAll(req.Body)
|
||||
require.NoError(t, err)
|
||||
if !cmp.Equal(string(body), expectedBody) {
|
||||
t.Errorf("Unexpected Body: %v", cmp.Diff(string(body), expectedBody))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func Test_executeQueryErrorWithDifferentLogAnalyticsCreds(t *testing.T) {
|
||||
|
Loading…
Reference in New Issue
Block a user