grafana/public/app/features/plugins/ds_edit_ctrl.ts

224 lines
5.6 KiB
TypeScript
Raw Normal View History

2017-12-20 05:33:33 -06:00
import _ from 'lodash';
import config from 'app/core/config';
import { coreModule, appEvents } from 'app/core/core';
2018-09-14 02:41:37 -05:00
import { store } from 'app/store/configureStore';
import { getNavModel } from 'app/core/selectors/navModel';
import { buildNavModel } from './state/navModel';
let datasourceTypes = [];
const defaults = {
2017-12-20 05:33:33 -06:00
name: '',
type: 'graphite',
url: '',
access: 'proxy',
jsonData: {},
2017-12-20 05:33:33 -06:00
secureJsonFields: {},
secureJsonData: {},
};
let datasourceCreated = false;
export class DataSourceEditCtrl {
isNew: boolean;
datasources: any[];
current: any;
types: any;
testing: any;
datasourceMeta: any;
editForm: any;
gettingStarted: boolean;
navModel: any;
/** @ngInject */
2018-01-18 11:17:58 -06:00
constructor(private $q, private backendSrv, private $routeParams, private $location, private datasourceSrv) {
2018-09-14 02:41:37 -05:00
const state = store.getState();
this.navModel = getNavModel(state.navIndex, 'datasources');
this.datasources = [];
this.loadDatasourceTypes().then(() => {
if (this.$routeParams.id) {
this.getDatasourceById(this.$routeParams.id);
} else {
this.initNewDatasourceModel();
}
});
}
initNewDatasourceModel() {
2017-08-14 11:11:35 -05:00
this.isNew = true;
this.current = _.cloneDeep(defaults);
2017-08-15 13:53:31 -05:00
// We are coming from getting started
if (this.$location.search().gettingstarted) {
this.gettingStarted = true;
this.current.isDefault = true;
}
this.typeChanged();
}
loadDatasourceTypes() {
if (datasourceTypes.length > 0) {
this.types = datasourceTypes;
return this.$q.when(null);
}
return this.backendSrv.get('/api/plugins', { enabled: 1, type: 'datasource' }).then(plugins => {
datasourceTypes = plugins;
this.types = plugins;
});
}
getDatasourceById(id) {
2017-12-20 05:33:33 -06:00
this.backendSrv.get('/api/datasources/' + id).then(ds => {
this.isNew = false;
this.current = ds;
2017-08-14 14:43:24 -05:00
if (datasourceCreated) {
datasourceCreated = false;
this.testDatasource();
}
2017-08-14 14:43:24 -05:00
return this.typeChanged();
});
}
userChangedType() {
// reset model but keep name & default flag
this.current = _.defaults(
{
id: this.current.id,
name: this.current.name,
isDefault: this.current.isDefault,
2017-12-20 05:33:33 -06:00
type: this.current.type,
},
_.cloneDeep(defaults)
);
this.typeChanged();
}
2018-01-18 10:42:40 -06:00
updateNav() {
2018-09-14 02:41:37 -05:00
this.navModel = buildNavModel(this.current, this.datasourceMeta, 'datasource-settings');
2018-01-18 10:42:40 -06:00
}
typeChanged() {
return this.backendSrv.get('/api/plugins/' + this.current.type + '/settings').then(pluginInfo => {
this.datasourceMeta = pluginInfo;
2018-01-18 10:42:40 -06:00
this.updateNav();
});
}
updateFrontendSettings() {
2017-12-20 05:33:33 -06:00
return this.backendSrv.get('/api/frontend/settings').then(settings => {
config.datasources = settings.datasources;
config.defaultDatasource = settings.defaultDatasource;
this.datasourceSrv.init();
});
}
testDatasource() {
this.datasourceSrv.get(this.current.name).then(datasource => {
if (!datasource.testDatasource) {
return;
}
2017-12-20 05:33:33 -06:00
this.testing = { done: false, status: 'error' };
// make test call in no backend cache context
this.backendSrv
.withNoBackendCache(() => {
return datasource
.testDatasource()
.then(result => {
this.testing.message = result.message;
this.testing.status = result.status;
})
.catch(err => {
if (err.statusText) {
2017-12-20 05:33:33 -06:00
this.testing.message = 'HTTP Error ' + err.statusText;
} else {
this.testing.message = err.message;
}
});
})
.finally(() => {
this.testing.done = true;
});
});
}
saveChanges() {
if (!this.editForm.$valid) {
return;
}
if (this.current.readOnly) {
return;
}
if (this.current.id) {
return this.backendSrv.put('/api/datasources/' + this.current.id, this.current).then(result => {
this.current = result.datasource;
2018-01-18 10:42:40 -06:00
this.updateNav();
this.updateFrontendSettings().then(() => {
this.testDatasource();
});
});
} else {
return this.backendSrv.post('/api/datasources', this.current).then(result => {
this.current = result.datasource;
this.updateFrontendSettings();
datasourceCreated = true;
this.$location.path('datasources/edit/' + result.id);
});
}
}
confirmDelete() {
2017-12-20 05:33:33 -06:00
this.backendSrv.delete('/api/datasources/' + this.current.id).then(() => {
this.$location.path('datasources');
});
}
delete(s) {
2017-12-20 05:33:33 -06:00
appEvents.emit('confirm-modal', {
title: 'Delete',
text: 'Are you sure you want to delete this datasource?',
yesText: 'Delete',
icon: 'fa-trash',
onConfirm: () => {
this.confirmDelete();
2017-12-20 05:33:33 -06:00
},
});
}
}
2017-12-20 05:33:33 -06:00
coreModule.controller('DataSourceEditCtrl', DataSourceEditCtrl);
coreModule.directive('datasourceHttpSettings', () => {
return {
scope: {
2017-12-20 05:33:33 -06:00
current: '=',
suggestUrl: '@',
noDirectAccess: '@',
},
2017-12-20 05:33:33 -06:00
templateUrl: 'public/app/features/plugins/partials/ds_http_settings.html',
link: {
pre: ($scope, elem, attrs) => {
// do not show access option if direct access is disabled
$scope.showAccessOption = $scope.noDirectAccess !== 'true';
$scope.showAccessHelp = false;
$scope.toggleAccessHelp = () => {
$scope.showAccessHelp = !$scope.showAccessHelp;
};
$scope.getSuggestUrls = () => {
return [$scope.suggestUrl];
};
2017-12-20 05:33:33 -06:00
},
},
};
});