Browse Source

Adds skeleton for loading projects from google resource manager

Erik Sundell 7 years ago
parent
commit
aff1de9892

+ 18 - 0
public/app/plugins/datasource/stackdriver/config_ctrl.ts

@@ -5,12 +5,16 @@ export class StackdriverConfigCtrl {
   jsonText: string;
   validationErrors: string[] = [];
   inputDataValid: boolean;
+  defaultProject: string;
+  projects: string[];
+  loadingProjects: boolean;
 
   /** @ngInject */
   constructor($scope, datasourceSrv) {
     this.datasourceSrv = datasourceSrv;
     this.current.jsonData = this.current.jsonData || {};
     this.current.secureJsonData = this.current.secureJsonData || {};
+    this.projects = [];
   }
 
   save(jwt) {
@@ -45,6 +49,7 @@ export class StackdriverConfigCtrl {
     this.jsonText = '';
     if (this.validateJwt(json)) {
       this.save(json);
+      this.displayProjects();
     }
   }
 
@@ -53,6 +58,7 @@ export class StackdriverConfigCtrl {
       const json = JSON.parse(e.originalEvent.clipboardData.getData('text/plain') || this.jsonText);
       if (this.validateJwt(json)) {
         this.save(json);
+        this.displayProjects();
       }
     } catch (error) {
       this.resetValidationMessages();
@@ -65,4 +71,16 @@ export class StackdriverConfigCtrl {
     this.inputDataValid = false;
     this.jsonText = '';
   }
+
+  async displayProjects() {
+    if (this.projects.length === 0) {
+      this.loadingProjects = true;
+      const ds = await this.datasourceSrv.loadDatasource(this.current.name);
+      try {
+        this.projects = await ds.doRequest(`/cloudresourcemanager/v1/projects`);
+      } catch (error) {
+        console.log(error);
+      }
+    }
+  }
 }

+ 2 - 1
public/app/plugins/datasource/stackdriver/datasource.ts

@@ -6,6 +6,7 @@ export default class StackdriverDatasource {
   constructor(instanceSettings, private backendSrv) {
     this.baseUrl = `/stackdriver/`;
     this.url = instanceSettings.url;
+    this.doRequest = this.doRequest;
   }
 
   testDatasource() {
@@ -15,7 +16,7 @@ export default class StackdriverDatasource {
         if (response.status === 200) {
           return {
             status: 'success',
-            message: 'Successfully queried the Azure Monitor service.',
+            message: 'Successfully queried the Stackdriver API.',
             title: 'Success',
           };
         }

+ 10 - 0
public/app/plugins/datasource/stackdriver/partials/config.html

@@ -11,6 +11,16 @@
 	<div class="gf-form" ng-if="!ctrl.inputDataValid">
 		<textarea rows="10" data-share-panel-url="" class="gf-form-input" ng-model="ctrl.jsonText" ng-paste="ctrl.onPasteJwt($event)"></textarea>
 	</div>
+	<div class="gf-form" ng-if="ctrl.inputDataValid">
+		<span class="gf-form-label width-7">Default Projects</span>
+		<div class="gf-form-select-wrapper max-width-23">
+			<select class="gf-form-input" ng-model="ctrl.defaultProject" ng-options="p.id as p.name for p in ctrl.projects" ng-change="ctrl.userChangedDefaultProject()"></select>
+		</div>
+		<div ng-if="ctrl.loadingProjects">
+			<i class="fa fa-spinner fa-spin"></i>
+			<em>Fetching history list&hellip;</em>
+		</div>
+	</div>
 	<div ng-repeat="valError in ctrl.validationErrors" class="text-error p-l-1">
 		<i class="fa fa-warning"></i>
 		{{valError}}

+ 12 - 0
public/app/plugins/datasource/stackdriver/plugin.json

@@ -25,6 +25,18 @@
           "private_key": "{{.SecureJsonData.privateKey}}"
         }
       }
+    },
+    {
+      "path": "cloudresourcemanager",
+      "method": "GET",
+      "url": "https://cloudresourcemanager.googleapis.com",
+      "jwtTokenAuth": {
+        "params": {
+          "token_uri": "{{.JsonData.tokenUri}}",
+          "client_email": "{{.JsonData.clientEmail}}",
+          "private_key": "{{.SecureJsonData.privateKey}}"
+        }
+      }
     }
   ]
 }