ds_edit_ctrl.ts 4.9 KB

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