ds_edit_ctrl.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import _ from 'lodash';
  2. import config from 'app/core/config';
  3. import { coreModule, appEvents } from 'app/core/core';
  4. import { store } from 'app/store/store';
  5. import { getNavModel } from 'app/core/selectors/navModel';
  6. import { buildNavModel } from './state/navModel';
  7. let datasourceTypes = [];
  8. const defaults = {
  9. name: '',
  10. type: 'graphite',
  11. url: '',
  12. access: 'proxy',
  13. jsonData: {},
  14. secureJsonFields: {},
  15. secureJsonData: {},
  16. };
  17. let datasourceCreated = false;
  18. export class DataSourceEditCtrl {
  19. isNew: boolean;
  20. datasources: any[];
  21. current: any;
  22. types: any;
  23. testing: any;
  24. datasourceMeta: any;
  25. editForm: any;
  26. gettingStarted: boolean;
  27. navModel: any;
  28. /** @ngInject */
  29. constructor(private $q, private backendSrv, private $routeParams, private $location, private datasourceSrv) {
  30. const state = store.getState();
  31. this.navModel = getNavModel(state.navIndex, 'datasources');
  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. this.navModel = buildNavModel(this.current, this.datasourceMeta, 'datasource-settings');
  87. }
  88. typeChanged() {
  89. return this.backendSrv.get('/api/plugins/' + this.current.type + '/settings').then(pluginInfo => {
  90. this.datasourceMeta = pluginInfo;
  91. this.updateNav();
  92. });
  93. }
  94. updateFrontendSettings() {
  95. return this.backendSrv.get('/api/frontend/settings').then(settings => {
  96. config.datasources = settings.datasources;
  97. config.defaultDatasource = settings.defaultDatasource;
  98. this.datasourceSrv.init();
  99. });
  100. }
  101. testDatasource() {
  102. this.datasourceSrv.get(this.current.name).then(datasource => {
  103. if (!datasource.testDatasource) {
  104. return;
  105. }
  106. this.testing = { done: false, status: 'error' };
  107. // make test call in no backend cache context
  108. this.backendSrv
  109. .withNoBackendCache(() => {
  110. return datasource
  111. .testDatasource()
  112. .then(result => {
  113. this.testing.message = result.message;
  114. this.testing.status = result.status;
  115. })
  116. .catch(err => {
  117. if (err.statusText) {
  118. this.testing.message = 'HTTP Error ' + err.statusText;
  119. } else {
  120. this.testing.message = err.message;
  121. }
  122. });
  123. })
  124. .finally(() => {
  125. this.testing.done = true;
  126. });
  127. });
  128. }
  129. saveChanges() {
  130. if (!this.editForm.$valid) {
  131. return;
  132. }
  133. if (this.current.readOnly) {
  134. return;
  135. }
  136. if (this.current.id) {
  137. return this.backendSrv.put('/api/datasources/' + this.current.id, this.current).then(result => {
  138. this.current = result.datasource;
  139. this.updateNav();
  140. this.updateFrontendSettings().then(() => {
  141. this.testDatasource();
  142. });
  143. });
  144. } else {
  145. return this.backendSrv.post('/api/datasources', this.current).then(result => {
  146. this.current = result.datasource;
  147. this.updateFrontendSettings();
  148. datasourceCreated = true;
  149. this.$location.path('datasources/edit/' + result.id);
  150. });
  151. }
  152. }
  153. confirmDelete() {
  154. this.backendSrv.delete('/api/datasources/' + this.current.id).then(() => {
  155. this.$location.path('datasources');
  156. });
  157. }
  158. delete(s) {
  159. appEvents.emit('confirm-modal', {
  160. title: 'Delete',
  161. text: 'Are you sure you want to delete this datasource?',
  162. yesText: 'Delete',
  163. icon: 'fa-trash',
  164. onConfirm: () => {
  165. this.confirmDelete();
  166. },
  167. });
  168. }
  169. }
  170. coreModule.controller('DataSourceEditCtrl', DataSourceEditCtrl);
  171. coreModule.directive('datasourceHttpSettings', () => {
  172. return {
  173. scope: {
  174. current: '=',
  175. suggestUrl: '@',
  176. noDirectAccess: '@',
  177. },
  178. templateUrl: 'public/app/features/plugins/partials/ds_http_settings.html',
  179. link: {
  180. pre: ($scope, elem, attrs) => {
  181. // do not show access option if direct access is disabled
  182. $scope.showAccessOption = $scope.noDirectAccess !== 'true';
  183. $scope.showAccessHelp = false;
  184. $scope.toggleAccessHelp = () => {
  185. $scope.showAccessHelp = !$scope.showAccessHelp;
  186. };
  187. $scope.getSuggestUrls = () => {
  188. return [$scope.suggestUrl];
  189. };
  190. },
  191. },
  192. };
  193. });