ds_edit_ctrl.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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.datasources = [];
  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.isNew = true;
  47. this.current = _.cloneDeep(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. userChangedType() {
  77. // reset model but keep name & default flag
  78. this.current = _.defaults(
  79. {
  80. id: this.current.id,
  81. name: this.current.name,
  82. isDefault: this.current.isDefault,
  83. type: this.current.type,
  84. },
  85. _.cloneDeep(defaults)
  86. );
  87. this.typeChanged();
  88. }
  89. updateNav() {
  90. store.nav.initDatasourceEditNav(this.current, this.datasourceMeta, 'datasource-settings');
  91. this.navModel = toJS(store.nav);
  92. }
  93. typeChanged() {
  94. return this.backendSrv.get('/api/plugins/' + this.current.type + '/settings').then(pluginInfo => {
  95. this.datasourceMeta = pluginInfo;
  96. this.updateNav();
  97. });
  98. }
  99. updateFrontendSettings() {
  100. return this.backendSrv.get('/api/frontend/settings').then(settings => {
  101. config.datasources = settings.datasources;
  102. config.defaultDatasource = settings.defaultDatasource;
  103. this.datasourceSrv.init();
  104. });
  105. }
  106. testDatasource() {
  107. this.datasourceSrv.get(this.current.name).then(datasource => {
  108. if (!datasource.testDatasource) {
  109. return;
  110. }
  111. this.testing = { done: false, status: 'error' };
  112. // make test call in no backend cache context
  113. this.backendSrv
  114. .withNoBackendCache(() => {
  115. return datasource
  116. .testDatasource()
  117. .then(result => {
  118. this.testing.message = result.message;
  119. this.testing.status = result.status;
  120. })
  121. .catch(err => {
  122. if (err.statusText) {
  123. this.testing.message = 'HTTP Error ' + err.statusText;
  124. } else {
  125. this.testing.message = err.message;
  126. }
  127. });
  128. })
  129. .finally(() => {
  130. this.testing.done = true;
  131. });
  132. });
  133. }
  134. saveChanges() {
  135. if (!this.editForm.$valid) {
  136. return;
  137. }
  138. if (this.current.readOnly) {
  139. return;
  140. }
  141. if (this.current.id) {
  142. return this.backendSrv.put('/api/datasources/' + this.current.id, this.current).then(result => {
  143. this.current = result.datasource;
  144. this.updateNav();
  145. this.updateFrontendSettings().then(() => {
  146. this.testDatasource();
  147. });
  148. });
  149. } else {
  150. return this.backendSrv.post('/api/datasources', this.current).then(result => {
  151. this.current = result.datasource;
  152. this.updateFrontendSettings();
  153. datasourceCreated = true;
  154. this.$location.path('datasources/edit/' + result.id);
  155. });
  156. }
  157. }
  158. confirmDelete() {
  159. this.backendSrv.delete('/api/datasources/' + this.current.id).then(() => {
  160. this.$location.path('datasources');
  161. });
  162. }
  163. delete(s) {
  164. appEvents.emit('confirm-modal', {
  165. title: 'Delete',
  166. text: 'Are you sure you want to delete this datasource?',
  167. yesText: 'Delete',
  168. icon: 'fa-trash',
  169. onConfirm: () => {
  170. this.confirmDelete();
  171. },
  172. });
  173. }
  174. }
  175. coreModule.controller('DataSourceEditCtrl', DataSourceEditCtrl);
  176. coreModule.directive('datasourceHttpSettings', function() {
  177. return {
  178. scope: {
  179. current: '=',
  180. suggestUrl: '@',
  181. },
  182. templateUrl: 'public/app/features/plugins/partials/ds_http_settings.html',
  183. link: {
  184. pre: function($scope, elem, attrs) {
  185. $scope.getSuggestUrls = function() {
  186. return [$scope.suggestUrl];
  187. };
  188. },
  189. },
  190. };
  191. });