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.
This commit is contained in:
Daniel Lee 2018-09-24 10:17:06 +02:00
parent 636d8421d0
commit e101bcdb13
4 changed files with 22 additions and 18 deletions

View File

@ -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`;

View File

@ -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);
}

View File

@ -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<any>;
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 + ': ' : '';

View File

@ -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' };