edit_ctrl.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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/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.isNew = false;
  43. $scope.current = ds;
  44. return $scope.typeChanged();
  45. });
  46. };
  47. $scope.typeChanged = function() {
  48. $scope.datasourceMeta = $scope.types[$scope.current.type];
  49. };
  50. $scope.updateFrontendSettings = function() {
  51. return backendSrv.get('/api/frontend/settings').then(function(settings) {
  52. config.datasources = settings.datasources;
  53. config.defaultDatasource = settings.defaultDatasource;
  54. datasourceSrv.init();
  55. });
  56. };
  57. $scope.testDatasource = function() {
  58. $scope.testing = { done: false };
  59. datasourceSrv.get($scope.current.name).then(function(datasource) {
  60. if (!datasource.testDatasource) {
  61. $scope.testing.message = 'Data source does not support test connection feature.';
  62. $scope.testing.status = 'warning';
  63. $scope.testing.title = 'Unknown';
  64. return;
  65. }
  66. return datasource.testDatasource().then(function(result) {
  67. $scope.testing.message = result.message;
  68. $scope.testing.status = result.status;
  69. $scope.testing.title = result.title;
  70. }, function(err) {
  71. if (err.statusText) {
  72. $scope.testing.message = err.statusText;
  73. $scope.testing.title = "HTTP Error";
  74. } else {
  75. $scope.testing.message = err.message;
  76. $scope.testing.title = "Unknown error";
  77. }
  78. });
  79. }).finally(function() {
  80. $scope.testing.done = true;
  81. });
  82. };
  83. $scope.saveChanges = function(test) {
  84. if (!$scope.editForm.$valid) {
  85. return;
  86. }
  87. if ($scope.current.id) {
  88. return backendSrv.put('/api/datasources/' + $scope.current.id, $scope.current).then(function() {
  89. $scope.updateFrontendSettings().then(function() {
  90. if (test) {
  91. $scope.testDatasource();
  92. }
  93. });
  94. });
  95. } else {
  96. return backendSrv.post('/api/datasources', $scope.current).then(function(result) {
  97. $scope.updateFrontendSettings();
  98. $location.path('datasources/edit/' + result.id);
  99. });
  100. }
  101. };
  102. $scope.init();
  103. });
  104. });