config_ctrl.ts 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { find } from 'lodash';
  2. import { DatasourceSrv } from 'app/features/plugins/datasource_srv';
  3. import {
  4. createChangeHandler,
  5. createResetHandler,
  6. PasswordFieldEnum,
  7. } from '../../../features/datasources/utils/passwordHandlers';
  8. export class PostgresConfigCtrl {
  9. static templateUrl = 'partials/config.html';
  10. // Set through angular bindings
  11. declare current: any;
  12. datasourceSrv: any;
  13. showTimescaleDBHelp: boolean;
  14. onPasswordReset: ReturnType<typeof createResetHandler>;
  15. onPasswordChange: ReturnType<typeof createChangeHandler>;
  16. /** @ngInject */
  17. constructor($scope: any, datasourceSrv: DatasourceSrv) {
  18. this.current = $scope.ctrl.current;
  19. this.datasourceSrv = datasourceSrv;
  20. this.current.jsonData.sslmode = this.current.jsonData.sslmode || 'verify-full';
  21. this.current.jsonData.tlsConfigurationMethod = this.current.jsonData.tlsConfigurationMethod || 'file-path';
  22. this.current.jsonData.postgresVersion = this.current.jsonData.postgresVersion || 903;
  23. this.showTimescaleDBHelp = false;
  24. this.autoDetectFeatures();
  25. this.onPasswordReset = createResetHandler(this, PasswordFieldEnum.Password);
  26. this.onPasswordChange = createChangeHandler(this, PasswordFieldEnum.Password);
  27. this.tlsModeMapping();
  28. }
  29. autoDetectFeatures() {
  30. if (!this.current.id) {
  31. return;
  32. }
  33. this.datasourceSrv.loadDatasource(this.current.name).then((ds: any) => {
  34. return ds.getVersion().then((version: any) => {
  35. version = Number(version[0].text);
  36. // timescaledb is only available for 9.6+
  37. if (version >= 906) {
  38. ds.getTimescaleDBVersion().then((version: any) => {
  39. if (version.length === 1) {
  40. this.current.jsonData.timescaledb = true;
  41. }
  42. });
  43. }
  44. const major = Math.trunc(version / 100);
  45. const minor = version % 100;
  46. let name = String(major);
  47. if (version < 1000) {
  48. name = String(major) + '.' + String(minor);
  49. }
  50. if (!find(this.postgresVersions, (p: any) => p.value === version)) {
  51. this.postgresVersions.push({ name: name, value: version });
  52. }
  53. this.current.jsonData.postgresVersion = version;
  54. });
  55. });
  56. }
  57. toggleTimescaleDBHelp() {
  58. this.showTimescaleDBHelp = !this.showTimescaleDBHelp;
  59. }
  60. tlsModeMapping() {
  61. if (this.current.jsonData.sslmode === 'disable') {
  62. this.current.jsonData.tlsAuth = false;
  63. this.current.jsonData.tlsAuthWithCACert = false;
  64. this.current.jsonData.tlsSkipVerify = true;
  65. } else {
  66. this.current.jsonData.tlsAuth = true;
  67. this.current.jsonData.tlsAuthWithCACert = true;
  68. this.current.jsonData.tlsSkipVerify = false;
  69. }
  70. }
  71. // the value portion is derived from postgres server_version_num/100
  72. postgresVersions = [
  73. { name: '9.3', value: 903 },
  74. { name: '9.4', value: 904 },
  75. { name: '9.5', value: 905 },
  76. { name: '9.6', value: 906 },
  77. { name: '10', value: 1000 },
  78. { name: '11', value: 1100 },
  79. { name: '12+', value: 1200 },
  80. ];
  81. }