edit_ctrl.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. ///<reference path="../../headers/common.d.ts" />
  2. import angular from 'angular';
  3. import _ from 'lodash';
  4. import coreModule from 'app/core/core_module';
  5. import config from 'app/core/config';
  6. var datasourceTypes = [];
  7. var defaults = {
  8. name: '',
  9. type: 'graphite',
  10. url: '',
  11. access: 'proxy',
  12. jsonData: {}
  13. };
  14. export class DataSourceEditCtrl {
  15. isNew: boolean;
  16. datasources: any[];
  17. current: any;
  18. types: any;
  19. testing: any;
  20. datasourceMeta: any;
  21. tabIndex: number;
  22. hasDashboards: boolean;
  23. /** @ngInject */
  24. constructor(
  25. private $scope,
  26. private $q,
  27. private backendSrv,
  28. private $routeParams,
  29. private $location,
  30. private datasourceSrv) {
  31. this.isNew = true;
  32. this.datasources = [];
  33. this.tabIndex = 0;
  34. this.loadDatasourceTypes().then(() => {
  35. if (this.$routeParams.id) {
  36. this.getDatasourceById(this.$routeParams.id);
  37. } else {
  38. this.current = angular.copy(defaults);
  39. this.typeChanged();
  40. }
  41. });
  42. }
  43. loadDatasourceTypes() {
  44. if (datasourceTypes.length > 0) {
  45. this.types = datasourceTypes;
  46. return this.$q.when(null);
  47. }
  48. return this.backendSrv.get('/api/plugins', {enabled: 1, type: 'datasource'}).then(plugins => {
  49. datasourceTypes = plugins;
  50. this.types = plugins;
  51. });
  52. }
  53. getDatasourceById(id) {
  54. this.backendSrv.get('/api/datasources/' + id).then(ds => {
  55. this.isNew = false;
  56. this.current = ds;
  57. return this.typeChanged();
  58. });
  59. }
  60. typeChanged() {
  61. this.hasDashboards = false;
  62. return this.backendSrv.get('/api/plugins/' + this.current.type + '/settings').then(pluginInfo => {
  63. this.datasourceMeta = pluginInfo;
  64. this.hasDashboards = _.findWhere(pluginInfo.includes, {type: 'dashboard'});
  65. });
  66. }
  67. updateFrontendSettings() {
  68. return this.backendSrv.get('/api/frontend/settings').then(settings => {
  69. config.datasources = settings.datasources;
  70. config.defaultDatasource = settings.defaultDatasource;
  71. this.datasourceSrv.init();
  72. });
  73. }
  74. testDatasource() {
  75. this.testing = { done: false };
  76. this.datasourceSrv.get(this.current.name).then(datasource => {
  77. if (!datasource.testDatasource) {
  78. this.testing.message = 'Data source does not support test connection feature.';
  79. this.testing.status = 'warning';
  80. this.testing.title = 'Unknown';
  81. return;
  82. }
  83. return datasource.testDatasource().then(result => {
  84. this.testing.message = result.message;
  85. this.testing.status = result.status;
  86. this.testing.title = result.title;
  87. }).catch(err => {
  88. if (err.statusText) {
  89. this.testing.message = err.statusText;
  90. this.testing.title = "HTTP Error";
  91. } else {
  92. this.testing.message = err.message;
  93. this.testing.title = "Unknown error";
  94. }
  95. });
  96. }).finally(() => {
  97. this.testing.done = true;
  98. });
  99. }
  100. saveChanges(test) {
  101. if (!this.$scope.editForm.$valid) {
  102. return;
  103. }
  104. if (this.current.id) {
  105. return this.backendSrv.put('/api/datasources/' + this.current.id, this.current).then(() => {
  106. this.updateFrontendSettings().then(() => {
  107. if (test) {
  108. this.testDatasource();
  109. }
  110. });
  111. });
  112. } else {
  113. return this.backendSrv.post('/api/datasources', this.current).then(result => {
  114. this.updateFrontendSettings();
  115. this.$location.path('datasources/edit/' + result.id);
  116. });
  117. }
  118. };
  119. }
  120. coreModule.controller('DataSourceEditCtrl', DataSourceEditCtrl);
  121. coreModule.directive('datasourceHttpSettings', function() {
  122. return {
  123. scope: {current: "="},
  124. templateUrl: 'public/app/features/datasources/partials/http_settings.html'
  125. };
  126. });