mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Adding reload to datasourceSrv (#44217)
This commit is contained in:
parent
732c2ebb37
commit
c3f69cc4d9
@ -28,6 +28,11 @@ export interface DataSourceSrv {
|
||||
ref?: DataSourceRef | string | null,
|
||||
scopedVars?: ScopedVars
|
||||
): DataSourceInstanceSettings | undefined;
|
||||
|
||||
/**
|
||||
* Reloads the DataSourceSrv
|
||||
*/
|
||||
reload(): void;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
|
@ -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' },
|
||||
|
@ -287,6 +287,8 @@ export class MockDataSourceSrv implements DataSourceSrv {
|
||||
async loadDatasource(name: string): Promise<DataSourceApi<any, any>> {
|
||||
return DatasourceSrv.prototype.loadDatasource.call(this, name);
|
||||
}
|
||||
|
||||
reload() {}
|
||||
}
|
||||
|
||||
export const mockGrafanaReceiver = (
|
||||
|
@ -220,6 +220,7 @@ describe('DashboardPage', () => {
|
||||
get: jest.fn().mockResolvedValue({}),
|
||||
getInstanceSettings: jest.fn().mockReturnValue({ meta: {} }),
|
||||
getList: jest.fn(),
|
||||
reload: jest.fn(),
|
||||
});
|
||||
|
||||
ctx.setup(() => {
|
||||
|
@ -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<void> {
|
||||
}
|
||||
|
||||
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<void> {
|
||||
export function updateDataSource(dataSource: DataSourceSettings): ThunkResult<void> {
|
||||
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<void> {
|
||||
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);
|
||||
}
|
||||
|
@ -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<string, DataSourceApi> = {}; // 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[]) {
|
||||
|
@ -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');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -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(),
|
||||
|
Loading…
Reference in New Issue
Block a user