ds_edit_ctrl.ts 5.5 KB

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