From 5b1ff83ee9c527c83439f0ac52d74d9b0df6d3c1 Mon Sep 17 00:00:00 2001 From: Andres Martinez Gotor Date: Tue, 22 Nov 2022 16:43:57 +0100 Subject: [PATCH] Azure Monitor: Fix resource picker selection for subresources (#56392) --- .../components/ResourcePicker/utils.test.ts | 63 +++++++++++++------ .../components/ResourcePicker/utils.ts | 27 ++++++-- 2 files changed, 66 insertions(+), 24 deletions(-) diff --git a/public/app/plugins/datasource/grafana-azure-monitor-datasource/components/ResourcePicker/utils.test.ts b/public/app/plugins/datasource/grafana-azure-monitor-datasource/components/ResourcePicker/utils.test.ts index b86486f1563..57083d316fd 100644 --- a/public/app/plugins/datasource/grafana-azure-monitor-datasource/components/ResourcePicker/utils.test.ts +++ b/public/app/plugins/datasource/grafana-azure-monitor-datasource/components/ResourcePicker/utils.test.ts @@ -6,16 +6,16 @@ import { findRow, parseResourceURI, setResource } from './utils'; describe('AzureMonitor ResourcePicker utils', () => { describe('parseResourceURI', () => { it('should parse subscription URIs', () => { - expect(parseResourceURI('/subscriptions/44693801-6ee6-49de-9b2d-9106972f9572')).toEqual({ - subscription: '44693801-6ee6-49de-9b2d-9106972f9572', + expect(parseResourceURI('/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee')).toEqual({ + subscription: 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee', }); }); it('should parse resource group URIs', () => { expect( - parseResourceURI('/subscriptions/44693801-6ee6-49de-9b2d-9106972f9572/resourceGroups/cloud-datasources') + parseResourceURI('/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/cloud-datasources') ).toEqual({ - subscription: '44693801-6ee6-49de-9b2d-9106972f9572', + subscription: 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee', resourceGroup: 'cloud-datasources', }); }); @@ -23,10 +23,10 @@ describe('AzureMonitor ResourcePicker utils', () => { it('should parse resource URIs', () => { expect( parseResourceURI( - '/subscriptions/44693801-6ee6-49de-9b2d-9106972f9572/resourceGroups/cloud-datasources/providers/Microsoft.Compute/virtualMachines/GithubTestDataVM' + '/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/cloud-datasources/providers/Microsoft.Compute/virtualMachines/GithubTestDataVM' ) ).toEqual({ - subscription: '44693801-6ee6-49de-9b2d-9106972f9572', + subscription: 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee', resourceGroup: 'cloud-datasources', metricNamespace: 'Microsoft.Compute/virtualMachines', resourceName: 'GithubTestDataVM', @@ -36,10 +36,10 @@ describe('AzureMonitor ResourcePicker utils', () => { it('should parse resource URIs with a subresource', () => { expect( parseResourceURI( - '/subscriptions/44693801-6ee6-49de-9b2d-9106972f9572/resourceGroups/cloud-datasources/providers/Microsoft.Storage/storageAccounts/csb100320016c43d2d0/fileServices/default' + '/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/cloud-datasources/providers/Microsoft.Storage/storageAccounts/csb100320016c43d2d0/fileServices/default' ) ).toEqual({ - subscription: '44693801-6ee6-49de-9b2d-9106972f9572', + subscription: 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee', resourceGroup: 'cloud-datasources', metricNamespace: 'Microsoft.Storage/storageAccounts/fileServices', resourceName: 'csb100320016c43d2d0/default', @@ -47,30 +47,30 @@ describe('AzureMonitor ResourcePicker utils', () => { }); it('returns undefined for invalid input', () => { - expect(parseResourceURI('44693801-6ee6-49de-9b2d-9106972f9572')).toEqual({}); + expect(parseResourceURI('aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee')).toEqual({}); }); it('returns a valid response with a missing element in the metric namespace and name', () => { expect( parseResourceURI( - '/subscriptions/44693801-6ee6-49de-9b2d-9106972f9572/resourceGroups/cloud-datasources/providers/foo' + '/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/cloud-datasources/providers/foo' ) ).toEqual({ metricNamespace: 'foo', resourceGroup: 'cloud-datasources', resourceName: '', - subscription: '44693801-6ee6-49de-9b2d-9106972f9572', + subscription: 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee', }); expect( parseResourceURI( - '/subscriptions/44693801-6ee6-49de-9b2d-9106972f9572/resourceGroups/cloud-datasources/providers/foo/bar' + '/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/cloud-datasources/providers/foo/bar' ) ).toEqual({ metricNamespace: 'foo/bar', resourceGroup: 'cloud-datasources', resourceName: '', - subscription: '44693801-6ee6-49de-9b2d-9106972f9572', + subscription: 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee', }); }); }); @@ -87,7 +87,7 @@ describe('AzureMonitor ResourcePicker utils', () => { const rows: ResourceRowGroup = [ { id: '', - uri: '/subscriptions/44693801-6ee6-49de-9b2d-9106972f9572/resourceGroups/cloud-datasources/providers/Microsoft.Storage/storageAccounts/csb100320016c43d2d0', + uri: '/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/cloud-datasources/providers/Microsoft.Storage/storageAccounts/csb100320016c43d2d0', name: '', type: ResourceRowType.Resource, typeLabel: '', @@ -96,7 +96,7 @@ describe('AzureMonitor ResourcePicker utils', () => { expect( findRow( rows, - '/subscriptions/44693801-6ee6-49de-9b2d-9106972f9572/resourceGroups/cloud-datasources/providers/Microsoft.Storage/storageAccounts/csb100320016c43d2d0/fileServices/default' + '/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/cloud-datasources/providers/Microsoft.Storage/storageAccounts/csb100320016c43d2d0/fileServices/default' ) ).toEqual(rows[0]); }); @@ -105,7 +105,7 @@ describe('AzureMonitor ResourcePicker utils', () => { const rows: ResourceRowGroup = [ { id: '', - uri: '/subscriptions/44693801-6ee6-49de-9b2d-9106972f9572/resourceGroups/cloud-datasources/providers/microsoft.storage/storageaccounts/csb100320016c43d2d0', + uri: '/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/cloud-datasources/providers/microsoft.storage/storageaccounts/csb100320016c43d2d0', name: '', type: ResourceRowType.Resource, typeLabel: '', @@ -114,7 +114,7 @@ describe('AzureMonitor ResourcePicker utils', () => { expect( findRow( rows, - '/subscriptions/44693801-6ee6-49de-9b2d-9106972f9572/resourceGroups/cloud-datasources/providers/Microsoft.Storage/storageAccounts/csb100320016c43d2d0' + '/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/cloud-datasources/providers/Microsoft.Storage/storageAccounts/csb100320016c43d2d0' ) ).toEqual(rows[0]); }); @@ -123,7 +123,7 @@ describe('AzureMonitor ResourcePicker utils', () => { const rows: ResourceRowGroup = [ { id: '', - uri: '/subscriptions/44693801-6ee6-49de-9b2d-9106972f9572/resourceGroups/CLOUD-DATASOURCES/providers/microsoft.storage/storageaccounts/csb100320016c43d2d0', + uri: '/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/CLOUD-DATASOURCES/providers/microsoft.storage/storageaccounts/csb100320016c43d2d0', name: '', type: ResourceRowType.Resource, typeLabel: '', @@ -132,10 +132,35 @@ describe('AzureMonitor ResourcePicker utils', () => { expect( findRow( rows, - '/subscriptions/44693801-6ee6-49de-9b2d-9106972f9572/resourceGroups/cloud-datasources/providers/Microsoft.Storage/storageAccounts/csb100320016c43d2d0' + '/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/cloud-datasources/providers/Microsoft.Storage/storageAccounts/csb100320016c43d2d0' ) ).toEqual(rows[0]); }); + + it('should find a row matching the right subresource', () => { + const rows: ResourceRowGroup = [ + { + id: '', + uri: '/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/cloud-datasources/providers/Microsoft.Sql/servers/foo', + name: '', + type: ResourceRowType.Resource, + typeLabel: '', + }, + { + id: '', + uri: '/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/cloud-datasources/providers/Microsoft.Sql/servers/foo/databases/bar', + name: '', + type: ResourceRowType.Resource, + typeLabel: '', + }, + ]; + expect( + findRow( + rows, + '/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/cloud-datasources/providers/Microsoft.Sql/servers/foo/databases/bar' + ) + ).toEqual(rows[1]); + }); }); describe('setResource', () => { diff --git a/public/app/plugins/datasource/grafana-azure-monitor-datasource/components/ResourcePicker/utils.ts b/public/app/plugins/datasource/grafana-azure-monitor-datasource/components/ResourcePicker/utils.ts index 6a20d5df88d..a5a8778eb76 100644 --- a/public/app/plugins/datasource/grafana-azure-monitor-datasource/components/ResourcePicker/utils.ts +++ b/public/app/plugins/datasource/grafana-azure-monitor-datasource/components/ResourcePicker/utils.ts @@ -62,6 +62,22 @@ export function isGUIDish(input: string) { return !!input.match(/^[A-Z0-9]+/i); } +function compareNamespaceAndName( + rowNamespace?: string, + rowName?: string, + resourceNamespace?: string, + resourceName?: string +) { + // StorageAccounts subresources are not listed independently + if (resourceNamespace?.startsWith('microsoft.storage/storageaccounts')) { + resourceNamespace = 'microsoft.storage/storageaccounts'; + if (resourceName?.endsWith('/default')) { + resourceName = resourceName.slice(0, -'/default'.length); + } + } + return rowNamespace === resourceNamespace && rowName === resourceName; +} + function matchURI(rowURI: string, resourceURI: string) { const targetParams = parseResourceDetails(resourceURI); const rowParams = parseResourceDetails(rowURI); @@ -69,11 +85,12 @@ function matchURI(rowURI: string, resourceURI: string) { return ( rowParams?.subscription === targetParams?.subscription && rowParams?.resourceGroup?.toLowerCase() === targetParams?.resourceGroup?.toLowerCase() && - // metricNamespace may include a subresource that we don't need to compare - rowParams?.metricNamespace?.toLowerCase().split('/')[0] === - targetParams?.metricNamespace?.toLowerCase().split('/')[0] && - // resourceName may include a subresource that we don't need to compare - rowParams?.resourceName?.split('/')[0] === targetParams?.resourceName?.split('/')[0] + compareNamespaceAndName( + rowParams?.metricNamespace?.toLowerCase(), + rowParams?.resourceName, + targetParams?.metricNamespace?.toLowerCase(), + targetParams?.resourceName + ) ); }