2015-02-14 03:04:27 -06:00
|
|
|
define([
|
|
|
|
'angular',
|
2015-09-07 01:57:46 -05:00
|
|
|
'lodash',
|
2015-10-30 08:19:02 -05:00
|
|
|
'app/core/config',
|
2015-02-14 03:04:27 -06:00
|
|
|
],
|
2015-10-30 08:19:02 -05:00
|
|
|
function (angular, _, config) {
|
2015-02-14 03:04:27 -06:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var module = angular.module('grafana.controllers');
|
2015-02-28 01:25:13 -06:00
|
|
|
var datasourceTypes = [];
|
2015-02-14 03:04:27 -06:00
|
|
|
|
2016-01-09 12:03:03 -06:00
|
|
|
module.directive('datasourceHttpSettings', function() {
|
2016-02-01 16:32:12 -06:00
|
|
|
return {
|
|
|
|
scope: {current: "="},
|
|
|
|
templateUrl: 'public/app/features/datasources/partials/http_settings.html'
|
|
|
|
};
|
2016-01-09 12:03:03 -06:00
|
|
|
});
|
2015-02-14 03:04:27 -06:00
|
|
|
|
2016-01-09 12:03:03 -06:00
|
|
|
module.controller('DataSourceEditCtrl', function($scope, $q, backendSrv, $routeParams, $location, datasourceSrv) {
|
2015-03-13 12:42:46 -05:00
|
|
|
|
2015-12-03 11:30:36 -06:00
|
|
|
var defaults = {name: '', type: 'graphite', url: '', access: 'proxy', jsonData: {}};
|
2015-09-06 09:09:42 -05:00
|
|
|
|
2015-02-14 03:04:27 -06:00
|
|
|
$scope.init = function() {
|
|
|
|
$scope.isNew = true;
|
|
|
|
$scope.datasources = [];
|
|
|
|
|
2015-02-28 01:25:13 -06:00
|
|
|
$scope.loadDatasourceTypes().then(function() {
|
|
|
|
if ($routeParams.id) {
|
|
|
|
$scope.getDatasourceById($routeParams.id);
|
|
|
|
} else {
|
|
|
|
$scope.current = angular.copy(defaults);
|
|
|
|
$scope.typeChanged();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.loadDatasourceTypes = function() {
|
|
|
|
if (datasourceTypes.length > 0) {
|
|
|
|
$scope.types = datasourceTypes;
|
|
|
|
return $q.when(null);
|
2015-02-14 03:04:27 -06:00
|
|
|
}
|
2015-02-28 01:25:13 -06:00
|
|
|
|
|
|
|
return backendSrv.get('/api/datasources/plugins').then(function(plugins) {
|
|
|
|
datasourceTypes = plugins;
|
|
|
|
$scope.types = plugins;
|
|
|
|
});
|
2015-02-14 03:04:27 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
$scope.getDatasourceById = function(id) {
|
|
|
|
backendSrv.get('/api/datasources/' + id).then(function(ds) {
|
2015-06-01 05:15:49 -05:00
|
|
|
$scope.isNew = false;
|
2015-02-14 03:04:27 -06:00
|
|
|
$scope.current = ds;
|
2016-01-09 11:07:42 -06:00
|
|
|
return $scope.typeChanged();
|
2015-02-14 03:04:27 -06:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-02-28 01:25:13 -06:00
|
|
|
$scope.typeChanged = function() {
|
|
|
|
$scope.datasourceMeta = $scope.types[$scope.current.type];
|
|
|
|
};
|
|
|
|
|
2015-02-18 07:06:44 -06:00
|
|
|
$scope.updateFrontendSettings = function() {
|
2015-06-24 14:31:16 -05:00
|
|
|
return backendSrv.get('/api/frontend/settings').then(function(settings) {
|
2015-03-31 07:31:47 -05:00
|
|
|
config.datasources = settings.datasources;
|
|
|
|
config.defaultDatasource = settings.defaultDatasource;
|
|
|
|
datasourceSrv.init();
|
2015-02-18 07:06:44 -06:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-06-01 05:15:49 -05:00
|
|
|
$scope.testDatasource = function() {
|
|
|
|
$scope.testing = { done: false };
|
2015-02-14 03:04:27 -06:00
|
|
|
|
2015-06-01 05:15:49 -05:00
|
|
|
datasourceSrv.get($scope.current.name).then(function(datasource) {
|
|
|
|
if (!datasource.testDatasource) {
|
|
|
|
$scope.testing.message = 'Data source does not support test connection feature.';
|
|
|
|
$scope.testing.status = 'warning';
|
|
|
|
$scope.testing.title = 'Unknown';
|
|
|
|
return;
|
|
|
|
}
|
2015-06-01 07:16:59 -05:00
|
|
|
|
2015-06-01 05:15:49 -05:00
|
|
|
return datasource.testDatasource().then(function(result) {
|
|
|
|
$scope.testing.message = result.message;
|
|
|
|
$scope.testing.status = result.status;
|
|
|
|
$scope.testing.title = result.title;
|
2015-06-01 07:16:59 -05:00
|
|
|
}, function(err) {
|
|
|
|
if (err.statusText) {
|
|
|
|
$scope.testing.message = err.statusText;
|
|
|
|
$scope.testing.title = "HTTP Error";
|
|
|
|
} else {
|
|
|
|
$scope.testing.message = err.message;
|
|
|
|
$scope.testing.title = "Unknown error";
|
|
|
|
}
|
2015-06-01 05:15:49 -05:00
|
|
|
});
|
|
|
|
}).finally(function() {
|
|
|
|
$scope.testing.done = true;
|
2015-02-14 03:04:27 -06:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-06-01 05:15:49 -05:00
|
|
|
$scope.saveChanges = function(test) {
|
2015-02-14 03:04:27 -06:00
|
|
|
if (!$scope.editForm.$valid) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-01 05:15:49 -05:00
|
|
|
if ($scope.current.id) {
|
|
|
|
return backendSrv.put('/api/datasources/' + $scope.current.id, $scope.current).then(function() {
|
2015-06-24 14:31:16 -05:00
|
|
|
$scope.updateFrontendSettings().then(function() {
|
|
|
|
if (test) {
|
|
|
|
$scope.testDatasource();
|
|
|
|
} else {
|
|
|
|
$location.path('datasources');
|
|
|
|
}
|
|
|
|
});
|
2015-06-01 05:15:49 -05:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return backendSrv.post('/api/datasources', $scope.current).then(function(result) {
|
|
|
|
$scope.updateFrontendSettings();
|
|
|
|
$location.path('datasources/edit/' + result.id);
|
|
|
|
});
|
|
|
|
}
|
2015-02-14 03:04:27 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
$scope.init();
|
|
|
|
});
|
|
|
|
});
|