ds_edit_ctrl.ts 5.4 KB

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