2018-09-04 06:21:02 -05:00
|
|
|
/** @ngInject */
|
2018-09-05 07:14:34 -05:00
|
|
|
export default class StackdriverDatasource {
|
|
|
|
url: string;
|
|
|
|
baseUrl: string;
|
|
|
|
|
|
|
|
constructor(instanceSettings, private backendSrv) {
|
2018-09-05 09:18:03 -05:00
|
|
|
this.baseUrl = `/stackdriver/`;
|
2018-09-05 07:14:34 -05:00
|
|
|
this.url = instanceSettings.url;
|
2018-09-05 11:04:51 -05:00
|
|
|
this.doRequest = this.doRequest;
|
2018-09-05 07:14:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
testDatasource() {
|
2018-09-05 09:18:03 -05:00
|
|
|
const path = `v3/projects/raintank-production/metricDescriptors`;
|
2018-09-05 07:14:34 -05:00
|
|
|
return this.doRequest(`${this.baseUrl}${path}`)
|
|
|
|
.then(response => {
|
|
|
|
if (response.status === 200) {
|
|
|
|
return {
|
|
|
|
status: 'success',
|
2018-09-05 11:04:51 -05:00
|
|
|
message: 'Successfully queried the Stackdriver API.',
|
2018-09-05 07:14:34 -05:00
|
|
|
title: 'Success',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
2018-09-05 09:18:03 -05:00
|
|
|
let message = 'Stackdriver: ';
|
2018-09-05 07:14:34 -05:00
|
|
|
message += error.statusText ? error.statusText + ': ' : '';
|
|
|
|
|
|
|
|
if (error.data && error.data.error && error.data.error.code) {
|
2018-09-05 09:18:03 -05:00
|
|
|
// 400, 401
|
2018-09-05 07:14:34 -05:00
|
|
|
message += error.data.error.code + '. ' + error.data.error.message;
|
|
|
|
} else {
|
2018-09-05 09:18:03 -05:00
|
|
|
message += 'Cannot connect to Stackdriver API';
|
2018-09-05 07:14:34 -05:00
|
|
|
}
|
|
|
|
return {
|
|
|
|
status: 'error',
|
|
|
|
message: message,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
doRequest(url, maxRetries = 1) {
|
|
|
|
return this.backendSrv
|
|
|
|
.datasourceRequest({
|
|
|
|
url: this.url + url,
|
|
|
|
method: 'GET',
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
if (maxRetries > 0) {
|
|
|
|
return this.doRequest(url, maxRetries - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
});
|
|
|
|
}
|
2018-09-04 06:21:02 -05:00
|
|
|
}
|