ds_edit_ctrl.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. /** @ngInject */
  28. constructor(
  29. private $scope,
  30. private $q,
  31. private backendSrv,
  32. private $routeParams,
  33. private $location,
  34. private datasourceSrv) {
  35. this.isNew = true;
  36. this.datasources = [];
  37. this.tabIndex = 0;
  38. this.loadDatasourceTypes().then(() => {
  39. if (this.$routeParams.id) {
  40. this.getDatasourceById(this.$routeParams.id);
  41. } else {
  42. this.initNewDatasourceModel();
  43. }
  44. });
  45. }
  46. initNewDatasourceModel() {
  47. this.current = angular.copy(defaults);
  48. // We are coming from getting started
  49. if (this.$location.search().gettingstarted) {
  50. this.gettingStarted = true;
  51. this.current.isDefault = true;
  52. }
  53. this.typeChanged();
  54. }
  55. loadDatasourceTypes() {
  56. if (datasourceTypes.length > 0) {
  57. this.types = datasourceTypes;
  58. return this.$q.when(null);
  59. }
  60. return this.backendSrv.get('/api/plugins', {enabled: 1, type: 'datasource'}).then(plugins => {
  61. datasourceTypes = plugins;
  62. this.types = plugins;
  63. });
  64. }
  65. getDatasourceById(id) {
  66. this.backendSrv.get('/api/datasources/' + id).then(ds => {
  67. this.isNew = false;
  68. this.current = ds;
  69. if (datasourceCreated) {
  70. datasourceCreated = false;
  71. this.testDatasource();
  72. }
  73. return this.typeChanged();
  74. });
  75. }
  76. typeChanged() {
  77. this.hasDashboards = false;
  78. return this.backendSrv.get('/api/plugins/' + this.current.type + '/settings').then(pluginInfo => {
  79. this.datasourceMeta = pluginInfo;
  80. console.log(this.datasourceMeta) ;
  81. this.hasDashboards = _.find(pluginInfo.includes, {type: 'dashboard'});
  82. });
  83. }
  84. updateFrontendSettings() {
  85. return this.backendSrv.get('/api/frontend/settings').then(settings => {
  86. config.datasources = settings.datasources;
  87. config.defaultDatasource = settings.defaultDatasource;
  88. this.datasourceSrv.init();
  89. });
  90. }
  91. testDatasource() {
  92. this.testing = { done: false };
  93. this.datasourceSrv.get(this.current.name).then(datasource => {
  94. if (!datasource.testDatasource) {
  95. delete this.testing;
  96. return;
  97. }
  98. return datasource.testDatasource().then(result => {
  99. this.testing.message = result.message;
  100. this.testing.status = result.status;
  101. this.testing.title = result.title;
  102. }).catch(err => {
  103. if (err.statusText) {
  104. this.testing.message = err.statusText;
  105. this.testing.title = "HTTP Error";
  106. } else {
  107. this.testing.message = err.message;
  108. this.testing.title = "Unknown error";
  109. }
  110. });
  111. }).finally(() => {
  112. if (this.testing) {
  113. this.testing.done = true;
  114. }
  115. });
  116. }
  117. saveChanges() {
  118. if (!this.editForm.$valid) {
  119. return;
  120. }
  121. if (this.current.id) {
  122. return this.backendSrv.put('/api/datasources/' + this.current.id, this.current).then(() => {
  123. this.updateFrontendSettings().then(() => {
  124. this.testDatasource();
  125. });
  126. });
  127. } else {
  128. return this.backendSrv.post('/api/datasources', this.current).then(result => {
  129. this.updateFrontendSettings();
  130. datasourceCreated = true;
  131. this.$location.path('datasources/edit/' + result.id);
  132. });
  133. }
  134. };
  135. confirmDelete() {
  136. this.backendSrv.delete('/api/datasources/' + this.current.id).then(() => {
  137. this.$location.path('datasources');
  138. });
  139. }
  140. delete(s) {
  141. appEvents.emit('confirm-modal', {
  142. title: 'Delete',
  143. text: 'Are you sure you want to delete this datasource?',
  144. yesText: "Delete",
  145. icon: "fa-trash",
  146. onConfirm: () => {
  147. this.confirmDelete();
  148. }
  149. });
  150. }
  151. }
  152. coreModule.controller('DataSourceEditCtrl', DataSourceEditCtrl);
  153. coreModule.directive('datasourceHttpSettings', function() {
  154. return {
  155. scope: {
  156. current: "=",
  157. suggestUrl: "@",
  158. },
  159. templateUrl: 'public/app/features/plugins/partials/ds_http_settings.html',
  160. link: {
  161. pre: function($scope, elem, attrs) {
  162. $scope.getSuggestUrls = function() {
  163. return [$scope.suggestUrl];
  164. };
  165. }
  166. }
  167. };
  168. });