datasourceEditCtrl.js 3.5 KB

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