From 5bee38ed69cc57751cf35ebd791f3673e4963e9b Mon Sep 17 00:00:00 2001 From: Erik Sundell Date: Thu, 6 Sep 2018 11:17:58 +0200 Subject: [PATCH] Stackdriver: Refactored api call to google resource manager --- .../datasource/stackdriver/config_ctrl.ts | 17 +++++++++-------- .../datasource/stackdriver/datasource.ts | 7 ++++++- .../datasource/stackdriver/partials/config.html | 3 ++- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/public/app/plugins/datasource/stackdriver/config_ctrl.ts b/public/app/plugins/datasource/stackdriver/config_ctrl.ts index 6a22c6b96ea..7c5b1fd29e7 100644 --- a/public/app/plugins/datasource/stackdriver/config_ctrl.ts +++ b/public/app/plugins/datasource/stackdriver/config_ctrl.ts @@ -7,7 +7,7 @@ export class StackdriverConfigCtrl { inputDataValid: boolean; defaultProject: string; projectsError: string; - projects: string[]; + projects: any[]; loadingProjects: boolean; /** @ngInject */ @@ -15,6 +15,7 @@ export class StackdriverConfigCtrl { this.datasourceSrv = datasourceSrv; this.current.jsonData = this.current.jsonData || {}; this.current.secureJsonData = this.current.secureJsonData || {}; + this.defaultProject = this.current.jsonData.defaultProject; this.projects = []; } @@ -80,11 +81,12 @@ export class StackdriverConfigCtrl { try { this.loadingProjects = true; const ds = await this.datasourceSrv.loadDatasource(this.current.name); - const response = await ds.doRequest(`/cloudresourcemanager/v1/projects`); + this.projects = await ds.getProjects(); this.$scope.$apply(() => { - this.projects = response.data.projects.map(p => ({ id: p.projectId, name: p.name })); + if (this.projects.length > 0) { + this.current.jsonData.defaultProject = this.current.jsonData.defaultProject || this.projects[0].id; + } }); - console.log(this.projects); } catch (error) { let message = 'Projects cannot be fetched: '; message += error.statusText ? error.statusText + ': ' : ''; @@ -100,10 +102,9 @@ export class StackdriverConfigCtrl { } else { message += 'Cannot connect to Stackdriver API'; } - this.$scope.$apply(() => { - this.loadingProjects = false; - this.projectsError = message; - }); + this.$scope.$apply(() => (this.projectsError = message)); + } finally { + this.$scope.$apply(() => (this.loadingProjects = false)); } } } diff --git a/public/app/plugins/datasource/stackdriver/datasource.ts b/public/app/plugins/datasource/stackdriver/datasource.ts index 5b5838990cc..5de2e49bf2a 100644 --- a/public/app/plugins/datasource/stackdriver/datasource.ts +++ b/public/app/plugins/datasource/stackdriver/datasource.ts @@ -38,7 +38,12 @@ export default class StackdriverDatasource { }); } - doRequest(url, maxRetries = 1) { + 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) { return this.backendSrv .datasourceRequest({ url: this.url + url, diff --git a/public/app/plugins/datasource/stackdriver/partials/config.html b/public/app/plugins/datasource/stackdriver/partials/config.html index dad86dd06c8..b3a8d55b5ef 100644 --- a/public/app/plugins/datasource/stackdriver/partials/config.html +++ b/public/app/plugins/datasource/stackdriver/partials/config.html @@ -28,7 +28,8 @@
Default Project
- +