config_ctrl.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. /** @ngInject */
  9. constructor(datasourceSrv) {
  10. this.datasourceSrv = datasourceSrv;
  11. this.current.jsonData = this.current.jsonData || {};
  12. this.current.secureJsonData = this.current.secureJsonData || {};
  13. this.current.secureJsonFields = this.current.secureJsonFields || {};
  14. }
  15. save(jwt) {
  16. this.current.secureJsonData.privateKey = jwt.private_key;
  17. this.current.jsonData.tokenUri = jwt.token_uri;
  18. this.current.jsonData.clientEmail = jwt.client_email;
  19. this.current.jsonData.defaultProject = jwt.project_id;
  20. }
  21. validateJwt(jwt) {
  22. this.resetValidationMessages();
  23. if (!jwt.private_key || jwt.private_key.length === 0) {
  24. this.validationErrors.push('Private key field missing in JWT file.');
  25. }
  26. if (!jwt.token_uri || jwt.token_uri.length === 0) {
  27. this.validationErrors.push('Token URI field missing in JWT file.');
  28. }
  29. if (!jwt.client_email || jwt.client_email.length === 0) {
  30. this.validationErrors.push('Client Email field missing in JWT file.');
  31. }
  32. if (this.validationErrors.length === 0) {
  33. this.inputDataValid = true;
  34. return true;
  35. }
  36. return false;
  37. }
  38. onUpload(json) {
  39. this.jsonText = '';
  40. if (this.validateJwt(json)) {
  41. this.save(json);
  42. }
  43. }
  44. onPasteJwt(e) {
  45. try {
  46. const json = JSON.parse(e.originalEvent.clipboardData.getData('text/plain') || this.jsonText);
  47. if (this.validateJwt(json)) {
  48. this.save(json);
  49. }
  50. } catch (error) {
  51. this.resetValidationMessages();
  52. this.validationErrors.push(`Invalid json: ${error.message}`);
  53. }
  54. }
  55. resetValidationMessages() {
  56. this.validationErrors = [];
  57. this.inputDataValid = false;
  58. this.jsonText = '';
  59. this.current.jsonData = {};
  60. this.current.secureJsonData = {};
  61. this.current.secureJsonFields = {};
  62. }
  63. }