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

226 lines
5.4 KiB
TypeScript
Raw Normal View History

2017-12-20 05:33:33 -06:00
import _ from 'lodash';
2017-12-20 05:33:33 -06:00
import config from 'app/core/config';
import { coreModule, appEvents } from 'app/core/core';
var datasourceTypes = [];
var 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: {},
};
var datasourceCreated = false;
export class DataSourceEditCtrl {
isNew: boolean;
datasources: any[];
current: any;
types: any;
testing: any;
datasourceMeta: any;
tabIndex: number;
hasDashboards: boolean;
editForm: any;
gettingStarted: boolean;
navModel: any;
/** @ngInject */
constructor(
private $q,
private backendSrv,
private $routeParams,
private $location,
private datasourceSrv,
navModelSrv
) {
2017-12-20 05:33:33 -06:00
this.navModel = navModelSrv.getNav('cfg', 'datasources', 0);
this.datasources = [];
this.tabIndex = 0;
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
2017-12-20 05:33:33 -06:00
this.navModel.breadcrumbs.push({ text: 'New' });
// 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;
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-08-15 13:53:31 -05:00
this.navModel.breadcrumbs.push(this.navModel.node);
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();
}
typeChanged() {
this.hasDashboards = false;
return this.backendSrv.get('/api/plugins/' + this.current.type + '/settings').then(pluginInfo => {
this.datasourceMeta = pluginInfo;
this.hasDashboards = _.find(pluginInfo.includes, { type: 'dashboard' }) !== undefined;
});
}
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;
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);
2017-12-20 05:33:33 -06:00
coreModule.directive('datasourceHttpSettings', function() {
return {
scope: {
2017-12-20 05:33:33 -06:00
current: '=',
suggestUrl: '@',
},
2017-12-20 05:33:33 -06:00
templateUrl: 'public/app/features/plugins/partials/ds_http_settings.html',
link: {
pre: function($scope, elem, attrs) {
$scope.getSuggestUrls = function() {
return [$scope.suggestUrl];
};
2017-12-20 05:33:33 -06:00
},
},
};
});