ds_edit_ctrl.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 = angular.copy(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. typeChanged() {
  86. this.hasDashboards = false;
  87. return this.backendSrv.get('/api/plugins/' + this.current.type + '/settings').then(pluginInfo => {
  88. this.datasourceMeta = pluginInfo;
  89. console.log(this.datasourceMeta) ;
  90. this.hasDashboards = _.find(pluginInfo.includes, {type: 'dashboard'});
  91. });
  92. }
  93. updateFrontendSettings() {
  94. return this.backendSrv.get('/api/frontend/settings').then(settings => {
  95. config.datasources = settings.datasources;
  96. config.defaultDatasource = settings.defaultDatasource;
  97. this.datasourceSrv.init();
  98. });
  99. }
  100. testDatasource() {
  101. this.testing = { done: false };
  102. this.datasourceSrv.get(this.current.name).then(datasource => {
  103. if (!datasource.testDatasource) {
  104. delete this.testing;
  105. return;
  106. }
  107. return datasource.testDatasource().then(result => {
  108. this.testing.message = result.message;
  109. this.testing.status = result.status;
  110. this.testing.title = result.title;
  111. }).catch(err => {
  112. if (err.statusText) {
  113. this.testing.message = err.statusText;
  114. this.testing.title = "HTTP Error";
  115. } else {
  116. this.testing.message = err.message;
  117. this.testing.title = "Unknown error";
  118. }
  119. });
  120. }).finally(() => {
  121. if (this.testing) {
  122. this.testing.done = true;
  123. }
  124. });
  125. }
  126. saveChanges() {
  127. if (!this.editForm.$valid) {
  128. return;
  129. }
  130. if (this.current.id) {
  131. return this.backendSrv.put('/api/datasources/' + this.current.id, this.current).then(() => {
  132. this.updateFrontendSettings().then(() => {
  133. this.testDatasource();
  134. });
  135. });
  136. } else {
  137. return this.backendSrv.post('/api/datasources', this.current).then(result => {
  138. this.updateFrontendSettings();
  139. datasourceCreated = true;
  140. this.$location.path('datasources/edit/' + result.id);
  141. });
  142. }
  143. }
  144. confirmDelete() {
  145. this.backendSrv.delete('/api/datasources/' + this.current.id).then(() => {
  146. this.$location.path('datasources');
  147. });
  148. }
  149. delete(s) {
  150. appEvents.emit('confirm-modal', {
  151. title: 'Delete',
  152. text: 'Are you sure you want to delete this datasource?',
  153. yesText: "Delete",
  154. icon: "fa-trash",
  155. onConfirm: () => {
  156. this.confirmDelete();
  157. }
  158. });
  159. }
  160. }
  161. coreModule.controller('DataSourceEditCtrl', DataSourceEditCtrl);
  162. coreModule.directive('datasourceHttpSettings', function() {
  163. return {
  164. scope: {
  165. current: "=",
  166. suggestUrl: "@",
  167. },
  168. templateUrl: 'public/app/features/plugins/partials/ds_http_settings.html',
  169. link: {
  170. pre: function($scope, elem, attrs) {
  171. $scope.getSuggestUrls = function() {
  172. return [$scope.suggestUrl];
  173. };
  174. }
  175. }
  176. };
  177. });