datasource.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /** @ngInject */
  2. export default class StackdriverDatasource {
  3. url: string;
  4. baseUrl: string;
  5. constructor(instanceSettings, private backendSrv) {
  6. this.baseUrl = `/stackdriver/`;
  7. this.url = instanceSettings.url;
  8. }
  9. testDatasource() {
  10. const path = `v3/projects/raintank-production/metricDescriptors`;
  11. return this.doRequest(`${this.baseUrl}${path}`)
  12. .then(response => {
  13. if (response.status === 200) {
  14. return {
  15. status: 'success',
  16. message: 'Successfully queried the Azure Monitor service.',
  17. title: 'Success',
  18. };
  19. }
  20. })
  21. .catch(error => {
  22. let message = 'Stackdriver: ';
  23. message += error.statusText ? error.statusText + ': ' : '';
  24. if (error.data && error.data.error && error.data.error.code) {
  25. // 400, 401
  26. message += error.data.error.code + '. ' + error.data.error.message;
  27. } else {
  28. message += 'Cannot connect to Stackdriver API';
  29. }
  30. return {
  31. status: 'error',
  32. message: message,
  33. };
  34. });
  35. }
  36. doRequest(url, maxRetries = 1) {
  37. return this.backendSrv
  38. .datasourceRequest({
  39. url: this.url + url,
  40. method: 'GET',
  41. })
  42. .catch(error => {
  43. if (maxRetries > 0) {
  44. return this.doRequest(url, maxRetries - 1);
  45. }
  46. throw error;
  47. });
  48. }
  49. }