config_ctrl.ts 1.8 KB

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