ds_edit_ctrl.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. ///<reference path="../../headers/common.d.ts" />
  2. import angular from 'angular';
  3. import _ from 'lodash';
  4. import config from 'app/core/config';
  5. import {coreModule, appEvents} from 'app/core/core';
  6. var datasourceTypes = [];
  7. var defaults = {
  8. name: '',
  9. type: 'graphite',
  10. url: '',
  11. access: 'proxy',
  12. jsonData: {},
  13. secureJsonFields: {},
  14. };
  15. var datasourceCreated = false;
  16. export class DataSourceEditCtrl {
  17. isNew: boolean;
  18. datasources: any[];
  19. current: any;
  20. types: any;
  21. testing: any;
  22. datasourceMeta: any;
  23. tabIndex: number;
  24. hasDashboards: boolean;
  25. editForm: any;
  26. gettingStarted: boolean;
  27. navModel: any;
  28. /** @ngInject */
  29. constructor(
  30. private $scope,
  31. private $q,
  32. private backendSrv,
  33. private $routeParams,
  34. private $location,
  35. private datasourceSrv,
  36. private navModelSrv,
  37. ) {
  38. this.navModel = navModelSrv.getDatasourceNav(0);
  39. this.isNew = true;
  40. this.datasources = [];
  41. this.tabIndex = 0;
  42. this.loadDatasourceTypes().then(() => {
  43. if (this.$routeParams.id) {
  44. this.getDatasourceById(this.$routeParams.id);
  45. } else {
  46. this.initNewDatasourceModel();
  47. }
  48. });
  49. }
  50. initNewDatasourceModel() {
  51. this.current = _.cloneDeep(defaults);
  52. // We are coming from getting started
  53. if (this.$location.search().gettingstarted) {
  54. this.gettingStarted = true;
  55. this.current.isDefault = true;
  56. }
  57. this.typeChanged();
  58. }
  59. loadDatasourceTypes() {
  60. if (datasourceTypes.length > 0) {
  61. this.types = datasourceTypes;
  62. return this.$q.when(null);
  63. }
  64. return this.backendSrv.get('/api/plugins', {enabled: 1, type: 'datasource'}).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. 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'});
  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.id) {
  132. return this.backendSrv.put('/api/datasources/' + this.current.id, this.current).then(() => {
  133. this.updateFrontendSettings().then(() => {
  134. this.testDatasource();
  135. });
  136. });
  137. } else {
  138. return this.backendSrv.post('/api/datasources', this.current).then(result => {
  139. this.updateFrontendSettings();
  140. datasourceCreated = true;
  141. this.$location.path('datasources/edit/' + result.id);
  142. });
  143. }
  144. }
  145. confirmDelete() {
  146. this.backendSrv.delete('/api/datasources/' + this.current.id).then(() => {
  147. this.$location.path('datasources');
  148. });
  149. }
  150. delete(s) {
  151. appEvents.emit('confirm-modal', {
  152. title: 'Delete',
  153. text: 'Are you sure you want to delete this datasource?',
  154. yesText: "Delete",
  155. icon: "fa-trash",
  156. onConfirm: () => {
  157. this.confirmDelete();
  158. }
  159. });
  160. }
  161. }
  162. coreModule.controller('DataSourceEditCtrl', DataSourceEditCtrl);
  163. coreModule.directive('datasourceHttpSettings', function() {
  164. return {
  165. scope: {
  166. current: "=",
  167. suggestUrl: "@",
  168. },
  169. templateUrl: 'public/app/features/plugins/partials/ds_http_settings.html',
  170. link: {
  171. pre: function($scope, elem, attrs) {
  172. $scope.getSuggestUrls = function() {
  173. return [$scope.suggestUrl];
  174. };
  175. }
  176. }
  177. };
  178. });