From 412bcf1fb0e376f0148dbf7b58aa56ecb0a3ba61 Mon Sep 17 00:00:00 2001 From: Sergey Kostrukov Date: Fri, 4 Jun 2021 04:29:18 -0700 Subject: [PATCH] AzureMonitor: fix Azure Resource Graph queries in Azure China (#35235) * Optimize Azure Monitor routes * Update API version compatible with Azure China --- .../azure-resource-graph-datasource.go | 2 +- pkg/tsdb/azuremonitor/routes.go | 16 -- .../api/routes.ts | 18 +-- .../plugin.json | 150 +++--------------- .../resourcePicker/resourcePickerData.ts | 6 +- 5 files changed, 30 insertions(+), 162 deletions(-) diff --git a/pkg/tsdb/azuremonitor/azure-resource-graph-datasource.go b/pkg/tsdb/azuremonitor/azure-resource-graph-datasource.go index 1ab25b0d85d..1422533a852 100644 --- a/pkg/tsdb/azuremonitor/azure-resource-graph-datasource.go +++ b/pkg/tsdb/azuremonitor/azure-resource-graph-datasource.go @@ -226,7 +226,7 @@ func (e *AzureResourceGraphDatasource) getPluginRoute(plugin *plugins.DataSource return nil, "", err } - routeName, err := getResourceGraphApiRoute(cloud) + routeName, err := getManagementApiRoute(cloud) if err != nil { return nil, "", err } diff --git a/pkg/tsdb/azuremonitor/routes.go b/pkg/tsdb/azuremonitor/routes.go index 029c44fdd9d..44161948fe7 100644 --- a/pkg/tsdb/azuremonitor/routes.go +++ b/pkg/tsdb/azuremonitor/routes.go @@ -43,19 +43,3 @@ func getAppInsightsApiRoute(azureCloud string) (string, error) { return "", err } } - -func getResourceGraphApiRoute(azureCloud string) (string, error) { - switch azureCloud { - case azureMonitorPublic: - return "azureresourcegraph", nil - case azureMonitorChina: - return "chinaazureresourcegraph", nil - case azureMonitorUSGovernment: - return "govazureresourcegraph", nil - case azureMonitorGermany: - return "germanyazureresourcegraph", nil - default: - err := fmt.Errorf("the cloud '%s' not supported", azureCloud) - return "", err - } -} diff --git a/public/app/plugins/datasource/grafana-azure-monitor-datasource/api/routes.ts b/public/app/plugins/datasource/grafana-azure-monitor-datasource/api/routes.ts index 89a8e4ef65c..ffd4bcb17aa 100644 --- a/public/app/plugins/datasource/grafana-azure-monitor-datasource/api/routes.ts +++ b/public/app/plugins/datasource/grafana-azure-monitor-datasource/api/routes.ts @@ -21,6 +21,8 @@ export function getLogAnalyticsManagementApiRoute(azureCloud: string): string { return 'chinaworkspacesloganalytics'; case 'govazuremonitor': return 'govworkspacesloganalytics'; + case 'germanyazuremonitor': + return 'germanyworkspacesloganalytics'; default: throw new Error('The cloud not supported.'); } @@ -49,19 +51,3 @@ export function getAppInsightsApiRoute(azureCloud: string): string { throw new Error('The cloud not supported.'); } } - -export function getLogAnalyticsResourcePickerApiRoute(azureCloud: string) { - switch (azureCloud) { - case 'azuremonitor': - return 'loganalytics-resourcepickerdata'; - - case 'govazuremonitor': // Azure US Government - return 'loganalytics-resourcepickerdata-gov'; - - case 'chinaazuremonitor': // Azure China - return 'loganalytics-resourcepickerdata-china'; - - default: - throw new Error('The cloud not supported.'); - } -} diff --git a/public/app/plugins/datasource/grafana-azure-monitor-datasource/plugin.json b/public/app/plugins/datasource/grafana-azure-monitor-datasource/plugin.json index a73d6d1b58e..df9cde8dabf 100644 --- a/public/app/plugins/datasource/grafana-azure-monitor-datasource/plugin.json +++ b/public/app/plugins/datasource/grafana-azure-monitor-datasource/plugin.json @@ -31,7 +31,7 @@ "routes": [ { "path": "azuremonitor", - "method": "GET", + "method": "*", "url": "https://management.azure.com", "authType": "azure", "tokenAuth": { @@ -48,7 +48,7 @@ }, { "path": "govazuremonitor", - "method": "GET", + "method": "*", "url": "https://management.usgovcloudapi.net", "authType": "azure", "tokenAuth": { @@ -65,7 +65,7 @@ }, { "path": "germanyazuremonitor", - "method": "GET", + "method": "*", "url": "https://management.microsoftazure.de", "authType": "azure", "tokenAuth": { @@ -82,7 +82,7 @@ }, { "path": "chinaazuremonitor", - "method": "GET", + "method": "*", "url": "https://management.chinacloudapi.cn", "authType": "azure", "tokenAuth": { @@ -97,74 +97,6 @@ }, "headers": [{ "name": "x-ms-app", "content": "Grafana" }] }, - { - "path": "azureresourcegraph", - "method": "POST", - "url": "https://management.azure.com", - "authType": "azure", - "tokenAuth": { - "scopes": ["https://management.azure.com/.default"], - "params": { - "azure_auth_type": "{{.JsonData.azureAuthType | orEmpty}}", - "azure_cloud": "AzureCloud", - "tenant_id": "{{.JsonData.tenantId | orEmpty}}", - "client_id": "{{.JsonData.clientId | orEmpty}}", - "client_secret": "{{.SecureJsonData.clientSecret | orEmpty}}" - } - }, - "headers": [{ "name": "x-ms-app", "content": "Grafana" }] - }, - { - "path": "chinaazureresourcegraph", - "method": "POST", - "url": "https://management.azure.com", - "authType": "azure", - "tokenAuth": { - "scopes": ["https://management.chinacloudapi.cn/.default"], - "params": { - "azure_auth_type": "{{.JsonData.azureAuthType | orEmpty}}", - "azure_cloud": "AzureChinaCloud", - "tenant_id": "{{.JsonData.tenantId | orEmpty}}", - "client_id": "{{.JsonData.clientId | orEmpty}}", - "client_secret": "{{.SecureJsonData.clientSecret | orEmpty}}" - } - }, - "headers": [{ "name": "x-ms-app", "content": "Grafana" }] - }, - { - "path": "govazureresourcegraph", - "method": "POST", - "url": "https://management.usgovcloudapi.net", - "authType": "azure", - "tokenAuth": { - "scopes": ["https://management.usgovcloudapi.net/.default"], - "params": { - "azure_auth_type": "{{.JsonData.azureAuthType | orEmpty}}", - "azure_cloud": "AzureUSGovernment", - "tenant_id": "{{.JsonData.tenantId | orEmpty}}", - "client_id": "{{.JsonData.clientId | orEmpty}}", - "client_secret": "{{.SecureJsonData.clientSecret | orEmpty}}" - } - }, - "headers": [{ "name": "x-ms-app", "content": "Grafana" }] - }, - { - "path": "germanyazureresourcegraph", - "method": "POST", - "url": "https://management.microsoftazure.de", - "authType": "azure", - "tokenAuth": { - "scopes": ["https://management.microsoftazure.de/.default"], - "params": { - "azure_auth_type": "{{.JsonData.azureAuthType | orEmpty}}", - "azure_cloud": "AzureGermanCloud", - "tenant_id": "{{.JsonData.tenantId | orEmpty}}", - "client_id": "{{.JsonData.clientId | orEmpty}}", - "client_secret": "{{.SecureJsonData.clientSecret | orEmpty}}" - } - }, - "headers": [{ "name": "x-ms-app", "content": "Grafana" }] - }, { "path": "appinsights", "method": "GET", @@ -183,60 +115,9 @@ { "name": "x-ms-app", "content": "Grafana" } ] }, - { - "path": "loganalytics-resourcepickerdata", - "method": "POST", - "url": "https://management.azure.com", - "authType": "azure", - "tokenAuth": { - "scopes": ["https://management.azure.com/.default"], - "params": { - "azure_auth_type": "{{.JsonData.azureAuthType | orEmpty}}", - "azure_cloud": "AzureCloud", - "tenant_id": "{{.JsonData.logAnalyticsTenantId | orEmpty}}", - "client_id": "{{.JsonData.logAnalyticsClientId | orEmpty}}", - "client_secret": "{{.SecureJsonData.logAnalyticsClientSecret | orEmpty}}" - } - }, - "headers": [{ "name": "x-ms-app", "content": "Grafana" }] - }, - { - "path": "loganalytics-resourcepickerdata-china", - "method": "POST", - "url": "https://management.chinacloudapi.cn", - "authType": "azure", - "tokenAuth": { - "scopes": ["https://management.chinacloudapi.cn/.default"], - "params": { - "azure_auth_type": "{{.JsonData.azureAuthType | orEmpty}}", - "azure_cloud": "AzureChinaCloud", - "tenant_id": "{{.JsonData.logAnalyticsTenantId | orEmpty}}", - "client_id": "{{.JsonData.logAnalyticsClientId | orEmpty}}", - "client_secret": "{{.SecureJsonData.logAnalyticsClientSecret | orEmpty}}" - } - }, - "headers": [{ "name": "x-ms-app", "content": "Grafana" }] - }, - { - "path": "loganalytics-resourcepickerdata-gov", - "method": "POST", - "url": "https://management.usgovcloudapi.net", - "authType": "azure", - "tokenAuth": { - "scopes": ["https://management.usgovcloudapi.net/.default"], - "params": { - "azure_auth_type": "{{.JsonData.azureAuthType | orEmpty}}", - "azure_cloud": "AzureUSGovernment", - "tenant_id": "{{.JsonData.logAnalyticsTenantId | orEmpty}}", - "client_id": "{{.JsonData.logAnalyticsClientId | orEmpty}}", - "client_secret": "{{.SecureJsonData.logAnalyticsClientSecret | orEmpty}}" - } - }, - "headers": [{ "name": "x-ms-app", "content": "Grafana" }] - }, { "path": "workspacesloganalytics", - "method": "GET", + "method": "*", "url": "https://management.azure.com", "authType": "azure", "tokenAuth": { @@ -253,7 +134,7 @@ }, { "path": "chinaworkspacesloganalytics", - "method": "GET", + "method": "*", "url": "https://management.chinacloudapi.cn", "authType": "azure", "tokenAuth": { @@ -270,7 +151,7 @@ }, { "path": "govworkspacesloganalytics", - "method": "GET", + "method": "*", "url": "https://management.usgovcloudapi.net", "authType": "azure", "tokenAuth": { @@ -285,6 +166,23 @@ }, "headers": [{ "name": "x-ms-app", "content": "Grafana" }] }, + { + "path": "germanyworkspacesloganalytics", + "method": "*", + "url": "https://management.microsoftazure.de", + "authType": "azure", + "tokenAuth": { + "scopes": ["https://management.microsoftazure.de/.default"], + "params": { + "azure_auth_type": "{{.JsonData.azureAuthType | orEmpty}}", + "azure_cloud": "AzureUSGovernment", + "tenant_id": "{{.JsonData.logAnalyticsTenantId | orEmpty}}", + "client_id": "{{.JsonData.logAnalyticsClientId | orEmpty}}", + "client_secret": "{{.SecureJsonData.logAnalyticsClientSecret | orEmpty}}" + } + }, + "headers": [{ "name": "x-ms-app", "content": "Grafana" }] + }, { "path": "loganalyticsazure", "method": "GET", diff --git a/public/app/plugins/datasource/grafana-azure-monitor-datasource/resourcePicker/resourcePickerData.ts b/public/app/plugins/datasource/grafana-azure-monitor-datasource/resourcePicker/resourcePickerData.ts index d8e24f16efc..77cba8b7bfe 100644 --- a/public/app/plugins/datasource/grafana-azure-monitor-datasource/resourcePicker/resourcePickerData.ts +++ b/public/app/plugins/datasource/grafana-azure-monitor-datasource/resourcePicker/resourcePickerData.ts @@ -1,5 +1,5 @@ import { FetchResponse, getBackendSrv } from '@grafana/runtime'; -import { getLogAnalyticsResourcePickerApiRoute } from '../api/routes'; +import { getLogAnalyticsManagementApiRoute } from '../api/routes'; import { locationDisplayNames, logsSupportedLocationsKusto, @@ -17,7 +17,7 @@ import { RawAzureResourceItem, } from '../types'; -const RESOURCE_GRAPH_URL = '/providers/Microsoft.ResourceGraph/resources?api-version=2020-04-01-preview'; +const RESOURCE_GRAPH_URL = '/providers/Microsoft.ResourceGraph/resources?api-version=2021-03-01'; export default class ResourcePickerData { private proxyUrl: string; @@ -148,7 +148,7 @@ export default class ResourcePickerData { try { return await getBackendSrv() .fetch>({ - url: this.proxyUrl + '/' + getLogAnalyticsResourcePickerApiRoute(this.cloud) + RESOURCE_GRAPH_URL, + url: this.proxyUrl + '/' + getLogAnalyticsManagementApiRoute(this.cloud) + RESOURCE_GRAPH_URL, method: 'POST', data: { query: query,