config_ctrl.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. defaultProject: string;
  9. projects: string[];
  10. loadingProjects: boolean;
  11. /** @ngInject */
  12. constructor($scope, datasourceSrv) {
  13. this.datasourceSrv = datasourceSrv;
  14. this.current.jsonData = this.current.jsonData || {};
  15. this.current.secureJsonData = this.current.secureJsonData || {};
  16. this.projects = [];
  17. }
  18. save(jwt) {
  19. this.current.secureJsonData.privateKey = jwt.private_key;
  20. this.current.jsonData.tokenUri = jwt.token_uri;
  21. this.current.jsonData.clientEmail = jwt.client_email;
  22. }
  23. validateJwt(jwt) {
  24. this.resetValidationMessages();
  25. if (!jwt.private_key || jwt.private_key.length === 0) {
  26. this.validationErrors.push('Private key field missing in JWT file.');
  27. }
  28. if (!jwt.token_uri || jwt.token_uri.length === 0) {
  29. this.validationErrors.push('Token URI field missing in JWT file.');
  30. }
  31. if (!jwt.client_email || jwt.client_email.length === 0) {
  32. this.validationErrors.push('Client Email field missing in JWT file.');
  33. }
  34. if (this.validationErrors.length === 0) {
  35. this.inputDataValid = true;
  36. return true;
  37. } else {
  38. return false;
  39. }
  40. }
  41. onUpload(json) {
  42. this.jsonText = '';
  43. if (this.validateJwt(json)) {
  44. this.save(json);
  45. this.displayProjects();
  46. }
  47. }
  48. onPasteJwt(e) {
  49. try {
  50. const json = JSON.parse(e.originalEvent.clipboardData.getData('text/plain') || this.jsonText);
  51. if (this.validateJwt(json)) {
  52. this.save(json);
  53. this.displayProjects();
  54. }
  55. } catch (error) {
  56. this.resetValidationMessages();
  57. this.validationErrors.push(`Invalid json: ${error.message}`);
  58. }
  59. }
  60. resetValidationMessages() {
  61. this.validationErrors = [];
  62. this.inputDataValid = false;
  63. this.jsonText = '';
  64. }
  65. async displayProjects() {
  66. if (this.projects.length === 0) {
  67. this.loadingProjects = true;
  68. const ds = await this.datasourceSrv.loadDatasource(this.current.name);
  69. try {
  70. this.projects = await ds.doRequest(`/cloudresourcemanager/v1/projects`);
  71. } catch (error) {
  72. console.log(error);
  73. }
  74. }
  75. }
  76. }