ds_edit_ctrl.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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.getNav('cfg', 'datasources');
  39. this.datasources = [];
  40. this.tabIndex = 0;
  41. this.loadDatasourceTypes().then(() => {
  42. if (this.$routeParams.id) {
  43. this.getDatasourceById(this.$routeParams.id);
  44. } else {
  45. this.initNewDatasourceModel();
  46. }
  47. });
  48. }
  49. initNewDatasourceModel() {
  50. this.isNew = true;
  51. this.current = _.cloneDeep(defaults);
  52. // add to nav & breadcrumbs
  53. this.navModel.node = {text: 'New data source', icon: 'icon-gf icon-gf-fw icon-gf-datasources'};
  54. this.navModel.breadcrumbs.push(this.navModel.node);
  55. // We are coming from getting started
  56. if (this.$location.search().gettingstarted) {
  57. this.gettingStarted = true;
  58. this.current.isDefault = true;
  59. }
  60. this.typeChanged();
  61. }
  62. loadDatasourceTypes() {
  63. if (datasourceTypes.length > 0) {
  64. this.types = datasourceTypes;
  65. return this.$q.when(null);
  66. }
  67. return this.backendSrv.get('/api/plugins', {enabled: 1, type: 'datasource'}).then(plugins => {
  68. datasourceTypes = plugins;
  69. this.types = plugins;
  70. });
  71. }
  72. getDatasourceById(id) {
  73. this.backendSrv.get('/api/datasources/' + id).then(ds => {
  74. this.isNew = false;
  75. this.current = ds;
  76. this.navModel.node = {text: ds.name, icon: 'icon-gf icon-gf-fw icon-gf-datasources'};
  77. this.navModel.breadcrumbs.push(this.navModel.node);
  78. if (datasourceCreated) {
  79. datasourceCreated = false;
  80. this.testDatasource();
  81. }
  82. return this.typeChanged();
  83. });
  84. }
  85. userChangedType() {
  86. // reset model but keep name & default flag
  87. this.current = _.defaults({
  88. id: this.current.id,
  89. name: this.current.name,
  90. isDefault: this.current.isDefault,
  91. type: this.current.type,
  92. }, _.cloneDeep(defaults));
  93. this.typeChanged();
  94. }
  95. typeChanged() {
  96. this.hasDashboards = false;
  97. return this.backendSrv.get('/api/plugins/' + this.current.type + '/settings').then(pluginInfo => {
  98. this.datasourceMeta = pluginInfo;
  99. this.hasDashboards = _.find(pluginInfo.includes, {type: 'dashboard'});
  100. });
  101. }
  102. updateFrontendSettings() {
  103. return this.backendSrv.get('/api/frontend/settings').then(settings => {
  104. config.datasources = settings.datasources;
  105. config.defaultDatasource = settings.defaultDatasource;
  106. this.datasourceSrv.init();
  107. });
  108. }
  109. testDatasource() {
  110. this.datasourceSrv.get(this.current.name).then(datasource => {
  111. if (!datasource.testDatasource) {
  112. return;
  113. }
  114. this.testing = {done: false, status: 'error'};
  115. // make test call in no backend cache context
  116. this.backendSrv.withNoBackendCache(() => {
  117. return datasource.testDatasource().then(result => {
  118. this.testing.message = result.message;
  119. this.testing.status = result.status;
  120. }).catch(err => {
  121. if (err.statusText) {
  122. this.testing.message = 'HTTP Error ' + err.statusText;
  123. } else {
  124. this.testing.message = err.message;
  125. }
  126. });
  127. }).finally(() => {
  128. this.testing.done = true;
  129. });
  130. });
  131. }
  132. saveChanges() {
  133. if (!this.editForm.$valid) {
  134. return;
  135. }
  136. if (this.current.id) {
  137. return this.backendSrv.put('/api/datasources/' + this.current.id, this.current).then(() => {
  138. this.updateFrontendSettings().then(() => {
  139. this.testDatasource();
  140. });
  141. });
  142. } else {
  143. return this.backendSrv.post('/api/datasources', this.current).then(result => {
  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. });