edit_ctrl.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. define([
  2. 'angular',
  3. 'lodash',
  4. 'app/core/config',
  5. ],
  6. function (angular, _, config) {
  7. 'use strict';
  8. var module = angular.module('grafana.controllers');
  9. var datasourceTypes = [];
  10. module.directive('datasourceHttpSettings', function() {
  11. return {
  12. scope: {current: "="},
  13. templateUrl: 'public/app/features/datasources/partials/http_settings.html'
  14. };
  15. });
  16. module.controller('DataSourceEditCtrl', function($scope, $q, backendSrv, $routeParams, $location, datasourceSrv) {
  17. var defaults = {name: '', type: 'graphite', url: '', access: 'proxy', jsonData: {}};
  18. $scope.init = function() {
  19. $scope.isNew = true;
  20. $scope.datasources = [];
  21. $scope.loadDatasourceTypes().then(function() {
  22. if ($routeParams.id) {
  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/org/plugins', {enabled: 1, type: 'datasource'}).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.isNew = false;
  43. $scope.current = ds;
  44. return $scope.typeChanged();
  45. });
  46. };
  47. $scope.typeChanged = function() {
  48. return backendSrv.get('/api/org/plugins/' + $scope.current.type + '/settings').then(function(pluginInfo) {
  49. $scope.datasourceMeta = pluginInfo;
  50. });
  51. };
  52. $scope.updateFrontendSettings = function() {
  53. return backendSrv.get('/api/frontend/settings').then(function(settings) {
  54. config.datasources = settings.datasources;
  55. config.defaultDatasource = settings.defaultDatasource;
  56. datasourceSrv.init();
  57. });
  58. };
  59. $scope.testDatasource = function() {
  60. $scope.testing = { done: false };
  61. datasourceSrv.get($scope.current.name).then(function(datasource) {
  62. if (!datasource.testDatasource) {
  63. $scope.testing.message = 'Data source does not support test connection feature.';
  64. $scope.testing.status = 'warning';
  65. $scope.testing.title = 'Unknown';
  66. return;
  67. }
  68. return datasource.testDatasource().then(function(result) {
  69. $scope.testing.message = result.message;
  70. $scope.testing.status = result.status;
  71. $scope.testing.title = result.title;
  72. }, function(err) {
  73. if (err.statusText) {
  74. $scope.testing.message = err.statusText;
  75. $scope.testing.title = "HTTP Error";
  76. } else {
  77. $scope.testing.message = err.message;
  78. $scope.testing.title = "Unknown error";
  79. }
  80. });
  81. }).finally(function() {
  82. $scope.testing.done = true;
  83. });
  84. };
  85. $scope.saveChanges = function(test) {
  86. if (!$scope.editForm.$valid) {
  87. return;
  88. }
  89. if ($scope.current.id) {
  90. return backendSrv.put('/api/datasources/' + $scope.current.id, $scope.current).then(function() {
  91. $scope.updateFrontendSettings().then(function() {
  92. if (test) {
  93. $scope.testDatasource();
  94. }
  95. });
  96. });
  97. } else {
  98. return backendSrv.post('/api/datasources', $scope.current).then(function(result) {
  99. $scope.updateFrontendSettings();
  100. $location.path('datasources/edit/' + result.id);
  101. });
  102. }
  103. };
  104. $scope.init();
  105. });
  106. });