From c3f69cc4d9d2fa3c10d668b59dce55ca5dc97c93 Mon Sep 17 00:00:00 2001 From: Timur Olzhabayev Date: Mon, 24 Jan 2022 10:45:05 +0100 Subject: [PATCH] Adding reload to datasourceSrv (#44217) --- .../src/services/dataSourceSrv.ts | 5 ++++ .../getAlertingValidationMessage.test.ts | 5 ++++ public/app/features/alerting/unified/mocks.ts | 2 ++ .../containers/DashboardPage.test.tsx | 1 + .../app/features/datasources/state/actions.ts | 18 ++---------- public/app/features/plugins/datasource_srv.ts | 9 ++++++ .../plugins/tests/datasource_srv.test.ts | 29 +++++++++++++++++++ .../state/initVariableTransaction.test.ts | 1 + 8 files changed, 55 insertions(+), 15 deletions(-) diff --git a/packages/grafana-runtime/src/services/dataSourceSrv.ts b/packages/grafana-runtime/src/services/dataSourceSrv.ts index 31baf9a9f5e..e23c09d5835 100644 --- a/packages/grafana-runtime/src/services/dataSourceSrv.ts +++ b/packages/grafana-runtime/src/services/dataSourceSrv.ts @@ -28,6 +28,11 @@ export interface DataSourceSrv { ref?: DataSourceRef | string | null, scopedVars?: ScopedVars ): DataSourceInstanceSettings | undefined; + + /** + * Reloads the DataSourceSrv + */ + reload(): void; } /** @public */ diff --git a/public/app/features/alerting/getAlertingValidationMessage.test.ts b/public/app/features/alerting/getAlertingValidationMessage.test.ts index 6deb2188b10..f633db400e9 100644 --- a/public/app/features/alerting/getAlertingValidationMessage.test.ts +++ b/public/app/features/alerting/getAlertingValidationMessage.test.ts @@ -35,6 +35,7 @@ describe('getAlertingValidationMessage', () => { return []; }, getInstanceSettings: (() => {}) as any, + reload: () => jest.fn(), }; const targets: ElasticsearchQuery[] = [ { refId: 'A', query: '@hostname:$hostname' }, @@ -77,6 +78,7 @@ describe('getAlertingValidationMessage', () => { getList(): DataSourceInstanceSettings[] { return []; }, + reload: () => jest.fn(), }; const targets: any[] = [ { refId: 'A', query: 'some query', datasource: 'alertingDatasource' }, @@ -108,6 +110,7 @@ describe('getAlertingValidationMessage', () => { getList(): DataSourceInstanceSettings[] { return []; }, + reload: () => jest.fn(), }; const targets: ElasticsearchQuery[] = [ { refId: 'A', query: '@hostname:$hostname' }, @@ -142,6 +145,7 @@ describe('getAlertingValidationMessage', () => { getList(): DataSourceInstanceSettings[] { return []; }, + reload: () => jest.fn(), }; const targets: ElasticsearchQuery[] = [ { refId: 'A', query: '@hostname:hostname' }, @@ -175,6 +179,7 @@ describe('getAlertingValidationMessage', () => { getList(): DataSourceInstanceSettings[] { return []; }, + reload: () => jest.fn(), }; const targets: ElasticsearchQuery[] = [ { refId: 'A', query: '@hostname:hostname' }, diff --git a/public/app/features/alerting/unified/mocks.ts b/public/app/features/alerting/unified/mocks.ts index 79033432099..4ca25450aa4 100644 --- a/public/app/features/alerting/unified/mocks.ts +++ b/public/app/features/alerting/unified/mocks.ts @@ -287,6 +287,8 @@ export class MockDataSourceSrv implements DataSourceSrv { async loadDatasource(name: string): Promise> { return DatasourceSrv.prototype.loadDatasource.call(this, name); } + + reload() {} } export const mockGrafanaReceiver = ( diff --git a/public/app/features/dashboard/containers/DashboardPage.test.tsx b/public/app/features/dashboard/containers/DashboardPage.test.tsx index 79eb8da8a0e..a36aa31fbaa 100644 --- a/public/app/features/dashboard/containers/DashboardPage.test.tsx +++ b/public/app/features/dashboard/containers/DashboardPage.test.tsx @@ -220,6 +220,7 @@ describe('DashboardPage', () => { get: jest.fn().mockResolvedValue({}), getInstanceSettings: jest.fn().mockReturnValue({ meta: {} }), getList: jest.fn(), + reload: jest.fn(), }); ctx.setup(() => { diff --git a/public/app/features/datasources/state/actions.ts b/public/app/features/datasources/state/actions.ts index 7fd392770c7..b2dcdadee76 100644 --- a/public/app/features/datasources/state/actions.ts +++ b/public/app/features/datasources/state/actions.ts @@ -8,8 +8,6 @@ import { importDataSourcePlugin } from 'app/features/plugins/plugin_loader'; import { getPluginSettings } from 'app/features/plugins/pluginSettings'; import { DataSourcePluginCategory, ThunkDispatch, ThunkResult } from 'app/types'; -import config from '../../../core/config'; - import { buildCategories } from './buildCategories'; import { buildNavModel } from './navModel'; import { @@ -218,7 +216,7 @@ export function addDataSource(plugin: DataSourcePluginMeta): ThunkResult { } const result = await getBackendSrv().post('/api/datasources', newInstance); - await updateFrontendSettings(); + await getDatasourceSrv().reload(); locationService.push(`/datasources/edit/${result.datasource.uid}`); }; } @@ -235,7 +233,7 @@ export function loadDataSourcePlugins(): ThunkResult { export function updateDataSource(dataSource: DataSourceSettings): ThunkResult { return async (dispatch) => { await getBackendSrv().put(`/api/datasources/${dataSource.id}`, dataSource); // by UID not yet supported - await updateFrontendSettings(); + await getDatasourceSrv().reload(); return dispatch(loadDataSource(dataSource.uid)); }; } @@ -245,7 +243,7 @@ export function deleteDataSource(): ThunkResult { const dataSource = getStore().dataSources.dataSource; await getBackendSrv().delete(`/api/datasources/${dataSource.id}`); - await updateFrontendSettings(); + await getDatasourceSrv().reload(); locationService.push('/datasources'); }; @@ -283,16 +281,6 @@ export function findNewName(dataSources: ItemWithName[], name: string) { return name; } -function updateFrontendSettings() { - return getBackendSrv() - .get('/api/frontend/settings') - .then((settings: any) => { - config.datasources = settings.datasources; - config.defaultDatasource = settings.defaultDatasource; - getDatasourceSrv().init(config.datasources, settings.defaultDatasource); - }); -} - function nameHasSuffix(name: string) { return name.endsWith('-', name.length - 1); } diff --git a/public/app/features/plugins/datasource_srv.ts b/public/app/features/plugins/datasource_srv.ts index 7db01beddeb..ad7cd75764e 100644 --- a/public/app/features/plugins/datasource_srv.ts +++ b/public/app/features/plugins/datasource_srv.ts @@ -7,6 +7,7 @@ import { TemplateSrv, getTemplateSrv, getLegacyAngularInjector, + getBackendSrv, } from '@grafana/runtime'; // Types import { @@ -26,6 +27,7 @@ import { import { DataSourceVariableModel } from '../variables/types'; import { ExpressionDatasourceRef } from '@grafana/runtime/src/utils/DataSourceWithBackend'; import appEvents from 'app/core/app_events'; +import config from 'app/core/config'; export class DatasourceSrv implements DataSourceService { private datasources: Record = {}; // UID @@ -324,6 +326,13 @@ export class DatasourceSrv implements DataSourceService { }; }); } + + async reload() { + const settings = await getBackendSrv().get('/api/frontend/settings'); + config.datasources = settings.datasources; + config.defaultDatasource = settings.defaultDatasource; + this.init(settings.datasources, settings.defaultDatasource); + } } export function variableInterpolation(value: any[]) { diff --git a/public/app/features/plugins/tests/datasource_srv.test.ts b/public/app/features/plugins/tests/datasource_srv.test.ts index 9af0de14ff1..eba0d0dc8ce 100644 --- a/public/app/features/plugins/tests/datasource_srv.test.ts +++ b/public/app/features/plugins/tests/datasource_srv.test.ts @@ -46,7 +46,20 @@ jest.mock('../plugin_loader', () => ({ }, })); +const getBackendSrvGetMock = jest.fn(); + +jest.mock('@grafana/runtime', () => ({ + ...jest.requireActual('@grafana/runtime'), + getBackendSrv: () => ({ + get: getBackendSrvGetMock, + }), +})); + describe('datasource_srv', () => { + beforeEach(() => { + jest.resetModules(); + }); + const dataSourceSrv = new DatasourceSrv(templateSrv); const dataSourceInit = { mmm: { @@ -294,5 +307,21 @@ describe('datasource_srv', () => { ] `); }); + + it('Should reload the datasource', async () => { + // arrange + getBackendSrvGetMock.mockReturnValueOnce({ + datasources: { + ...dataSourceInit, + }, + defaultDatasource: 'aaa', + }); + const initMock = jest.spyOn(dataSourceSrv, 'init').mockImplementation(() => {}); + // act + await dataSourceSrv.reload(); + // assert + expect(getBackendSrvGetMock).toHaveBeenCalledWith('/api/frontend/settings'); + expect(initMock).toHaveBeenCalledWith(dataSourceInit, 'aaa'); + }); }); }); diff --git a/public/app/features/variables/state/initVariableTransaction.test.ts b/public/app/features/variables/state/initVariableTransaction.test.ts index 6659fb05187..4e64567864d 100644 --- a/public/app/features/variables/state/initVariableTransaction.test.ts +++ b/public/app/features/variables/state/initVariableTransaction.test.ts @@ -47,6 +47,7 @@ function getTestContext(variables?: VariableModel[]) { get: jest.fn().mockResolvedValue({}), getList: jest.fn().mockReturnValue([]), getInstanceSettings: getInstanceSettingsMock, + reload: jest.fn(), }); const variableQueryRunner: any = { cancelRequest: jest.fn(),