config_ctrl.ts 3.5 KB

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