| 
									
										
										
										
											2018-09-04 19:21:02 +08:00
										 |  |  | /** @ngInject */ | 
					
						
							| 
									
										
										
										
											2018-09-05 20:14:34 +08:00
										 |  |  | export default class StackdriverDatasource { | 
					
						
							|  |  |  |   url: string; | 
					
						
							|  |  |  |   baseUrl: string; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   constructor(instanceSettings, private backendSrv) { | 
					
						
							| 
									
										
										
										
											2018-09-05 22:18:03 +08:00
										 |  |  |     this.baseUrl = `/stackdriver/`; | 
					
						
							| 
									
										
										
										
											2018-09-05 20:14:34 +08:00
										 |  |  |     this.url = instanceSettings.url; | 
					
						
							| 
									
										
										
										
											2018-09-06 00:04:51 +08:00
										 |  |  |     this.doRequest = this.doRequest; | 
					
						
							| 
									
										
										
										
											2018-09-05 20:14:34 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   testDatasource() { | 
					
						
							| 
									
										
										
										
											2018-09-05 22:18:03 +08:00
										 |  |  |     const path = `v3/projects/raintank-production/metricDescriptors`; | 
					
						
							| 
									
										
										
										
											2018-09-05 20:14:34 +08:00
										 |  |  |     return this.doRequest(`${this.baseUrl}${path}`) | 
					
						
							|  |  |  |       .then(response => { | 
					
						
							|  |  |  |         if (response.status === 200) { | 
					
						
							|  |  |  |           return { | 
					
						
							|  |  |  |             status: 'success', | 
					
						
							| 
									
										
										
										
											2018-09-06 00:04:51 +08:00
										 |  |  |             message: 'Successfully queried the Stackdriver API.', | 
					
						
							| 
									
										
										
										
											2018-09-05 20:14:34 +08:00
										 |  |  |             title: 'Success', | 
					
						
							|  |  |  |           }; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |       .catch(error => { | 
					
						
							| 
									
										
										
										
											2018-09-05 22:18:03 +08:00
										 |  |  |         let message = 'Stackdriver: '; | 
					
						
							| 
									
										
										
										
											2018-09-05 20:14:34 +08:00
										 |  |  |         message += error.statusText ? error.statusText + ': ' : ''; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (error.data && error.data.error && error.data.error.code) { | 
					
						
							| 
									
										
										
										
											2018-09-05 22:18:03 +08:00
										 |  |  |           // 400, 401
 | 
					
						
							| 
									
										
										
										
											2018-09-05 20:14:34 +08:00
										 |  |  |           message += error.data.error.code + '. ' + error.data.error.message; | 
					
						
							|  |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2018-09-05 22:18:03 +08:00
										 |  |  |           message += 'Cannot connect to Stackdriver API'; | 
					
						
							| 
									
										
										
										
											2018-09-05 20:14:34 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |         return { | 
					
						
							|  |  |  |           status: 'error', | 
					
						
							|  |  |  |           message: message, | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-06 17:17:58 +08:00
										 |  |  |   async getProjects() { | 
					
						
							|  |  |  |     const response = await this.doRequest(`/cloudresourcemanager/v1/projects`); | 
					
						
							|  |  |  |     return response.data.projects.map(p => ({ id: p.projectId, name: p.name })); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   async doRequest(url, maxRetries = 1) { | 
					
						
							| 
									
										
										
										
											2018-09-05 20:14:34 +08:00
										 |  |  |     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 19:21:02 +08:00
										 |  |  | } |