added actions

This commit is contained in:
Peter Holmberg 2018-11-05 16:33:43 +01:00
parent 52232f98b9
commit 6dc3e0399d
3 changed files with 56 additions and 1 deletions

1
.gitignore vendored
View File

@ -75,3 +75,4 @@ debug.test
/devenv/bulk_alerting_dashboards/*.json /devenv/bulk_alerting_dashboards/*.json
/scripts/build/release_publisher/release_publisher /scripts/build/release_publisher/release_publisher
*.patch

View File

@ -1,11 +1,14 @@
import { ThunkAction } from 'redux-thunk'; import { ThunkAction } from 'redux-thunk';
import { DataSource, Plugin, StoreState } from 'app/types'; import { DataSource, Plugin, StoreState } from 'app/types';
import { getBackendSrv } from '../../../core/services/backend_srv'; import { getBackendSrv } from '../../../core/services/backend_srv';
import { getDatasourceSrv } from '../../plugins/datasource_srv';
import { LayoutMode } from '../../../core/components/LayoutSelector/LayoutSelector'; import { LayoutMode } from '../../../core/components/LayoutSelector/LayoutSelector';
import { updateLocation, updateNavIndex, UpdateNavIndexAction } from '../../../core/actions'; import { updateLocation, updateNavIndex, UpdateNavIndexAction } from '../../../core/actions';
import { UpdateLocationAction } from '../../../core/actions/location'; import { UpdateLocationAction } from '../../../core/actions/location';
import { buildNavModel } from './navModel'; import { buildNavModel } from './navModel';
import config from '../../../core/config';
export enum ActionTypes { export enum ActionTypes {
LoadDataSources = 'LOAD_DATA_SOURCES', LoadDataSources = 'LOAD_DATA_SOURCES',
LoadDataSourceTypes = 'LOAD_DATA_SOURCE_TYPES', LoadDataSourceTypes = 'LOAD_DATA_SOURCE_TYPES',
@ -159,7 +162,14 @@ export function loadDataSourceTypes(): ThunkResult<void> {
export function updateDataSource(dataSource: DataSource): ThunkResult<void> { export function updateDataSource(dataSource: DataSource): ThunkResult<void> {
return async dispatch => { return async dispatch => {
await getBackendSrv().put(`/api/datasources/${dataSource.id}`, dataSource); await getBackendSrv()
.put(`/api/datasources/${dataSource.id}`, dataSource)
.then(response => {
updateFrontendSettings().then(() => {
testDataSource(response.name);
});
});
dispatch(loadDataSource(dataSource.id)); dispatch(loadDataSource(dataSource.id));
}; };
} }
@ -201,6 +211,49 @@ export function findNewName(dataSources, name) {
return name; return name;
} }
function updateFrontendSettings() {
return getBackendSrv()
.get('/api/frontend/settings')
.then(settings => {
config.datasources = settings.datasources;
config.defaultDatasource = settings.defaultDatasource;
getDatasourceSrv().init();
});
}
function testDataSource(name) {
getDatasourceSrv()
.get(name)
.then(dataSource => {
if (!dataSource.testDatasource) {
return;
}
const testing = { done: false, status: 'error', message: '' };
// make test call in no backend cache context
getBackendSrv()
.withNoBackendCache(() => {
return dataSource
.testDatasource()
.then(result => {
testing.message = result.message;
testing.status = result.status;
})
.catch(err => {
if (err.statusText) {
testing.message = 'HTTP Error ' + err.statusText;
} else {
testing.message = err.message;
}
});
})
.finally(() => {
testing.done = true;
});
});
}
function nameHasSuffix(name) { function nameHasSuffix(name) {
return name.endsWith('-', name.length - 1); return name.endsWith('-', name.length - 1);
} }

View File

@ -88,4 +88,5 @@ export interface DataQueryOptions {
export interface DataSourceApi { export interface DataSourceApi {
query(options: DataQueryOptions): Promise<DataQueryResponse>; query(options: DataQueryOptions): Promise<DataQueryResponse>;
testDatasource(): Promise<any>;
} }