From b975a982f89833b66627fb9d0a3fbf925cc18747 Mon Sep 17 00:00:00 2001 From: Andreas Christou Date: Wed, 9 Apr 2025 20:16:10 +0100 Subject: [PATCH] Azure: Ensure basic logs queries are limited to a single resource (#103588) Ensure basic logs queries are limited to a single resource --- .../__mocks__/resourcePickerRows.ts | 16 ++++++++ .../LogsQueryEditor/LogsQueryEditor.test.tsx | 40 +++++++++++++++++++ .../LogsQueryEditor/LogsQueryEditor.tsx | 14 ++++++- 3 files changed, 69 insertions(+), 1 deletion(-) diff --git a/public/app/plugins/datasource/azuremonitor/__mocks__/resourcePickerRows.ts b/public/app/plugins/datasource/azuremonitor/__mocks__/resourcePickerRows.ts index a9ad12a83a7..de67a17f987 100644 --- a/public/app/plugins/datasource/azuremonitor/__mocks__/resourcePickerRows.ts +++ b/public/app/plugins/datasource/azuremonitor/__mocks__/resourcePickerRows.ts @@ -104,6 +104,22 @@ export const mockResourcesByResourceGroup = (): ResourceRowGroup => [ type: ResourceRowType.Resource, location: 'northeurope', }, + { + id: 'la-workspace', + uri: '/subscriptions/def-456/resourceGroups/dev-3/providers/microsoft.operationalinsights/workspaces/la-workspace', + name: 'la-workspace', + typeLabel: 'Microsoft.OperationalInsights', + type: ResourceRowType.Resource, + location: 'northeurope', + }, + { + id: 'la-workspace-1', + uri: '/subscriptions/def-456/resourceGroups/dev-3/providers/microsoft.operationalinsights/workspaces/la-workspace-1', + name: 'la-workspace-1', + typeLabel: 'Microsoft.OperationalInsights', + type: ResourceRowType.Resource, + location: 'northeurope', + }, { id: 'app-insights-1', uri: '/subscriptions/def-456/resourceGroups/dev-3/providers/microsoft.insights/components/app-insights-1', diff --git a/public/app/plugins/datasource/azuremonitor/components/LogsQueryEditor/LogsQueryEditor.test.tsx b/public/app/plugins/datasource/azuremonitor/components/LogsQueryEditor/LogsQueryEditor.test.tsx index 531abccb8ef..9eadee09f48 100644 --- a/public/app/plugins/datasource/azuremonitor/components/LogsQueryEditor/LogsQueryEditor.test.tsx +++ b/public/app/plugins/datasource/azuremonitor/components/LogsQueryEditor/LogsQueryEditor.test.tsx @@ -424,6 +424,46 @@ describe('LogsQueryEditor', () => { expect(await screen.queryByLabelText('Basic')).not.toBeInTheDocument(); }); + + it('should disable other resources with a basic logs query when one resource is selected', async () => { + const mockDatasource = createMockDatasource({ resourcePickerData: createMockResourcePickerData() }); + const query = createMockQuery({ + azureLogAnalytics: { + resources: [ + '/subscriptions/def-456/resourceGroups/dev-3/providers/microsoft.operationalinsights/workspaces/la-workspace', + ], + basicLogsQuery: true, + }, + }); + const basicLogsEnabled = true; + const onChange = jest.fn(); + const onQueryChange = jest.fn(); + + render( + {}} + basicLogsEnabled={basicLogsEnabled} + /> + ); + + expect(await screen.findByLabelText('Basic')).toBeInTheDocument(); + + const resourcePickerButton = await screen.findByRole('button', { name: 'la-workspace' }); + await userEvent.click(resourcePickerButton); + + const checkbox = await screen.findByLabelText('la-workspace'); + expect(checkbox).toBeChecked(); + + expect(await screen.findByLabelText('la-workspace-1')).toBeDisabled(); + expect( + await screen.findByText('When using Basic Logs, you may only select one resource at a time.') + ).toBeInTheDocument(); + }); }); describe('data ingestion warning', () => { diff --git a/public/app/plugins/datasource/azuremonitor/components/LogsQueryEditor/LogsQueryEditor.tsx b/public/app/plugins/datasource/azuremonitor/components/LogsQueryEditor/LogsQueryEditor.tsx index 31108f92d52..0970842e25d 100644 --- a/public/app/plugins/datasource/azuremonitor/components/LogsQueryEditor/LogsQueryEditor.tsx +++ b/public/app/plugins/datasource/azuremonitor/components/LogsQueryEditor/LogsQueryEditor.tsx @@ -59,12 +59,19 @@ const LogsQueryEditor = ({ const from = templateSrv?.replace('$__from'); const to = templateSrv?.replace('$__to'); const templateVariableOptions = templateSrv.getVariables(); + const isBasicLogsQuery = (basicLogsEnabled && query.azureLogAnalytics?.basicLogsQuery) ?? false; const disableRow = (row: ResourceRow, selectedRows: ResourceRowGroup) => { if (selectedRows.length === 0) { // Only if there is some resource(s) selected we should disable rows return false; } + + if (isBasicLogsQuery && selectedRows.length === 1) { + // Basic logs queries can only have one resource selected + return true; + } + const rowResourceNS = parseResourceDetails(row.uri, row.location).metricNamespace?.toLowerCase(); const selectedRowSampleNs = parseResourceDetails( selectedRows[0].uri, @@ -210,7 +217,12 @@ const LogsQueryEditor = ({ // eslint-disable-next-line )} - selectionNotice={() => 'You may only choose items of the same resource type.'} + selectionNotice={(selected) => { + if (selected.length === 1 && isBasicLogsQuery) { + return 'When using Basic Logs, you may only select one resource at a time.'; + } + return 'You may only choose items of the same resource type.'; + }} /> {showBasicLogsToggle && (