datasourceEditCtrl.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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.getDatasourceById($routeParams.id);
  23. } else {
  24. $scope.current = angular.copy(defaults);
  25. $scope.typeChanged();
  26. }
  27. });
  28. };
  29. $scope.loadDatasourceTypes = function() {
  30. if (datasourceTypes.length > 0) {
  31. $scope.types = datasourceTypes;
  32. return $q.when(null);
  33. }
  34. return backendSrv.get('/api/datasources/plugins').then(function(plugins) {
  35. datasourceTypes = plugins;
  36. $scope.types = plugins;
  37. });
  38. };
  39. $scope.getDatasourceById = function(id) {
  40. backendSrv.get('/api/datasources/' + id).then(function(ds) {
  41. $scope.isNew = false;
  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. return 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.testDatasource = function() {
  57. $scope.testing = { done: false };
  58. datasourceSrv.get($scope.current.name).then(function(datasource) {
  59. if (!datasource.testDatasource) {
  60. $scope.testing.message = 'Data source does not support test connection feature.';
  61. $scope.testing.status = 'warning';
  62. $scope.testing.title = 'Unknown';
  63. return;
  64. }
  65. return datasource.testDatasource().then(function(result) {
  66. $scope.testing.message = result.message;
  67. $scope.testing.status = result.status;
  68. $scope.testing.title = result.title;
  69. }, function(err) {
  70. if (err.statusText) {
  71. $scope.testing.message = err.statusText;
  72. $scope.testing.title = "HTTP Error";
  73. } else {
  74. $scope.testing.message = err.message;
  75. $scope.testing.title = "Unknown error";
  76. }
  77. });
  78. }).finally(function() {
  79. $scope.testing.done = true;
  80. });
  81. };
  82. $scope.saveChanges = function(test) {
  83. if (!$scope.editForm.$valid) {
  84. return;
  85. }
  86. if ($scope.current.id) {
  87. return backendSrv.put('/api/datasources/' + $scope.current.id, $scope.current).then(function() {
  88. $scope.updateFrontendSettings().then(function() {
  89. if (test) {
  90. $scope.testDatasource();
  91. } else {
  92. $location.path('datasources');
  93. }
  94. });
  95. });
  96. } else {
  97. return backendSrv.post('/api/datasources', $scope.current).then(function(result) {
  98. $scope.updateFrontendSettings();
  99. $location.path('datasources/edit/' + result.id);
  100. });
  101. }
  102. };
  103. $scope.init();
  104. });
  105. });