ds_edit_ctrl.ts 5.2 KB

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