2017-12-20 05:33:33 -06:00
|
|
|
import _ from 'lodash';
|
2016-03-23 11:39:10 -05:00
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
import config from 'app/core/config';
|
|
|
|
import { coreModule, appEvents } from 'app/core/core';
|
2016-03-10 02:47:29 -06:00
|
|
|
|
|
|
|
var datasourceTypes = [];
|
|
|
|
|
|
|
|
var 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: {},
|
2016-03-10 02:47:29 -06:00
|
|
|
};
|
|
|
|
|
2016-04-12 10:20:03 -05:00
|
|
|
var datasourceCreated = false;
|
|
|
|
|
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-10 07:28:31 -06:00
|
|
|
tabIndex: number;
|
|
|
|
hasDashboards: boolean;
|
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 */
|
|
|
|
constructor(
|
|
|
|
private $q,
|
|
|
|
private backendSrv,
|
|
|
|
private $routeParams,
|
|
|
|
private $location,
|
2017-06-02 07:00:42 -05:00
|
|
|
private datasourceSrv,
|
2017-12-19 09:06:54 -06:00
|
|
|
navModelSrv
|
2017-06-02 07:00:42 -05:00
|
|
|
) {
|
2017-12-20 05:33:33 -06:00
|
|
|
this.navModel = navModelSrv.getNav('cfg', 'datasources', 0);
|
2017-06-02 07:00:42 -05:00
|
|
|
this.datasources = [];
|
|
|
|
this.tabIndex = 0;
|
|
|
|
|
|
|
|
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-12-20 05:33:33 -06:00
|
|
|
this.navModel.breadcrumbs.push({ text: 'New' });
|
2016-03-10 02:47:29 -06: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-12-19 09:06:54 -06:00
|
|
|
this.navModel.node = {
|
|
|
|
text: ds.name,
|
2017-12-20 05:33:33 -06:00
|
|
|
icon: 'icon-gf icon-gf-fw icon-gf-datasources',
|
|
|
|
id: 'ds-new',
|
2017-12-19 09:06:54 -06:00
|
|
|
};
|
2017-08-15 13:53:31 -05:00
|
|
|
this.navModel.breadcrumbs.push(this.navModel.node);
|
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();
|
|
|
|
}
|
|
|
|
|
2017-06-02 07:00:42 -05:00
|
|
|
typeChanged() {
|
|
|
|
this.hasDashboards = false;
|
2017-12-21 01:39:31 -06:00
|
|
|
return this.backendSrv.get('/api/plugins/' + this.current.type + '/settings').then(pluginInfo => {
|
|
|
|
this.datasourceMeta = pluginInfo;
|
|
|
|
this.hasDashboards = _.find(pluginInfo.includes, { type: 'dashboard' }) !== undefined;
|
|
|
|
});
|
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;
|
|
|
|
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
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
coreModule.directive('datasourceHttpSettings', function() {
|
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: '@',
|
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: {
|
|
|
|
pre: function($scope, elem, attrs) {
|
2016-06-17 06:35:29 -05:00
|
|
|
$scope.getSuggestUrls = function() {
|
|
|
|
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
|
|
|
};
|
|
|
|
});
|