ds_edit_ctrl.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. import _ from 'lodash';
  2. import { toJS } from 'mobx';
  3. import config from 'app/core/config';
  4. import { coreModule, appEvents } from 'app/core/core';
  5. import { store } from 'app/stores/store';
  6. let datasourceTypes = [];
  7. const defaults = {
  8. name: '',
  9. type: 'graphite',
  10. url: '',
  11. access: 'proxy',
  12. jsonData: {},
  13. secureJsonFields: {},
  14. secureJsonData: {},
  15. };
  16. let datasourceCreated = false;
  17. export class DataSourceEditCtrl {
  18. isNew: boolean;
  19. datasources: any[];
  20. current: any;
  21. types: any;
  22. testing: any;
  23. datasourceMeta: any;
  24. editForm: any;
  25. gettingStarted: boolean;
  26. navModel: any;
  27. /** @ngInject */
  28. constructor(private $q, private backendSrv, private $routeParams, private $location, private datasourceSrv) {
  29. if (store.nav.main === null) {
  30. store.nav.load('cfg', 'datasources');
  31. }
  32. this.navModel = toJS(store.nav);
  33. this.datasources = [];
  34. this.loadDatasourceTypes().then(() => {
  35. if (this.$routeParams.id) {
  36. this.getDatasourceById(this.$routeParams.id);
  37. } else {
  38. this.initNewDatasourceModel();
  39. }
  40. });
  41. }
  42. initNewDatasourceModel() {
  43. this.isNew = true;
  44. this.current = _.cloneDeep(defaults);
  45. // We are coming from getting started
  46. if (this.$location.search().gettingstarted) {
  47. this.gettingStarted = true;
  48. this.current.isDefault = true;
  49. }
  50. this.typeChanged();
  51. }
  52. loadDatasourceTypes() {
  53. if (datasourceTypes.length > 0) {
  54. this.types = datasourceTypes;
  55. return this.$q.when(null);
  56. }
  57. return this.backendSrv.get('/api/plugins', { enabled: 1, type: 'datasource' }).then(plugins => {
  58. datasourceTypes = plugins;
  59. this.types = plugins;
  60. });
  61. }
  62. getDatasourceById(id) {
  63. this.backendSrv.get('/api/datasources/' + id).then(ds => {
  64. this.isNew = false;
  65. this.current = ds;
  66. if (datasourceCreated) {
  67. datasourceCreated = false;
  68. this.testDatasource();
  69. }
  70. return this.typeChanged();
  71. });
  72. }
  73. userChangedType() {
  74. // reset model but keep name & default flag
  75. this.current = _.defaults(
  76. {
  77. id: this.current.id,
  78. name: this.current.name,
  79. isDefault: this.current.isDefault,
  80. type: this.current.type,
  81. },
  82. _.cloneDeep(defaults)
  83. );
  84. this.typeChanged();
  85. }
  86. updateNav() {
  87. store.nav.initDatasourceEditNav(this.current, this.datasourceMeta, 'datasource-settings');
  88. this.navModel = toJS(store.nav);
  89. }
  90. typeChanged() {
  91. return this.backendSrv.get('/api/plugins/' + this.current.type + '/settings').then(pluginInfo => {
  92. this.datasourceMeta = pluginInfo;
  93. this.updateNav();
  94. });
  95. }
  96. updateFrontendSettings() {
  97. return this.backendSrv.get('/api/frontend/settings').then(settings => {
  98. config.datasources = settings.datasources;
  99. config.defaultDatasource = settings.defaultDatasource;
  100. this.datasourceSrv.init();
  101. });
  102. }
  103. testDatasource() {
  104. this.datasourceSrv.get(this.current.name).then(datasource => {
  105. if (!datasource.testDatasource) {
  106. return;
  107. }
  108. this.testing = { done: false, status: 'error' };
  109. // make test call in no backend cache context
  110. this.backendSrv
  111. .withNoBackendCache(() => {
  112. return datasource
  113. .testDatasource()
  114. .then(result => {
  115. this.testing.message = result.message;
  116. this.testing.status = result.status;
  117. })
  118. .catch(err => {
  119. if (err.statusText) {
  120. this.testing.message = 'HTTP Error ' + err.statusText;
  121. } else {
  122. this.testing.message = err.message;
  123. }
  124. });
  125. })
  126. .finally(() => {
  127. this.testing.done = true;
  128. });
  129. });
  130. }
  131. saveChanges() {
  132. if (!this.editForm.$valid) {
  133. return;
  134. }
  135. if (this.current.readOnly) {
  136. return;
  137. }
  138. if (this.current.id) {
  139. return this.backendSrv.put('/api/datasources/' + this.current.id, this.current).then(result => {
  140. this.current = result.datasource;
  141. this.updateNav();
  142. this.updateFrontendSettings().then(() => {
  143. this.testDatasource();
  144. });
  145. });
  146. } else {
  147. return this.backendSrv.post('/api/datasources', this.current).then(result => {
  148. this.current = result.datasource;
  149. this.updateFrontendSettings();
  150. datasourceCreated = true;
  151. this.$location.path('datasources/edit/' + result.id);
  152. });
  153. }
  154. }
  155. confirmDelete() {
  156. this.backendSrv.delete('/api/datasources/' + this.current.id).then(() => {
  157. this.$location.path('datasources');
  158. });
  159. }
  160. delete(s) {
  161. appEvents.emit('confirm-modal', {
  162. title: 'Delete',
  163. text: 'Are you sure you want to delete this datasource?',
  164. yesText: 'Delete',
  165. icon: 'fa-trash',
  166. onConfirm: () => {
  167. this.confirmDelete();
  168. },
  169. });
  170. }
  171. }
  172. coreModule.controller('DataSourceEditCtrl', DataSourceEditCtrl);
  173. coreModule.directive('datasourceHttpSettings', function() {
  174. return {
  175. scope: {
  176. current: '=',
  177. suggestUrl: '@',
  178. noDirectAccess: '@',
  179. },
  180. templateUrl: 'public/app/features/plugins/partials/ds_http_settings.html',
  181. link: {
  182. pre: function($scope, elem, attrs) {
  183. // do not show access option if direct access is disabled
  184. $scope.showAccessOption = $scope.noDirectAccess !== 'true';
  185. $scope.showAccessHelp = false;
  186. $scope.toggleAccessHelp = function() {
  187. $scope.showAccessHelp = !$scope.showAccessHelp;
  188. };
  189. $scope.getSuggestUrls = function() {
  190. return [$scope.suggestUrl];
  191. };
  192. },
  193. },
  194. };
  195. });