datasourceEditCtrl.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. define([
  2. 'angular',
  3. 'config',
  4. ],
  5. function (angular, config) {
  6. 'use strict';
  7. var module = angular.module('grafana.controllers');
  8. var datasourceTypes = [];
  9. module.controller('DataSourceEditCtrl', function($scope, $q, backendSrv, $routeParams, $location, datasourceSrv) {
  10. $scope.httpConfigPartialSrc = 'app/features/org/partials/datasourceHttpConfig.html';
  11. var defaults = {
  12. name: '',
  13. type: 'graphite',
  14. url: '',
  15. access: 'proxy'
  16. };
  17. $scope.init = function() {
  18. $scope.isNew = true;
  19. $scope.datasources = [];
  20. $scope.loadDatasourceTypes().then(function() {
  21. if ($routeParams.id) {
  22. $scope.isNew = false;
  23. $scope.getDatasourceById($routeParams.id);
  24. } else {
  25. $scope.current = angular.copy(defaults);
  26. $scope.typeChanged();
  27. }
  28. });
  29. };
  30. $scope.loadDatasourceTypes = function() {
  31. if (datasourceTypes.length > 0) {
  32. $scope.types = datasourceTypes;
  33. return $q.when(null);
  34. }
  35. return backendSrv.get('/api/datasources/plugins').then(function(plugins) {
  36. datasourceTypes = plugins;
  37. $scope.types = plugins;
  38. });
  39. };
  40. $scope.getDatasourceById = function(id) {
  41. backendSrv.get('/api/datasources/' + id).then(function(ds) {
  42. $scope.current = ds;
  43. $scope.typeChanged();
  44. });
  45. };
  46. $scope.typeChanged = function() {
  47. $scope.datasourceMeta = $scope.types[$scope.current.type];
  48. };
  49. $scope.updateFrontendSettings = function() {
  50. backendSrv.get('/api/frontend/settings').then(function(settings) {
  51. config.datasources = settings.datasources;
  52. config.defaultDatasource = settings.defaultDatasource;
  53. datasourceSrv.init();
  54. });
  55. };
  56. $scope.update = function() {
  57. if (!$scope.editForm.$valid) {
  58. return;
  59. }
  60. backendSrv.post('/api/datasources', $scope.current).then(function() {
  61. $scope.updateFrontendSettings();
  62. $location.path("datasources");
  63. });
  64. };
  65. $scope.add = function() {
  66. if (!$scope.editForm.$valid) {
  67. return;
  68. }
  69. backendSrv.put('/api/datasources', $scope.current).then(function() {
  70. $scope.updateFrontendSettings();
  71. $location.path("datasources");
  72. });
  73. };
  74. $scope.init();
  75. });
  76. });