From 1be45d1ed90ebaa4d9f0e7400c686a6544d8cb65 Mon Sep 17 00:00:00 2001 From: Kevin Yu Date: Tue, 26 Apr 2022 07:36:37 -0700 Subject: [PATCH] Azure Monitor: reset resource uri when resource changes (#48235) --- .../MetricsQueryEditor.test.tsx | 117 ++++++++++++++++++ .../MetricsQueryEditor/dataHooks.test.ts | 3 + .../MetricsQueryEditor/setQueryValue.ts | 4 + 3 files changed, 124 insertions(+) diff --git a/public/app/plugins/datasource/grafana-azure-monitor-datasource/components/MetricsQueryEditor/MetricsQueryEditor.test.tsx b/public/app/plugins/datasource/grafana-azure-monitor-datasource/components/MetricsQueryEditor/MetricsQueryEditor.test.tsx index fde54f35f09..ac757133c35 100644 --- a/public/app/plugins/datasource/grafana-azure-monitor-datasource/components/MetricsQueryEditor/MetricsQueryEditor.test.tsx +++ b/public/app/plugins/datasource/grafana-azure-monitor-datasource/components/MetricsQueryEditor/MetricsQueryEditor.test.tsx @@ -76,6 +76,123 @@ describe('Azure Monitor QueryEditor', () => { }); }); + it('should change the resource group when selected', async () => { + const mockDatasource = createMockDatasource(); + const onChange = jest.fn(); + const mockQuery = createMockQuery(); + mockDatasource.getResourceGroups = jest.fn().mockResolvedValue([ + { text: 'grafanastaging', value: 'grafanastaging' }, + { text: 'Grafana Prod', value: 'grafanaprod' }, + ]); + render( + {}} + /> + ); + await waitFor(() => expect(screen.getByTestId('azure-monitor-metrics-query-editor')).toBeInTheDocument()); + + const resourceGroup = await screen.findByLabelText('Resource group'); + await selectOptionInTest(resourceGroup, 'Grafana Prod'); + + expect(onChange).toHaveBeenLastCalledWith({ + ...mockQuery, + azureMonitor: { + ...mockQuery.azureMonitor, + resourceUri: '', + resourceGroup: 'grafanaprod', + metricDefinition: undefined, + metricNamespace: undefined, + resourceName: undefined, + metricName: undefined, + aggregation: undefined, + timeGrain: '', + dimensionFilters: [], + }, + }); + }); + + it('should change the resource type when selected', async () => { + const mockDatasource = createMockDatasource(); + const onChange = jest.fn(); + const mockQuery = createMockQuery(); + mockDatasource.getMetricDefinitions = jest.fn().mockResolvedValue([ + { text: 'Virtual Machine', value: 'azure/vm' }, + { text: 'Database', value: 'azure/db' }, + ]); + render( + {}} + /> + ); + await waitFor(() => expect(screen.getByTestId('azure-monitor-metrics-query-editor')).toBeInTheDocument()); + + const resourceGroup = await screen.findByLabelText('Resource type'); + await selectOptionInTest(resourceGroup, 'Virtual Machine'); + + expect(onChange).toHaveBeenLastCalledWith({ + ...mockQuery, + azureMonitor: { + ...mockQuery.azureMonitor, + resourceUri: '', + metricDefinition: 'azure/vm', + resourceName: undefined, + metricNamespace: undefined, + metricName: undefined, + aggregation: undefined, + timeGrain: '', + dimensionFilters: [], + }, + }); + }); + + it('should change the resource name when selected', async () => { + const mockDatasource = createMockDatasource(); + const onChange = jest.fn(); + const mockQuery = createMockQuery(); + mockDatasource.getResourceNames = jest.fn().mockResolvedValue([ + { text: 'ResourceName1', value: 'resource-name-1' }, + { text: 'ResourceName2', value: 'resource-name-2' }, + ]); + render( + {}} + /> + ); + await waitFor(() => expect(screen.getByTestId('azure-monitor-metrics-query-editor')).toBeInTheDocument()); + + const resourceGroup = await screen.findByLabelText('Resource name'); + await selectOptionInTest(resourceGroup, 'ResourceName1'); + + expect(onChange).toHaveBeenLastCalledWith({ + ...mockQuery, + azureMonitor: { + ...mockQuery.azureMonitor, + resourceUri: '', + resourceName: 'resource-name-1', + metricNamespace: undefined, + metricName: undefined, + aggregation: undefined, + timeGrain: '', + dimensionFilters: [], + }, + }); + }); + it('should change the metric name when selected', async () => { const mockDatasource = createMockDatasource(); const onChange = jest.fn(); diff --git a/public/app/plugins/datasource/grafana-azure-monitor-datasource/components/MetricsQueryEditor/dataHooks.test.ts b/public/app/plugins/datasource/grafana-azure-monitor-datasource/components/MetricsQueryEditor/dataHooks.test.ts index 1c7f84d40da..c88c079c89e 100644 --- a/public/app/plugins/datasource/grafana-azure-monitor-datasource/components/MetricsQueryEditor/dataHooks.test.ts +++ b/public/app/plugins/datasource/grafana-azure-monitor-datasource/components/MetricsQueryEditor/dataHooks.test.ts @@ -314,6 +314,7 @@ describe('AzureMonitor: updateSubscriptions', () => { azureMonitor: { dimensionFilters: [], timeGrain: '', + resourceUri: '', }, }, }, @@ -327,6 +328,7 @@ describe('AzureMonitor: updateSubscriptions', () => { azureMonitor: { dimensionFilters: [], timeGrain: '', + resourceUri: '', }, }, }, @@ -341,6 +343,7 @@ describe('AzureMonitor: updateSubscriptions', () => { azureMonitor: { dimensionFilters: [], timeGrain: '', + resourceUri: '', }, }, }, diff --git a/public/app/plugins/datasource/grafana-azure-monitor-datasource/components/MetricsQueryEditor/setQueryValue.ts b/public/app/plugins/datasource/grafana-azure-monitor-datasource/components/MetricsQueryEditor/setQueryValue.ts index 19c49b3d3dd..7e00af88168 100644 --- a/public/app/plugins/datasource/grafana-azure-monitor-datasource/components/MetricsQueryEditor/setQueryValue.ts +++ b/public/app/plugins/datasource/grafana-azure-monitor-datasource/components/MetricsQueryEditor/setQueryValue.ts @@ -25,6 +25,7 @@ export function setSubscriptionID(query: AzureMonitorQuery, subscriptionID: stri subscription: subscriptionID, azureMonitor: { ...query.azureMonitor, + resourceUri: '', resourceGroup: undefined, metricDefinition: undefined, metricNamespace: undefined, @@ -46,6 +47,7 @@ export function setResourceGroup(query: AzureMonitorQuery, resourceGroup: string ...query, azureMonitor: { ...query.azureMonitor, + resourceUri: '', resourceGroup: resourceGroup, metricDefinition: undefined, metricNamespace: undefined, @@ -68,6 +70,7 @@ export function setResourceType(query: AzureMonitorQuery, resourceType: string | ...query, azureMonitor: { ...query.azureMonitor, + resourceUri: '', metricDefinition: resourceType, resourceName: undefined, metricNamespace: undefined, @@ -90,6 +93,7 @@ export function setResourceName(query: AzureMonitorQuery, resourceName: string | ...query, azureMonitor: { ...query.azureMonitor, + resourceUri: '', resourceName: resourceName, metricNamespace: undefined, metricName: undefined,