ds_edit_ctrl.ts 4.8 KB

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