edit_ctrl.js 3.7 KB

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