datasourceEditCtrl.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. define([
  2. 'angular',
  3. 'lodash',
  4. ],
  5. function (angular) {
  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.isNew = false;
  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.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. backendSrv.get('/api/frontend/settings').then(function(settings) {
  51. datasourceSrv.init(settings.datasources);
  52. });
  53. };
  54. $scope.update = function() {
  55. if (!$scope.editForm.$valid) {
  56. return;
  57. }
  58. backendSrv.post('/api/datasources', $scope.current).then(function() {
  59. $scope.updateFrontendSettings();
  60. $location.path("datasources");
  61. });
  62. };
  63. $scope.add = function() {
  64. if (!$scope.editForm.$valid) {
  65. return;
  66. }
  67. backendSrv.put('/api/datasources', $scope.current).then(function() {
  68. $scope.updateFrontendSettings();
  69. $location.path("datasources");
  70. });
  71. };
  72. $scope.init();
  73. });
  74. });