From e101bcdb13627babe38959b0de4d90d9c2d4dc14 Mon Sep 17 00:00:00 2001 From: Daniel Lee Date: Mon, 24 Sep 2018 10:17:06 +0200 Subject: [PATCH] stackdriver: fix bug when multiple projects connected to service account We had incorrectly assumed that a service account could only be connected to one project. --- .../plugins/datasource/stackdriver/datasource.ts | 11 +++++++++++ .../datasource/stackdriver/filter_segments.ts | 6 +++--- .../plugins/datasource/stackdriver/query_ctrl.ts | 10 +--------- .../datasource/stackdriver/specs/query_ctrl.test.ts | 13 +++++++------ 4 files changed, 22 insertions(+), 18 deletions(-) diff --git a/public/app/plugins/datasource/stackdriver/datasource.ts b/public/app/plugins/datasource/stackdriver/datasource.ts index ca078463e94..7b8d6ab0b5e 100644 --- a/public/app/plugins/datasource/stackdriver/datasource.ts +++ b/public/app/plugins/datasource/stackdriver/datasource.ts @@ -38,6 +38,7 @@ export default class StackdriverDatasource { return this.templateSrv.replace(f, options.scopedVars || {}); }), aliasBy: this.templateSrv.replace(t.aliasBy, options.scopedVars || {}), + type: 'timeSeriesQuery', }; }); @@ -127,6 +128,16 @@ export default class StackdriverDatasource { return response.data.projects.map(p => ({ id: p.projectId, name: p.name })); } + async getDefaultProject() { + const projects = await this.getProjects(); + if (projects && projects.length > 0) { + const test = projects.filter(p => p.id === this.projectName)[0]; + return test; + } else { + throw new Error('No projects found'); + } + } + async getMetricTypes(projectId: string) { try { const metricsApiPath = `v3/projects/${projectId}/metricDescriptors`; diff --git a/public/app/plugins/datasource/stackdriver/filter_segments.ts b/public/app/plugins/datasource/stackdriver/filter_segments.ts index 0aed1262907..8d193f663c2 100644 --- a/public/app/plugins/datasource/stackdriver/filter_segments.ts +++ b/public/app/plugins/datasource/stackdriver/filter_segments.ts @@ -1,9 +1,9 @@ export const DefaultRemoveFilterValue = '-- remove filter --'; +export const DefaultFilterValue = 'select value'; export class FilterSegments { filterSegments: any[]; removeSegment: any; - defaultFilterValue = 'select value'; constructor(private uiSegmentSrv, private target, private getFilterKeysFunc, private getFilterValuesFunc) {} @@ -76,7 +76,7 @@ export class FilterSegments { } segment.type = 'key'; this.filterSegments.push(this.uiSegmentSrv.newOperator('=')); - this.filterSegments.push(this.uiSegmentSrv.newFake(this.defaultFilterValue, 'value', 'query-segment-value')); + this.filterSegments.push(this.uiSegmentSrv.newFake(DefaultFilterValue, 'value', 'query-segment-value')); } removeFilterSegment(index) { @@ -107,7 +107,7 @@ export class FilterSegments { } else if (segment.type === 'key' && segment.value === DefaultRemoveFilterValue) { this.removeFilterSegment(index); this.ensurePlusButton(this.filterSegments); - } else if (segment.type === 'value' && segment.value !== this.defaultFilterValue) { + } else if (segment.type === 'value' && segment.value !== DefaultFilterValue) { this.ensurePlusButton(this.filterSegments); } diff --git a/public/app/plugins/datasource/stackdriver/query_ctrl.ts b/public/app/plugins/datasource/stackdriver/query_ctrl.ts index 6bc378fffa7..b6a3e81b7d0 100644 --- a/public/app/plugins/datasource/stackdriver/query_ctrl.ts +++ b/public/app/plugins/datasource/stackdriver/query_ctrl.ts @@ -30,9 +30,7 @@ export class StackdriverQueryCtrl extends QueryCtrl { aliasBy: string; }; defaultDropdownValue = 'select metric'; - defaultFilterValue = 'select value'; defaultRemoveGroupByValue = '-- remove group by --'; - defaultRemoveFilterValue = '-- remove filter --'; loadLabelsPromise: Promise; stackdriverConstants; @@ -75,7 +73,6 @@ export class StackdriverQueryCtrl extends QueryCtrl { this.getCurrentProject() .then(this.getMetricTypes.bind(this)) .then(this.getLabels.bind(this)); - this.initSegments(); } @@ -97,12 +94,7 @@ export class StackdriverQueryCtrl extends QueryCtrl { async getCurrentProject() { try { - const projects = await this.datasource.getProjects(); - if (projects && projects.length > 0) { - this.target.project = projects[0]; - } else { - throw new Error('No projects found'); - } + this.target.project = await this.datasource.getDefaultProject(); } catch (error) { let message = 'Projects cannot be fetched: '; message += error.statusText ? error.statusText + ': ' : ''; diff --git a/public/app/plugins/datasource/stackdriver/specs/query_ctrl.test.ts b/public/app/plugins/datasource/stackdriver/specs/query_ctrl.test.ts index 1335a9fccb3..c7e167c7c35 100644 --- a/public/app/plugins/datasource/stackdriver/specs/query_ctrl.test.ts +++ b/public/app/plugins/datasource/stackdriver/specs/query_ctrl.test.ts @@ -1,5 +1,6 @@ import { StackdriverQueryCtrl } from '../query_ctrl'; import { TemplateSrvStub } from 'test/specs/helpers'; +import { DefaultRemoveFilterValue, DefaultFilterValue } from '../filter_segments'; describe('StackdriverQueryCtrl', () => { let ctrl; @@ -242,7 +243,7 @@ describe('StackdriverQueryCtrl', () => { beforeEach(() => { const existingKeySegment = { value: 'filterkey1', type: 'key' }; const existingOperatorSegment = { value: '=', type: 'operator' }; - const existingValueSegment = { value: ctrl.defaultFilterValue, type: 'value' }; + const existingValueSegment = { value: DefaultFilterValue, type: 'value' }; ctrl.filterSegments.filterSegments = [existingKeySegment, existingOperatorSegment, existingValueSegment]; ctrl.filterSegmentUpdated(existingValueSegment, 2); }); @@ -256,7 +257,7 @@ describe('StackdriverQueryCtrl', () => { }); describe('and user removes key segment', () => { beforeEach(() => { - const existingKeySegment = { value: ctrl.defaultRemoveFilterValue, type: 'key' }; + const existingKeySegment = { value: DefaultRemoveFilterValue, type: 'key' }; const existingOperatorSegment = { value: '=', type: 'operator' }; const existingValueSegment = { value: 'filtervalue', type: 'value' }; const plusSegment = { value: '', type: 'plus-button' }; @@ -277,8 +278,8 @@ describe('StackdriverQueryCtrl', () => { describe('and user removes key segment and there is a previous filter', () => { beforeEach(() => { - const existingKeySegment1 = { value: ctrl.defaultRemoveFilterValue, type: 'key' }; - const existingKeySegment2 = { value: ctrl.defaultRemoveFilterValue, type: 'key' }; + const existingKeySegment1 = { value: DefaultRemoveFilterValue, type: 'key' }; + const existingKeySegment2 = { value: DefaultRemoveFilterValue, type: 'key' }; const existingOperatorSegment = { value: '=', type: 'operator' }; const existingValueSegment = { value: 'filtervalue', type: 'value' }; const conditionSegment = { value: 'AND', type: 'condition' }; @@ -307,8 +308,8 @@ describe('StackdriverQueryCtrl', () => { describe('and user removes key segment and there is a filter after it', () => { beforeEach(() => { - const existingKeySegment1 = { value: ctrl.defaultRemoveFilterValue, type: 'key' }; - const existingKeySegment2 = { value: ctrl.defaultRemoveFilterValue, type: 'key' }; + const existingKeySegment1 = { value: DefaultRemoveFilterValue, type: 'key' }; + const existingKeySegment2 = { value: DefaultRemoveFilterValue, type: 'key' }; const existingOperatorSegment = { value: '=', type: 'operator' }; const existingValueSegment = { value: 'filtervalue', type: 'value' }; const conditionSegment = { value: 'AND', type: 'condition' };