edit_ctrl.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 {templateUrl: 'public/app/features/datasources/partials/http_settings.html'};
  12. });
  13. module.controller('DataSourceEditCtrl', function($scope, $q, backendSrv, $routeParams, $location, datasourceSrv) {
  14. var defaults = {name: '', type: 'graphite', url: '', access: 'proxy', jsonData: {}};
  15. $scope.init = function() {
  16. $scope.isNew = true;
  17. $scope.datasources = [];
  18. $scope.loadDatasourceTypes().then(function() {
  19. if ($routeParams.id) {
  20. $scope.getDatasourceById($routeParams.id);
  21. } else {
  22. $scope.current = angular.copy(defaults);
  23. $scope.typeChanged();
  24. }
  25. });
  26. };
  27. $scope.loadDatasourceTypes = function() {
  28. if (datasourceTypes.length > 0) {
  29. $scope.types = datasourceTypes;
  30. return $q.when(null);
  31. }
  32. return backendSrv.get('/api/datasources/plugins').then(function(plugins) {
  33. datasourceTypes = plugins;
  34. $scope.types = plugins;
  35. });
  36. };
  37. $scope.getDatasourceById = function(id) {
  38. backendSrv.get('/api/datasources/' + id).then(function(ds) {
  39. $scope.isNew = false;
  40. $scope.current = ds;
  41. return $scope.typeChanged();
  42. });
  43. };
  44. $scope.typeChanged = function() {
  45. $scope.datasourceMeta = $scope.types[$scope.current.type];
  46. };
  47. $scope.updateFrontendSettings = function() {
  48. return backendSrv.get('/api/frontend/settings').then(function(settings) {
  49. config.datasources = settings.datasources;
  50. config.defaultDatasource = settings.defaultDatasource;
  51. datasourceSrv.init();
  52. });
  53. };
  54. $scope.testDatasource = function() {
  55. $scope.testing = { done: false };
  56. datasourceSrv.get($scope.current.name).then(function(datasource) {
  57. if (!datasource.testDatasource) {
  58. $scope.testing.message = 'Data source does not support test connection feature.';
  59. $scope.testing.status = 'warning';
  60. $scope.testing.title = 'Unknown';
  61. return;
  62. }
  63. return datasource.testDatasource().then(function(result) {
  64. $scope.testing.message = result.message;
  65. $scope.testing.status = result.status;
  66. $scope.testing.title = result.title;
  67. }, function(err) {
  68. if (err.statusText) {
  69. $scope.testing.message = err.statusText;
  70. $scope.testing.title = "HTTP Error";
  71. } else {
  72. $scope.testing.message = err.message;
  73. $scope.testing.title = "Unknown error";
  74. }
  75. });
  76. }).finally(function() {
  77. $scope.testing.done = true;
  78. });
  79. };
  80. $scope.saveChanges = function(test) {
  81. if (!$scope.editForm.$valid) {
  82. return;
  83. }
  84. if ($scope.current.id) {
  85. return backendSrv.put('/api/datasources/' + $scope.current.id, $scope.current).then(function() {
  86. $scope.updateFrontendSettings().then(function() {
  87. if (test) {
  88. $scope.testDatasource();
  89. } else {
  90. $location.path('datasources');
  91. }
  92. });
  93. });
  94. } else {
  95. return backendSrv.post('/api/datasources', $scope.current).then(function(result) {
  96. $scope.updateFrontendSettings();
  97. $location.path('datasources/edit/' + result.id);
  98. });
  99. }
  100. };
  101. $scope.init();
  102. });
  103. });