ds_edit_ctrl.ts 5.2 KB

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