ds_edit_ctrl.ts 3.9 KB

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