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';
|
2016-03-10 02:47:29 -06:00
|
|
|
|
2018-08-30 01:58:43 -05:00
|
|
|
let datasourceTypes = [];
|
2016-03-10 02:47:29 -06:00
|
|
|
|
2018-08-26 14:52:57 -05:00
|
|
|
const defaults = {
|
2017-12-20 05:33:33 -06:00
|
|
|
name: '',
|
|
|
|
type: 'graphite',
|
|
|
|
url: '',
|
|
|
|
access: 'proxy',
|
2016-12-14 13:38:35 -06:00
|
|
|
jsonData: {},
|
2017-12-20 05:33:33 -06:00
|
|
|
secureJsonFields: {},
|
2018-04-16 09:28:20 -05:00
|
|
|
secureJsonData: {},
|
2016-03-10 02:47:29 -06:00
|
|
|
};
|
|
|
|
|
2018-08-30 01:58:43 -05:00
|
|
|
let datasourceCreated = false;
|
2016-04-12 10:20:03 -05:00
|
|
|
|
2016-03-10 02:47:29 -06:00
|
|
|
export class DataSourceEditCtrl {
|
|
|
|
isNew: boolean;
|
|
|
|
datasources: any[];
|
|
|
|
current: any;
|
|
|
|
types: any;
|
|
|
|
testing: any;
|
|
|
|
datasourceMeta: any;
|
2016-03-11 17:16:08 -06:00
|
|
|
editForm: any;
|
2016-11-18 09:31:19 -06:00
|
|
|
gettingStarted: boolean;
|
2017-06-02 07:00:42 -05:00
|
|
|
navModel: any;
|
2016-03-10 02:47:29 -06:00
|
|
|
|
|
|
|
/** @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');
|
2017-06-02 07:00:42 -05:00
|
|
|
this.datasources = [];
|
|
|
|
|
|
|
|
this.loadDatasourceTypes().then(() => {
|
|
|
|
if (this.$routeParams.id) {
|
|
|
|
this.getDatasourceById(this.$routeParams.id);
|
|
|
|
} else {
|
|
|
|
this.initNewDatasourceModel();
|
2016-03-10 02:47:29 -06:00
|
|
|
}
|
2017-06-02 07:00:42 -05:00
|
|
|
});
|
|
|
|
}
|
2016-03-10 02:47:29 -06:00
|
|
|
|
2017-06-02 07:00:42 -05:00
|
|
|
initNewDatasourceModel() {
|
2017-08-14 11:11:35 -05:00
|
|
|
this.isNew = true;
|
2017-08-28 08:29:31 -05:00
|
|
|
this.current = _.cloneDeep(defaults);
|
2017-08-15 13:53:31 -05:00
|
|
|
|
2017-06-02 07:00:42 -05:00
|
|
|
// We are coming from getting started
|
|
|
|
if (this.$location.search().gettingstarted) {
|
|
|
|
this.gettingStarted = true;
|
|
|
|
this.current.isDefault = true;
|
2016-03-10 02:47:29 -06:00
|
|
|
}
|
|
|
|
|
2017-06-02 07:00:42 -05:00
|
|
|
this.typeChanged();
|
|
|
|
}
|
2016-03-10 02:47:29 -06:00
|
|
|
|
2017-06-02 07:00:42 -05:00
|
|
|
loadDatasourceTypes() {
|
|
|
|
if (datasourceTypes.length > 0) {
|
|
|
|
this.types = datasourceTypes;
|
|
|
|
return this.$q.when(null);
|
2016-03-10 02:47:29 -06:00
|
|
|
}
|
|
|
|
|
2017-12-21 01:39:31 -06:00
|
|
|
return this.backendSrv.get('/api/plugins', { enabled: 1, type: 'datasource' }).then(plugins => {
|
|
|
|
datasourceTypes = plugins;
|
|
|
|
this.types = plugins;
|
|
|
|
});
|
2017-06-02 07:00:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
getDatasourceById(id) {
|
2017-12-20 05:33:33 -06:00
|
|
|
this.backendSrv.get('/api/datasources/' + id).then(ds => {
|
2017-06-02 07:00:42 -05:00
|
|
|
this.isNew = false;
|
|
|
|
this.current = ds;
|
2017-08-14 14:43:24 -05:00
|
|
|
|
2017-06-02 07:00:42 -05:00
|
|
|
if (datasourceCreated) {
|
|
|
|
datasourceCreated = false;
|
|
|
|
this.testDatasource();
|
|
|
|
}
|
2017-08-14 14:43:24 -05:00
|
|
|
|
2017-06-02 07:00:42 -05:00
|
|
|
return this.typeChanged();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-08-28 08:29:31 -05:00
|
|
|
userChangedType() {
|
|
|
|
// reset model but keep name & default flag
|
2017-12-19 09:06:54 -06:00
|
|
|
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,
|
2017-12-19 09:06:54 -06:00
|
|
|
},
|
|
|
|
_.cloneDeep(defaults)
|
|
|
|
);
|
2017-08-28 08:29:31 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-06-02 07:00:42 -05:00
|
|
|
typeChanged() {
|
2017-12-21 01:39:31 -06:00
|
|
|
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();
|
2017-12-21 01:39:31 -06:00
|
|
|
});
|
2017-06-02 07:00:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
updateFrontendSettings() {
|
2017-12-20 05:33:33 -06:00
|
|
|
return this.backendSrv.get('/api/frontend/settings').then(settings => {
|
2017-06-02 07:00:42 -05:00
|
|
|
config.datasources = settings.datasources;
|
|
|
|
config.defaultDatasource = settings.defaultDatasource;
|
|
|
|
this.datasourceSrv.init();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
testDatasource() {
|
|
|
|
this.datasourceSrv.get(this.current.name).then(datasource => {
|
|
|
|
if (!datasource.testDatasource) {
|
|
|
|
return;
|
|
|
|
}
|
2016-03-10 02:47:29 -06:00
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
this.testing = { done: false, status: 'error' };
|
2017-08-23 06:31:26 -05:00
|
|
|
|
|
|
|
// make test call in no backend cache context
|
2017-12-19 09:06:54 -06:00
|
|
|
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;
|
2017-12-19 09:06:54 -06:00
|
|
|
} else {
|
|
|
|
this.testing.message = err.message;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
this.testing.done = true;
|
2017-08-23 06:31:26 -05:00
|
|
|
});
|
2017-06-02 07:00:42 -05:00
|
|
|
});
|
|
|
|
}
|
2016-03-10 02:47:29 -06:00
|
|
|
|
2017-06-02 07:00:42 -05:00
|
|
|
saveChanges() {
|
|
|
|
if (!this.editForm.$valid) {
|
|
|
|
return;
|
2017-04-20 04:16:37 -05:00
|
|
|
}
|
2016-03-16 09:05:07 -05:00
|
|
|
|
2017-10-24 08:28:39 -05:00
|
|
|
if (this.current.readOnly) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-02 07:00:42 -05:00
|
|
|
if (this.current.id) {
|
2017-12-21 01:39:31 -06:00
|
|
|
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();
|
2017-12-21 01:39:31 -06:00
|
|
|
this.updateFrontendSettings().then(() => {
|
|
|
|
this.testDatasource();
|
2017-06-02 07:00:42 -05:00
|
|
|
});
|
2017-12-21 01:39:31 -06:00
|
|
|
});
|
2017-06-02 07:00:42 -05:00
|
|
|
} else {
|
2017-12-21 01:39:31 -06:00
|
|
|
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);
|
|
|
|
});
|
2016-03-16 09:05:07 -05:00
|
|
|
}
|
2017-06-02 07:00:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
confirmDelete() {
|
2017-12-20 05:33:33 -06:00
|
|
|
this.backendSrv.delete('/api/datasources/' + this.current.id).then(() => {
|
|
|
|
this.$location.path('datasources');
|
2017-06-02 07:00:42 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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',
|
2017-06-02 07:00:42 -05:00
|
|
|
onConfirm: () => {
|
|
|
|
this.confirmDelete();
|
2017-12-20 05:33:33 -06:00
|
|
|
},
|
2017-06-02 07:00:42 -05:00
|
|
|
});
|
|
|
|
}
|
2016-03-10 02:47:29 -06:00
|
|
|
}
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
coreModule.controller('DataSourceEditCtrl', DataSourceEditCtrl);
|
2016-03-10 02:47:29 -06:00
|
|
|
|
2018-09-05 00:47:30 -05:00
|
|
|
coreModule.directive('datasourceHttpSettings', () => {
|
2016-03-10 02:47:29 -06:00
|
|
|
return {
|
2016-06-17 06:18:00 -05:00
|
|
|
scope: {
|
2017-12-20 05:33:33 -06:00
|
|
|
current: '=',
|
|
|
|
suggestUrl: '@',
|
2018-06-04 04:17:50 -05:00
|
|
|
noDirectAccess: '@',
|
2016-06-17 06:18:00 -05:00
|
|
|
},
|
2017-12-20 05:33:33 -06:00
|
|
|
templateUrl: 'public/app/features/plugins/partials/ds_http_settings.html',
|
2016-06-17 06:18:00 -05:00
|
|
|
link: {
|
2018-09-05 00:47:30 -05:00
|
|
|
pre: ($scope, elem, attrs) => {
|
2018-06-04 04:17:50 -05:00
|
|
|
// do not show access option if direct access is disabled
|
|
|
|
$scope.showAccessOption = $scope.noDirectAccess !== 'true';
|
2018-06-04 11:05:16 -05:00
|
|
|
$scope.showAccessHelp = false;
|
2018-09-05 00:47:30 -05:00
|
|
|
$scope.toggleAccessHelp = () => {
|
2018-06-04 11:05:16 -05:00
|
|
|
$scope.showAccessHelp = !$scope.showAccessHelp;
|
|
|
|
};
|
2018-06-04 04:17:50 -05:00
|
|
|
|
2018-09-05 00:47:30 -05:00
|
|
|
$scope.getSuggestUrls = () => {
|
2016-06-17 06:35:29 -05:00
|
|
|
return [$scope.suggestUrl];
|
2016-06-17 06:18:00 -05:00
|
|
|
};
|
2017-12-20 05:33:33 -06:00
|
|
|
},
|
|
|
|
},
|
2016-03-10 02:47:29 -06:00
|
|
|
};
|
|
|
|
});
|