config_ctrl.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. export class StackdriverConfigCtrl {
  2. static templateUrl = 'public/app/plugins/datasource/stackdriver/partials/config.html';
  3. datasourceSrv: any;
  4. current: any;
  5. jsonText: string;
  6. validationErrors: string[] = [];
  7. inputDataValid: boolean;
  8. authenticationTypes: any[];
  9. defaultAuthenticationType: string;
  10. /** @ngInject */
  11. constructor(datasourceSrv) {
  12. this.defaultAuthenticationType = 'jwt';
  13. this.datasourceSrv = datasourceSrv;
  14. this.current.jsonData = this.current.jsonData || {};
  15. this.current.jsonData.authenticationType = this.current.jsonData.authenticationType
  16. ? this.current.jsonData.authenticationType
  17. : this.defaultAuthenticationType;
  18. this.current.secureJsonData = this.current.secureJsonData || {};
  19. this.current.secureJsonFields = this.current.secureJsonFields || {};
  20. this.authenticationTypes = [
  21. { key: this.defaultAuthenticationType, value: 'Google JWT File' },
  22. { key: 'gce', value: 'Use GCE default Authentication' },
  23. ];
  24. }
  25. save(jwt) {
  26. this.current.secureJsonData.privateKey = jwt.private_key;
  27. this.current.jsonData.tokenUri = jwt.token_uri;
  28. this.current.jsonData.clientEmail = jwt.client_email;
  29. this.current.jsonData.defaultProject = jwt.project_id;
  30. }
  31. validateJwt(jwt) {
  32. this.resetValidationMessages();
  33. if (!jwt.private_key || jwt.private_key.length === 0) {
  34. this.validationErrors.push('Private key field missing in JWT file.');
  35. }
  36. if (!jwt.token_uri || jwt.token_uri.length === 0) {
  37. this.validationErrors.push('Token URI field missing in JWT file.');
  38. }
  39. if (!jwt.client_email || jwt.client_email.length === 0) {
  40. this.validationErrors.push('Client Email field missing in JWT file.');
  41. }
  42. if (this.validationErrors.length === 0) {
  43. this.inputDataValid = true;
  44. return true;
  45. }
  46. return false;
  47. }
  48. onUpload(json) {
  49. this.jsonText = '';
  50. if (this.validateJwt(json)) {
  51. this.save(json);
  52. }
  53. }
  54. onPasteJwt(e) {
  55. try {
  56. const json = JSON.parse(e.originalEvent.clipboardData.getData('text/plain') || this.jsonText);
  57. if (this.validateJwt(json)) {
  58. this.save(json);
  59. }
  60. } catch (error) {
  61. this.resetValidationMessages();
  62. this.validationErrors.push(`Invalid json: ${error.message}`);
  63. }
  64. }
  65. resetValidationMessages() {
  66. this.validationErrors = [];
  67. this.inputDataValid = false;
  68. this.jsonText = '';
  69. this.current.jsonData = Object.assign({}, { authenticationType: this.current.jsonData.authenticationType });
  70. this.current.secureJsonData = {};
  71. this.current.secureJsonFields = {};
  72. }
  73. }