config_ctrl.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. projectsError: string;
  10. projects: any[];
  11. loadingProjects: boolean;
  12. /** @ngInject */
  13. constructor(private $scope, datasourceSrv) {
  14. this.datasourceSrv = datasourceSrv;
  15. this.current.jsonData = this.current.jsonData || {};
  16. this.current.secureJsonData = this.current.secureJsonData || {};
  17. this.current.secureJsonFields = this.current.secureJsonFields || {};
  18. console.log(this.current.secureJsonFields.privateKey);
  19. this.defaultProject = this.current.jsonData.defaultProject;
  20. this.projects = [];
  21. }
  22. save(jwt) {
  23. this.current.secureJsonData.privateKey = jwt.private_key;
  24. this.current.jsonData.tokenUri = jwt.token_uri;
  25. this.current.jsonData.clientEmail = jwt.client_email;
  26. }
  27. validateJwt(jwt) {
  28. this.resetValidationMessages();
  29. if (!jwt.private_key || jwt.private_key.length === 0) {
  30. this.validationErrors.push('Private key field missing in JWT file.');
  31. }
  32. if (!jwt.token_uri || jwt.token_uri.length === 0) {
  33. this.validationErrors.push('Token URI field missing in JWT file.');
  34. }
  35. if (!jwt.client_email || jwt.client_email.length === 0) {
  36. this.validationErrors.push('Client Email field missing in JWT file.');
  37. }
  38. if (this.validationErrors.length === 0) {
  39. this.inputDataValid = true;
  40. return true;
  41. } else {
  42. return false;
  43. }
  44. }
  45. onUpload(json) {
  46. this.jsonText = '';
  47. if (this.validateJwt(json)) {
  48. this.save(json);
  49. this.displayProjects();
  50. }
  51. }
  52. onPasteJwt(e) {
  53. try {
  54. const json = JSON.parse(e.originalEvent.clipboardData.getData('text/plain') || this.jsonText);
  55. if (this.validateJwt(json)) {
  56. this.save(json);
  57. this.displayProjects();
  58. }
  59. } catch (error) {
  60. this.resetValidationMessages();
  61. this.validationErrors.push(`Invalid json: ${error.message}`);
  62. }
  63. }
  64. resetValidationMessages() {
  65. this.validationErrors = [];
  66. this.inputDataValid = false;
  67. this.jsonText = '';
  68. this.loadingProjects = false;
  69. this.projectsError = '';
  70. this.current.jsonData = {};
  71. this.current.secureJsonData = {};
  72. this.current.secureJsonFields = {};
  73. }
  74. async displayProjects() {
  75. if (this.projects.length === 0) {
  76. try {
  77. this.loadingProjects = true;
  78. const ds = await this.datasourceSrv.loadDatasource(this.current.name);
  79. this.projects = await ds.getProjects();
  80. this.$scope.$apply(() => {
  81. if (this.projects.length > 0) {
  82. this.current.jsonData.defaultProject = this.current.jsonData.defaultProject || this.projects[0].id;
  83. }
  84. });
  85. } catch (error) {
  86. let message = 'Projects cannot be fetched: ';
  87. message += error.statusText ? error.statusText + ': ' : '';
  88. if (error && error.data && error.data.error && error.data.error.message) {
  89. if (error.data.error.code === 403) {
  90. message += `
  91. A list of projects could not be fetched from the Google Cloud Resource Manager API.
  92. You might need to enable it first:
  93. https://console.developers.google.com/apis/library/cloudresourcemanager.googleapis.com`;
  94. } else {
  95. message += error.data.error.code + '. ' + error.data.error.message;
  96. }
  97. } else {
  98. message += 'Cannot connect to Stackdriver API';
  99. }
  100. this.$scope.$apply(() => (this.projectsError = message));
  101. } finally {
  102. this.$scope.$apply(() => (this.loadingProjects = false));
  103. }
  104. }
  105. }
  106. }