config_ctrl.ts 3.6 KB

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