config_ctrl.ts 3.4 KB

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