Adding reload to datasourceSrv (#44217)
This commit is contained in:
@@ -28,6 +28,11 @@ export interface DataSourceSrv {
|
|||||||
ref?: DataSourceRef | string | null,
|
ref?: DataSourceRef | string | null,
|
||||||
scopedVars?: ScopedVars
|
scopedVars?: ScopedVars
|
||||||
): DataSourceInstanceSettings | undefined;
|
): DataSourceInstanceSettings | undefined;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reloads the DataSourceSrv
|
||||||
|
*/
|
||||||
|
reload(): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @public */
|
/** @public */
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ describe('getAlertingValidationMessage', () => {
|
|||||||
return [];
|
return [];
|
||||||
},
|
},
|
||||||
getInstanceSettings: (() => {}) as any,
|
getInstanceSettings: (() => {}) as any,
|
||||||
|
reload: () => jest.fn(),
|
||||||
};
|
};
|
||||||
const targets: ElasticsearchQuery[] = [
|
const targets: ElasticsearchQuery[] = [
|
||||||
{ refId: 'A', query: '@hostname:$hostname' },
|
{ refId: 'A', query: '@hostname:$hostname' },
|
||||||
@@ -77,6 +78,7 @@ describe('getAlertingValidationMessage', () => {
|
|||||||
getList(): DataSourceInstanceSettings[] {
|
getList(): DataSourceInstanceSettings[] {
|
||||||
return [];
|
return [];
|
||||||
},
|
},
|
||||||
|
reload: () => jest.fn(),
|
||||||
};
|
};
|
||||||
const targets: any[] = [
|
const targets: any[] = [
|
||||||
{ refId: 'A', query: 'some query', datasource: 'alertingDatasource' },
|
{ refId: 'A', query: 'some query', datasource: 'alertingDatasource' },
|
||||||
@@ -108,6 +110,7 @@ describe('getAlertingValidationMessage', () => {
|
|||||||
getList(): DataSourceInstanceSettings[] {
|
getList(): DataSourceInstanceSettings[] {
|
||||||
return [];
|
return [];
|
||||||
},
|
},
|
||||||
|
reload: () => jest.fn(),
|
||||||
};
|
};
|
||||||
const targets: ElasticsearchQuery[] = [
|
const targets: ElasticsearchQuery[] = [
|
||||||
{ refId: 'A', query: '@hostname:$hostname' },
|
{ refId: 'A', query: '@hostname:$hostname' },
|
||||||
@@ -142,6 +145,7 @@ describe('getAlertingValidationMessage', () => {
|
|||||||
getList(): DataSourceInstanceSettings[] {
|
getList(): DataSourceInstanceSettings[] {
|
||||||
return [];
|
return [];
|
||||||
},
|
},
|
||||||
|
reload: () => jest.fn(),
|
||||||
};
|
};
|
||||||
const targets: ElasticsearchQuery[] = [
|
const targets: ElasticsearchQuery[] = [
|
||||||
{ refId: 'A', query: '@hostname:hostname' },
|
{ refId: 'A', query: '@hostname:hostname' },
|
||||||
@@ -175,6 +179,7 @@ describe('getAlertingValidationMessage', () => {
|
|||||||
getList(): DataSourceInstanceSettings[] {
|
getList(): DataSourceInstanceSettings[] {
|
||||||
return [];
|
return [];
|
||||||
},
|
},
|
||||||
|
reload: () => jest.fn(),
|
||||||
};
|
};
|
||||||
const targets: ElasticsearchQuery[] = [
|
const targets: ElasticsearchQuery[] = [
|
||||||
{ refId: 'A', query: '@hostname:hostname' },
|
{ refId: 'A', query: '@hostname:hostname' },
|
||||||
|
|||||||
@@ -287,6 +287,8 @@ export class MockDataSourceSrv implements DataSourceSrv {
|
|||||||
async loadDatasource(name: string): Promise<DataSourceApi<any, any>> {
|
async loadDatasource(name: string): Promise<DataSourceApi<any, any>> {
|
||||||
return DatasourceSrv.prototype.loadDatasource.call(this, name);
|
return DatasourceSrv.prototype.loadDatasource.call(this, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
reload() {}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const mockGrafanaReceiver = (
|
export const mockGrafanaReceiver = (
|
||||||
|
|||||||
@@ -220,6 +220,7 @@ describe('DashboardPage', () => {
|
|||||||
get: jest.fn().mockResolvedValue({}),
|
get: jest.fn().mockResolvedValue({}),
|
||||||
getInstanceSettings: jest.fn().mockReturnValue({ meta: {} }),
|
getInstanceSettings: jest.fn().mockReturnValue({ meta: {} }),
|
||||||
getList: jest.fn(),
|
getList: jest.fn(),
|
||||||
|
reload: jest.fn(),
|
||||||
});
|
});
|
||||||
|
|
||||||
ctx.setup(() => {
|
ctx.setup(() => {
|
||||||
|
|||||||
@@ -8,8 +8,6 @@ import { importDataSourcePlugin } from 'app/features/plugins/plugin_loader';
|
|||||||
import { getPluginSettings } from 'app/features/plugins/pluginSettings';
|
import { getPluginSettings } from 'app/features/plugins/pluginSettings';
|
||||||
import { DataSourcePluginCategory, ThunkDispatch, ThunkResult } from 'app/types';
|
import { DataSourcePluginCategory, ThunkDispatch, ThunkResult } from 'app/types';
|
||||||
|
|
||||||
import config from '../../../core/config';
|
|
||||||
|
|
||||||
import { buildCategories } from './buildCategories';
|
import { buildCategories } from './buildCategories';
|
||||||
import { buildNavModel } from './navModel';
|
import { buildNavModel } from './navModel';
|
||||||
import {
|
import {
|
||||||
@@ -218,7 +216,7 @@ export function addDataSource(plugin: DataSourcePluginMeta): ThunkResult<void> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const result = await getBackendSrv().post('/api/datasources', newInstance);
|
const result = await getBackendSrv().post('/api/datasources', newInstance);
|
||||||
await updateFrontendSettings();
|
await getDatasourceSrv().reload();
|
||||||
locationService.push(`/datasources/edit/${result.datasource.uid}`);
|
locationService.push(`/datasources/edit/${result.datasource.uid}`);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -235,7 +233,7 @@ export function loadDataSourcePlugins(): ThunkResult<void> {
|
|||||||
export function updateDataSource(dataSource: DataSourceSettings): ThunkResult<void> {
|
export function updateDataSource(dataSource: DataSourceSettings): ThunkResult<void> {
|
||||||
return async (dispatch) => {
|
return async (dispatch) => {
|
||||||
await getBackendSrv().put(`/api/datasources/${dataSource.id}`, dataSource); // by UID not yet supported
|
await getBackendSrv().put(`/api/datasources/${dataSource.id}`, dataSource); // by UID not yet supported
|
||||||
await updateFrontendSettings();
|
await getDatasourceSrv().reload();
|
||||||
return dispatch(loadDataSource(dataSource.uid));
|
return dispatch(loadDataSource(dataSource.uid));
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -245,7 +243,7 @@ export function deleteDataSource(): ThunkResult<void> {
|
|||||||
const dataSource = getStore().dataSources.dataSource;
|
const dataSource = getStore().dataSources.dataSource;
|
||||||
|
|
||||||
await getBackendSrv().delete(`/api/datasources/${dataSource.id}`);
|
await getBackendSrv().delete(`/api/datasources/${dataSource.id}`);
|
||||||
await updateFrontendSettings();
|
await getDatasourceSrv().reload();
|
||||||
|
|
||||||
locationService.push('/datasources');
|
locationService.push('/datasources');
|
||||||
};
|
};
|
||||||
@@ -283,16 +281,6 @@ export function findNewName(dataSources: ItemWithName[], name: string) {
|
|||||||
return name;
|
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) {
|
function nameHasSuffix(name: string) {
|
||||||
return name.endsWith('-', name.length - 1);
|
return name.endsWith('-', name.length - 1);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import {
|
|||||||
TemplateSrv,
|
TemplateSrv,
|
||||||
getTemplateSrv,
|
getTemplateSrv,
|
||||||
getLegacyAngularInjector,
|
getLegacyAngularInjector,
|
||||||
|
getBackendSrv,
|
||||||
} from '@grafana/runtime';
|
} from '@grafana/runtime';
|
||||||
// Types
|
// Types
|
||||||
import {
|
import {
|
||||||
@@ -26,6 +27,7 @@ import {
|
|||||||
import { DataSourceVariableModel } from '../variables/types';
|
import { DataSourceVariableModel } from '../variables/types';
|
||||||
import { ExpressionDatasourceRef } from '@grafana/runtime/src/utils/DataSourceWithBackend';
|
import { ExpressionDatasourceRef } from '@grafana/runtime/src/utils/DataSourceWithBackend';
|
||||||
import appEvents from 'app/core/app_events';
|
import appEvents from 'app/core/app_events';
|
||||||
|
import config from 'app/core/config';
|
||||||
|
|
||||||
export class DatasourceSrv implements DataSourceService {
|
export class DatasourceSrv implements DataSourceService {
|
||||||
private datasources: Record<string, DataSourceApi> = {}; // UID
|
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[]) {
|
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', () => {
|
describe('datasource_srv', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
jest.resetModules();
|
||||||
|
});
|
||||||
|
|
||||||
const dataSourceSrv = new DatasourceSrv(templateSrv);
|
const dataSourceSrv = new DatasourceSrv(templateSrv);
|
||||||
const dataSourceInit = {
|
const dataSourceInit = {
|
||||||
mmm: {
|
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({}),
|
get: jest.fn().mockResolvedValue({}),
|
||||||
getList: jest.fn().mockReturnValue([]),
|
getList: jest.fn().mockReturnValue([]),
|
||||||
getInstanceSettings: getInstanceSettingsMock,
|
getInstanceSettings: getInstanceSettingsMock,
|
||||||
|
reload: jest.fn(),
|
||||||
});
|
});
|
||||||
const variableQueryRunner: any = {
|
const variableQueryRunner: any = {
|
||||||
cancelRequest: jest.fn(),
|
cancelRequest: jest.fn(),
|
||||||
|
|||||||
Reference in New Issue
Block a user