config_ctrl.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 (!jwt.project_id || jwt.project_id.length === 0) {
  43. this.validationErrors.push('Project Id field missing in JWT file.');
  44. }
  45. if (this.validationErrors.length === 0) {
  46. this.inputDataValid = true;
  47. return true;
  48. }
  49. return false;
  50. }
  51. onUpload(json) {
  52. this.jsonText = '';
  53. if (this.validateJwt(json)) {
  54. this.save(json);
  55. }
  56. }
  57. onPasteJwt(e) {
  58. try {
  59. const json = JSON.parse(e.originalEvent.clipboardData.getData('text/plain') || this.jsonText);
  60. if (this.validateJwt(json)) {
  61. this.save(json);
  62. }
  63. } catch (error) {
  64. this.resetValidationMessages();
  65. this.validationErrors.push(`Invalid json: ${error.message}`);
  66. }
  67. }
  68. resetValidationMessages() {
  69. this.validationErrors = [];
  70. this.inputDataValid = false;
  71. this.jsonText = '';
  72. this.current.jsonData = Object.assign({}, { authenticationType: this.current.jsonData.authenticationType });
  73. this.current.secureJsonData = {};
  74. this.current.secureJsonFields = {};
  75. }
  76. }