ds_edit_ctrl.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. ///<reference path="../../headers/common.d.ts" />
  2. import angular from 'angular';
  3. import _ from 'lodash';
  4. import config from 'app/core/config';
  5. import {coreModule, appEvents} from 'app/core/core';
  6. var datasourceTypes = [];
  7. var defaults = {
  8. name: '',
  9. type: 'graphite',
  10. url: '',
  11. access: 'proxy',
  12. jsonData: {}
  13. };
  14. var datasourceCreated = false;
  15. export class DataSourceEditCtrl {
  16. isNew: boolean;
  17. datasources: any[];
  18. current: any;
  19. types: any;
  20. testing: any;
  21. datasourceMeta: any;
  22. tabIndex: number;
  23. hasDashboards: boolean;
  24. editForm: any;
  25. /** @ngInject */
  26. constructor(
  27. private $scope,
  28. private $q,
  29. private backendSrv,
  30. private $routeParams,
  31. private $location,
  32. private datasourceSrv) {
  33. this.isNew = true;
  34. this.datasources = [];
  35. this.tabIndex = 0;
  36. this.loadDatasourceTypes().then(() => {
  37. if (this.$routeParams.id) {
  38. this.getDatasourceById(this.$routeParams.id);
  39. } else {
  40. this.current = angular.copy(defaults);
  41. this.typeChanged();
  42. }
  43. });
  44. }
  45. loadDatasourceTypes() {
  46. if (datasourceTypes.length > 0) {
  47. this.types = datasourceTypes;
  48. return this.$q.when(null);
  49. }
  50. return this.backendSrv.get('/api/plugins', {enabled: 1, type: 'datasource'}).then(plugins => {
  51. datasourceTypes = plugins;
  52. this.types = plugins;
  53. });
  54. }
  55. getDatasourceById(id) {
  56. this.backendSrv.get('/api/datasources/' + id).then(ds => {
  57. this.isNew = false;
  58. this.current = ds;
  59. if (datasourceCreated) {
  60. datasourceCreated = false;
  61. this.testDatasource();
  62. }
  63. return this.typeChanged();
  64. });
  65. }
  66. typeChanged() {
  67. this.hasDashboards = false;
  68. return this.backendSrv.get('/api/plugins/' + this.current.type + '/settings').then(pluginInfo => {
  69. this.datasourceMeta = pluginInfo;
  70. this.hasDashboards = _.find(pluginInfo.includes, {type: 'dashboard'});
  71. });
  72. }
  73. updateFrontendSettings() {
  74. return this.backendSrv.get('/api/frontend/settings').then(settings => {
  75. config.datasources = settings.datasources;
  76. config.defaultDatasource = settings.defaultDatasource;
  77. this.datasourceSrv.init();
  78. });
  79. }
  80. testDatasource() {
  81. this.testing = { done: false };
  82. this.datasourceSrv.get(this.current.name).then(datasource => {
  83. if (!datasource.testDatasource) {
  84. delete this.testing;
  85. return;
  86. }
  87. return datasource.testDatasource().then(result => {
  88. this.testing.message = result.message;
  89. this.testing.status = result.status;
  90. this.testing.title = result.title;
  91. }).catch(err => {
  92. if (err.statusText) {
  93. this.testing.message = err.statusText;
  94. this.testing.title = "HTTP Error";
  95. } else {
  96. this.testing.message = err.message;
  97. this.testing.title = "Unknown error";
  98. }
  99. });
  100. }).finally(() => {
  101. if (this.testing) {
  102. this.testing.done = true;
  103. }
  104. });
  105. }
  106. saveChanges() {
  107. if (!this.editForm.$valid) {
  108. return;
  109. }
  110. if (this.current.id) {
  111. return this.backendSrv.put('/api/datasources/' + this.current.id, this.current).then(() => {
  112. this.updateFrontendSettings().then(() => {
  113. this.testDatasource();
  114. });
  115. });
  116. } else {
  117. return this.backendSrv.post('/api/datasources', this.current).then(result => {
  118. this.updateFrontendSettings();
  119. datasourceCreated = true;
  120. this.$location.path('datasources/edit/' + result.id);
  121. });
  122. }
  123. };
  124. confirmDelete() {
  125. this.backendSrv.delete('/api/datasources/' + this.current.id).then(() => {
  126. this.$location.path('datasources');
  127. });
  128. }
  129. delete(s) {
  130. appEvents.emit('confirm-modal', {
  131. title: 'Delete',
  132. text: 'Are you sure you want to delete this datasource?',
  133. yesText: "Delete",
  134. icon: "fa-trash",
  135. onConfirm: () => {
  136. this.confirmDelete();
  137. }
  138. });
  139. }
  140. }
  141. coreModule.controller('DataSourceEditCtrl', DataSourceEditCtrl);
  142. coreModule.directive('datasourceHttpSettings', function() {
  143. return {
  144. scope: {
  145. current: "=",
  146. suggestUrl: "@",
  147. },
  148. templateUrl: 'public/app/features/plugins/partials/ds_http_settings.html',
  149. link: {
  150. pre: function($scope, elem, attrs) {
  151. $scope.getSuggestUrls = function() {
  152. return [$scope.suggestUrl];
  153. };
  154. }
  155. }
  156. };
  157. });