ds_edit_ctrl.ts 5.2 KB

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