ds_edit_ctrl.ts 5.4 KB

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