From 3b728ed78ae50884da9a452d5ad0faf6b727aea7 Mon Sep 17 00:00:00 2001 From: Andres Martinez Gotor Date: Wed, 21 Jul 2021 11:28:43 +0200 Subject: [PATCH] AzureMonitor: remove default workspace from configuration editor (#36910) --- .../azure_log_analytics_datasource.test.ts | 47 +- .../azure_log_analytics_datasource.ts | 26 +- .../components/AnalyticsConfig.test.tsx | 43 +- .../components/AnalyticsConfig.tsx | 162 +----- .../components/ConfigEditor.tsx | 33 +- .../AnalyticsConfig.test.tsx.snap | 525 ------------------ .../types/types.ts | 1 - 7 files changed, 89 insertions(+), 748 deletions(-) delete mode 100644 public/app/plugins/datasource/grafana-azure-monitor-datasource/components/__snapshots__/AnalyticsConfig.test.tsx.snap diff --git a/public/app/plugins/datasource/grafana-azure-monitor-datasource/azure_log_analytics/azure_log_analytics_datasource.test.ts b/public/app/plugins/datasource/grafana-azure-monitor-datasource/azure_log_analytics/azure_log_analytics_datasource.test.ts index 50bbddc00bf..429310f8482 100644 --- a/public/app/plugins/datasource/grafana-azure-monitor-datasource/azure_log_analytics/azure_log_analytics_datasource.test.ts +++ b/public/app/plugins/datasource/grafana-azure-monitor-datasource/azure_log_analytics/azure_log_analytics_datasource.test.ts @@ -32,6 +32,10 @@ describe('AzureLogAnalyticsDatasource', () => { }); describe('When performing testDatasource', () => { + beforeEach(() => { + ctx.instanceSettings.jsonData.azureAuthType = 'msi'; + }); + describe('and an error is returned', () => { const error = { data: { @@ -45,7 +49,6 @@ describe('AzureLogAnalyticsDatasource', () => { }; beforeEach(() => { - ctx.instanceSettings.jsonData.azureAuthType = 'msi'; ctx.ds.azureLogAnalyticsDatasource.getResource = jest.fn().mockRejectedValue(error); }); @@ -58,6 +61,13 @@ describe('AzureLogAnalyticsDatasource', () => { }); }); }); + + it('should not include double slashes when getting the resource', async () => { + ctx.ds.azureLogAnalyticsDatasource.firstWorkspace = '/foo/bar'; + ctx.ds.azureLogAnalyticsDatasource.getResource = jest.fn().mockResolvedValue(true); + await ctx.ds.azureLogAnalyticsDatasource.testDatasource(); + expect(ctx.ds.azureLogAnalyticsDatasource.getResource).toHaveBeenCalledWith('loganalytics/v1/foo/bar/metadata'); + }); }); describe('When performing getSchema', () => { @@ -307,4 +317,39 @@ describe('AzureLogAnalyticsDatasource', () => { expect(annotationResults[1].tags[0]).toBe('tag2'); }); }); + + describe('When performing getWorkspaces', () => { + beforeEach(() => { + ctx.ds.azureLogAnalyticsDatasource.getWorkspaceList = jest + .fn() + .mockResolvedValue({ value: [{ name: 'foobar', id: 'foo', properties: { customerId: 'bar' } }] }); + }); + + it('should return the workspace id', async () => { + const workspaces = await ctx.ds.azureLogAnalyticsDatasource.getWorkspaces('sub'); + expect(workspaces).toEqual([{ text: 'foobar', value: 'foo' }]); + }); + }); + + describe('When performing getFirstWorkspace', () => { + beforeEach(() => { + ctx.ds.azureLogAnalyticsDatasource.getDefaultOrFirstSubscription = jest.fn().mockResolvedValue('foo'); + ctx.ds.azureLogAnalyticsDatasource.getWorkspaces = jest + .fn() + .mockResolvedValue([{ text: 'foobar', value: 'foo' }]); + ctx.ds.azureLogAnalyticsDatasource.firstWorkspace = undefined; + }); + + it('should return the stored workspace', async () => { + ctx.ds.azureLogAnalyticsDatasource.firstWorkspace = 'bar'; + const workspace = await ctx.ds.azureLogAnalyticsDatasource.getFirstWorkspace(); + expect(workspace).toEqual('bar'); + expect(ctx.ds.azureLogAnalyticsDatasource.getDefaultOrFirstSubscription).not.toHaveBeenCalled(); + }); + + it('should return the first workspace', async () => { + const workspace = await ctx.ds.azureLogAnalyticsDatasource.getFirstWorkspace(); + expect(workspace).toEqual('foo'); + }); + }); }); diff --git a/public/app/plugins/datasource/grafana-azure-monitor-datasource/azure_log_analytics/azure_log_analytics_datasource.ts b/public/app/plugins/datasource/grafana-azure-monitor-datasource/azure_log_analytics/azure_log_analytics_datasource.ts index d82e967b24f..82bdc9c6f64 100644 --- a/public/app/plugins/datasource/grafana-azure-monitor-datasource/azure_log_analytics/azure_log_analytics_datasource.ts +++ b/public/app/plugins/datasource/grafana-azure-monitor-datasource/azure_log_analytics/azure_log_analytics_datasource.ts @@ -39,7 +39,7 @@ export default class AzureLogAnalyticsDatasource extends DataSourceWithBackend< defaultSubscriptionId?: string; azureMonitorPath: string; - defaultOrFirstWorkspace: string; + firstWorkspace?: string; cache: Map; constructor(private instanceSettings: DataSourceInstanceSettings) { @@ -52,7 +52,6 @@ export default class AzureLogAnalyticsDatasource extends DataSourceWithBackend< this.azurePortalUrl = getAzurePortalUrl(cloud); this.defaultSubscriptionId = this.instanceSettings.jsonData.subscriptionId || ''; - this.defaultOrFirstWorkspace = this.instanceSettings.jsonData.logAnalyticsDefaultWorkspace || ''; } isConfigured(): boolean { @@ -76,7 +75,10 @@ export default class AzureLogAnalyticsDatasource extends DataSourceWithBackend< return ( map(response.value, (val: any) => { - return { text: val.name, value: val.id }; + return { + text: val.name, + value: val.id, + }; }) || [] ); } @@ -112,8 +114,8 @@ export default class AzureLogAnalyticsDatasource extends DataSourceWithBackend< const resource = templateSrv.replace(item.resource, scopedVars); let workspace = templateSrv.replace(item.workspace, scopedVars); - if (!workspace && !resource && this.defaultOrFirstWorkspace) { - workspace = this.defaultOrFirstWorkspace; + if (!workspace && !resource && this.firstWorkspace) { + workspace = this.firstWorkspace; } const query = templateSrv.replace(item.query, scopedVars, this.interpolateVariable); @@ -238,7 +240,7 @@ export default class AzureLogAnalyticsDatasource extends DataSourceWithBackend< } // Execute the query as KQL to the default or first workspace - return this.getDefaultOrFirstWorkspace().then((resourceURI) => { + return this.getFirstWorkspace().then((resourceURI) => { if (!resourceURI) { return []; } @@ -322,9 +324,9 @@ export default class AzureLogAnalyticsDatasource extends DataSourceWithBackend< return subscriptions[0]?.value; } - async getDefaultOrFirstWorkspace(): Promise { - if (this.defaultOrFirstWorkspace) { - return this.defaultOrFirstWorkspace; + async getFirstWorkspace(): Promise { + if (this.firstWorkspace) { + return this.firstWorkspace; } const subscriptionId = await this.getDefaultOrFirstSubscription(); @@ -336,7 +338,7 @@ export default class AzureLogAnalyticsDatasource extends DataSourceWithBackend< const workspace = workspaces[0]?.value; if (workspace) { - this.defaultOrFirstWorkspace = workspace; + this.firstWorkspace = workspace; } return workspace; @@ -384,7 +386,7 @@ export default class AzureLogAnalyticsDatasource extends DataSourceWithBackend< let resourceOrWorkspace: string; try { - const result = await this.getDefaultOrFirstWorkspace(); + const result = await this.getFirstWorkspace(); if (!result) { return { status: 'error', @@ -403,7 +405,7 @@ export default class AzureLogAnalyticsDatasource extends DataSourceWithBackend< try { const path = isGUIDish(resourceOrWorkspace) ? `${this.resourcePath}/v1/workspaces/${resourceOrWorkspace}/metadata` - : `${this.resourcePath}/v1/${resourceOrWorkspace}/metadata`; + : `${this.resourcePath}/v1${resourceOrWorkspace}/metadata`; return await this.getResource(path).then((response: any) => { return { diff --git a/public/app/plugins/datasource/grafana-azure-monitor-datasource/components/AnalyticsConfig.test.tsx b/public/app/plugins/datasource/grafana-azure-monitor-datasource/components/AnalyticsConfig.test.tsx index 166799c87f0..5863a0b29d4 100644 --- a/public/app/plugins/datasource/grafana-azure-monitor-datasource/components/AnalyticsConfig.test.tsx +++ b/public/app/plugins/datasource/grafana-azure-monitor-datasource/components/AnalyticsConfig.test.tsx @@ -27,14 +27,11 @@ const setup = (propsFunc?: (props: Props) => Props) => { jsonData: { cloudName: '', subscriptionId: '', - logAnalyticsDefaultWorkspace: '', }, version: 1, readOnly: false, }, updateOptions: jest.fn(), - getSubscriptions: jest.fn(), - getWorkspaces: jest.fn(), }; if (propsFunc) { @@ -45,14 +42,8 @@ const setup = (propsFunc?: (props: Props) => Props) => { }; describe('Render', () => { - it('should render component', () => { - const wrapper = setup(); - - expect(wrapper.baseElement).toMatchSnapshot(); - }); - it('should disable log analytics credentials form', () => { - const wrapper = setup((props) => ({ + setup((props) => ({ ...props, options: { ...props.options, @@ -62,26 +53,7 @@ describe('Render', () => { }, }, })); - expect(wrapper.baseElement).toMatchSnapshot(); - }); - - it('should enable azure log analytics load workspaces button', () => { - const wrapper = setup((props) => ({ - ...props, - options: { - ...props.options, - jsonData: { - ...props.options.jsonData, - azureLogAnalyticsSameAs: false, - logAnalyticsDefaultWorkspace: '', - tenantId: 'e7f3f661-a933-4b3f-8176-51c4f982ec48', - clientId: '44693801-6ee6-49de-9b2d-9106972f9572', - subscriptionId: 'e3fe4fde-ad5e-4d60-9974-e2f3562ffdf2', - clientSecret: 'cddcc020-2c94-460a-a3d0-df3147ffa792', - }, - }, - })); - expect(wrapper.baseElement).toMatchSnapshot(); + expect(screen.queryByText('Azure Monitor Logs')).not.toBeInTheDocument(); }); it('should not render the Switch to use different creds for log analytics by default', () => { @@ -123,15 +95,4 @@ describe('Render', () => { const newOpts = onUpdate.mock.calls[0][0]({}); expect(newOpts).toEqual({ jsonData: { azureLogAnalyticsSameAs: true } }); }); - - it('should disable inputs', () => { - setup((props) => ({ - ...props, - options: { - ...props.options, - readOnly: true, - }, - })); - expect(screen.queryByText('Load Workspaces')?.closest('button')).toBeDisabled(); - }); }); diff --git a/public/app/plugins/datasource/grafana-azure-monitor-datasource/components/AnalyticsConfig.tsx b/public/app/plugins/datasource/grafana-azure-monitor-datasource/components/AnalyticsConfig.tsx index b5336ac4b56..16d36788b9b 100644 --- a/public/app/plugins/datasource/grafana-azure-monitor-datasource/components/AnalyticsConfig.tsx +++ b/public/app/plugins/datasource/grafana-azure-monitor-datasource/components/AnalyticsConfig.tsx @@ -1,96 +1,24 @@ -import React, { FunctionComponent, useEffect, useMemo, useReducer, useState } from 'react'; -import { SelectableValue } from '@grafana/data'; +import React, { FunctionComponent, useMemo } from 'react'; import { AzureCredentialsForm } from './AzureCredentialsForm'; -import { InlineFormLabel, LegacyForms, Button, Alert } from '@grafana/ui'; -const { Select } = LegacyForms; +import { Button, Alert } from '@grafana/ui'; import { AzureDataSourceSettings } from '../types'; -import { getCredentials, isCredentialsComplete } from '../credentials'; +import { getCredentials } from '../credentials'; export interface Props { options: AzureDataSourceSettings; updateOptions: (optionsFunc: (options: AzureDataSourceSettings) => AzureDataSourceSettings) => void; - getSubscriptions: () => Promise>>; - getWorkspaces: (subscriptionId: string) => Promise>>; } export const AnalyticsConfig: FunctionComponent = (props: Props) => { - const { updateOptions, getSubscriptions, getWorkspaces } = props; + const { updateOptions } = props; const primaryCredentials = useMemo(() => getCredentials(props.options), [props.options]); - const subscriptionId = primaryCredentials.defaultSubscriptionId; - // Only show a section for setting LogAnalytics credentials if // they were set from before with different values and the // authType is supported const logCredentialsEnabled = primaryCredentials.authType === 'clientsecret' && props.options.jsonData.azureLogAnalyticsSameAs === false; - const hasRequiredFields = subscriptionId && isCredentialsComplete(primaryCredentials); - - const defaultWorkspace = props.options.jsonData.logAnalyticsDefaultWorkspace; - - const [workspaces, setWorkspaces] = useState([]); - const [loadWorkspaces, onLoadWorkspaces] = useReducer((val) => val + 1, 0); - useEffect(() => { - if (!hasRequiredFields || !subscriptionId) { - updateWorkspaces([]); - return; - } - let canceled = false; - getWorkspaces(subscriptionId).then((result) => { - if (!canceled) { - updateWorkspaces(result); - } - }); - return () => { - canceled = true; - }; - // This effect is intended to be called only once initially and on Load Workspaces click - // eslint-disable-next-line react-hooks/exhaustive-deps - }, [loadWorkspaces, subscriptionId]); - - const updateWorkspaces = (received: Array>) => { - setWorkspaces(received); - if (!defaultWorkspace && received.length > 0) { - // Setting the default workspace if workspaces received but no default workspace selected - updateOptions((options) => { - return { - ...options, - jsonData: { - ...options.jsonData, - logAnalyticsDefaultWorkspace: received[0].value, - }, - }; - }); - } else if (defaultWorkspace) { - const found = received.find((opt) => opt.value === defaultWorkspace); - if (!found) { - // Unsetting the default workspace if it isn't found among the received workspaces - updateOptions((options) => { - return { - ...options, - jsonData: { - ...options.jsonData, - logAnalyticsDefaultWorkspace: undefined, - }, - }; - }); - } - } - }; - - const onDefaultWorkspaceChange = (selected: SelectableValue) => { - updateOptions((options) => { - return { - ...options, - jsonData: { - ...options.jsonData, - logAnalyticsDefaultWorkspace: selected.value || '', - }, - }; - }); - }; - const onClearAzLogsCreds = () => { updateOptions((options) => { return { @@ -103,70 +31,32 @@ export const AnalyticsConfig: FunctionComponent = (props: Props) => { }); }; - return ( + return logCredentialsEnabled ? ( <>

Azure Monitor Logs

- {logCredentialsEnabled && ( - <> - - Using different credentials for Azure Monitor Logs is no longer supported. Authentication information above - will be used instead. Please create a new data source with the credentials below. - + <> + + Using different credentials for Azure Monitor Logs is no longer supported. Authentication information above + will be used instead. Please create a new data source with the credentials below. + - - - - - )} -
-
-
- - Default Workspace - -
- -
-
-
-
-
-
-
-
-
-
- - - -
-
-
- -
-
-
- - - -`; - -exports[`Render should enable azure log analytics load workspaces button 1`] = ` - -
-

- Azure Monitor Logs -

-
-
-
-
-
-
- Deprecated -
-
- Using different credentials for Azure Monitor Logs is no longer supported. Authentication information above will be used instead. Please create a new data source with the credentials below. -
-
-
-
-
-
- -
-
- -
-
-
-
-
-
- -
-
- -
-
-
-
-
-
- -
-
-
- -
-
-
- Choose -
-
-
- -
-
-
-
-
-
-
-
-
-
-
-
-
- -
-
-
-
-
- -`; - -exports[`Render should render component 1`] = ` - -
-

- Azure Monitor Logs -

-
-
-
-
- -`; diff --git a/public/app/plugins/datasource/grafana-azure-monitor-datasource/types/types.ts b/public/app/plugins/datasource/grafana-azure-monitor-datasource/types/types.ts index 5f3163088bb..719b6e92648 100644 --- a/public/app/plugins/datasource/grafana-azure-monitor-datasource/types/types.ts +++ b/public/app/plugins/datasource/grafana-azure-monitor-datasource/types/types.ts @@ -58,7 +58,6 @@ export interface AzureDataSourceJsonData extends DataSourceJsonData { subscriptionId?: string; // logs - logAnalyticsDefaultWorkspace?: string; /** @deprecated Azure Logs credentials */ azureLogAnalyticsSameAs?: boolean; /** @deprecated Azure Logs credentials */