ds_edit_ctrl.ts 5.3 KB

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