ds_edit_ctrl.ts 4.8 KB

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