浏览代码

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.
Daniel Lee 7 年之前
父节点
当前提交
e101bcdb13

+ 11 - 0
public/app/plugins/datasource/stackdriver/datasource.ts

@@ -38,6 +38,7 @@ export default class StackdriverDatasource {
             return this.templateSrv.replace(f, options.scopedVars || {});
             return this.templateSrv.replace(f, options.scopedVars || {});
           }),
           }),
           aliasBy: this.templateSrv.replace(t.aliasBy, 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 }));
     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) {
   async getMetricTypes(projectId: string) {
     try {
     try {
       const metricsApiPath = `v3/projects/${projectId}/metricDescriptors`;
       const metricsApiPath = `v3/projects/${projectId}/metricDescriptors`;

+ 3 - 3
public/app/plugins/datasource/stackdriver/filter_segments.ts

@@ -1,9 +1,9 @@
 export const DefaultRemoveFilterValue = '-- remove filter --';
 export const DefaultRemoveFilterValue = '-- remove filter --';
+export const DefaultFilterValue = 'select value';
 
 
 export class FilterSegments {
 export class FilterSegments {
   filterSegments: any[];
   filterSegments: any[];
   removeSegment: any;
   removeSegment: any;
-  defaultFilterValue = 'select value';
 
 
   constructor(private uiSegmentSrv, private target, private getFilterKeysFunc, private getFilterValuesFunc) {}
   constructor(private uiSegmentSrv, private target, private getFilterKeysFunc, private getFilterValuesFunc) {}
 
 
@@ -76,7 +76,7 @@ export class FilterSegments {
     }
     }
     segment.type = 'key';
     segment.type = 'key';
     this.filterSegments.push(this.uiSegmentSrv.newOperator('='));
     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) {
   removeFilterSegment(index) {
@@ -107,7 +107,7 @@ export class FilterSegments {
     } else if (segment.type === 'key' && segment.value === DefaultRemoveFilterValue) {
     } else if (segment.type === 'key' && segment.value === DefaultRemoveFilterValue) {
       this.removeFilterSegment(index);
       this.removeFilterSegment(index);
       this.ensurePlusButton(this.filterSegments);
       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);
       this.ensurePlusButton(this.filterSegments);
     }
     }
 
 

+ 1 - 9
public/app/plugins/datasource/stackdriver/query_ctrl.ts

@@ -30,9 +30,7 @@ export class StackdriverQueryCtrl extends QueryCtrl {
     aliasBy: string;
     aliasBy: string;
   };
   };
   defaultDropdownValue = 'select metric';
   defaultDropdownValue = 'select metric';
-  defaultFilterValue = 'select value';
   defaultRemoveGroupByValue = '-- remove group by --';
   defaultRemoveGroupByValue = '-- remove group by --';
-  defaultRemoveFilterValue = '-- remove filter --';
   loadLabelsPromise: Promise<any>;
   loadLabelsPromise: Promise<any>;
   stackdriverConstants;
   stackdriverConstants;
 
 
@@ -75,7 +73,6 @@ export class StackdriverQueryCtrl extends QueryCtrl {
     this.getCurrentProject()
     this.getCurrentProject()
       .then(this.getMetricTypes.bind(this))
       .then(this.getMetricTypes.bind(this))
       .then(this.getLabels.bind(this));
       .then(this.getLabels.bind(this));
-
     this.initSegments();
     this.initSegments();
   }
   }
 
 
@@ -97,12 +94,7 @@ export class StackdriverQueryCtrl extends QueryCtrl {
 
 
   async getCurrentProject() {
   async getCurrentProject() {
     try {
     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) {
     } catch (error) {
       let message = 'Projects cannot be fetched: ';
       let message = 'Projects cannot be fetched: ';
       message += error.statusText ? error.statusText + ': ' : '';
       message += error.statusText ? error.statusText + ': ' : '';

+ 7 - 6
public/app/plugins/datasource/stackdriver/specs/query_ctrl.test.ts

@@ -1,5 +1,6 @@
 import { StackdriverQueryCtrl } from '../query_ctrl';
 import { StackdriverQueryCtrl } from '../query_ctrl';
 import { TemplateSrvStub } from 'test/specs/helpers';
 import { TemplateSrvStub } from 'test/specs/helpers';
+import { DefaultRemoveFilterValue, DefaultFilterValue } from '../filter_segments';
 
 
 describe('StackdriverQueryCtrl', () => {
 describe('StackdriverQueryCtrl', () => {
   let ctrl;
   let ctrl;
@@ -242,7 +243,7 @@ describe('StackdriverQueryCtrl', () => {
         beforeEach(() => {
         beforeEach(() => {
           const existingKeySegment = { value: 'filterkey1', type: 'key' };
           const existingKeySegment = { value: 'filterkey1', type: 'key' };
           const existingOperatorSegment = { value: '=', type: 'operator' };
           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.filterSegments.filterSegments = [existingKeySegment, existingOperatorSegment, existingValueSegment];
           ctrl.filterSegmentUpdated(existingValueSegment, 2);
           ctrl.filterSegmentUpdated(existingValueSegment, 2);
         });
         });
@@ -256,7 +257,7 @@ describe('StackdriverQueryCtrl', () => {
       });
       });
       describe('and user removes key segment', () => {
       describe('and user removes key segment', () => {
         beforeEach(() => {
         beforeEach(() => {
-          const existingKeySegment = { value: ctrl.defaultRemoveFilterValue, type: 'key' };
+          const existingKeySegment = { value: DefaultRemoveFilterValue, type: 'key' };
           const existingOperatorSegment = { value: '=', type: 'operator' };
           const existingOperatorSegment = { value: '=', type: 'operator' };
           const existingValueSegment = { value: 'filtervalue', type: 'value' };
           const existingValueSegment = { value: 'filtervalue', type: 'value' };
           const plusSegment = { value: '', type: 'plus-button' };
           const plusSegment = { value: '', type: 'plus-button' };
@@ -277,8 +278,8 @@ describe('StackdriverQueryCtrl', () => {
 
 
       describe('and user removes key segment and there is a previous filter', () => {
       describe('and user removes key segment and there is a previous filter', () => {
         beforeEach(() => {
         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 existingOperatorSegment = { value: '=', type: 'operator' };
           const existingValueSegment = { value: 'filtervalue', type: 'value' };
           const existingValueSegment = { value: 'filtervalue', type: 'value' };
           const conditionSegment = { value: 'AND', type: 'condition' };
           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', () => {
       describe('and user removes key segment and there is a filter after it', () => {
         beforeEach(() => {
         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 existingOperatorSegment = { value: '=', type: 'operator' };
           const existingValueSegment = { value: 'filtervalue', type: 'value' };
           const existingValueSegment = { value: 'filtervalue', type: 'value' };
           const conditionSegment = { value: 'AND', type: 'condition' };
           const conditionSegment = { value: 'AND', type: 'condition' };