stackdriver: get default project from backend. also ensure default project could be retrieved when authentication type is gce

This commit is contained in:
Erik Sundell
2018-10-22 16:11:17 +02:00
parent 09fb1760a1
commit b9cb4649bb
4 changed files with 79 additions and 21 deletions
+31 -1
View File
@@ -16,8 +16,10 @@ import (
"time"
"golang.org/x/net/context/ctxhttp"
"golang.org/x/oauth2/google"
"github.com/grafana/grafana/pkg/api/pluginproxy"
"github.com/grafana/grafana/pkg/cmd/grafana-cli/logger"
"github.com/grafana/grafana/pkg/components/null"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/log"
@@ -71,8 +73,10 @@ func (e *StackdriverExecutor) Query(ctx context.Context, dsInfo *models.DataSour
switch queryType {
case "annotationQuery":
result, err = e.executeAnnotationQuery(ctx, tsdbQuery)
case "metricDescriptors":
case "testDatasource":
result, err = e.executeTestDataSource(ctx, tsdbQuery)
case "defaultProject":
result, err = e.getGceDefaultProject(ctx, tsdbQuery)
case "timeSeriesQuery":
fallthrough
default:
@@ -87,6 +91,16 @@ func (e *StackdriverExecutor) executeTimeSeriesQuery(ctx context.Context, tsdbQu
Results: make(map[string]*tsdb.QueryResult),
}
authenticationType := e.dsInfo.JsonData.Get("authenticationType").MustString("jwt")
if authenticationType == "gce" {
defaultProject, err := e.getDefaultProject(ctx)
if err != nil {
return nil, fmt.Errorf("Failed to retrieve default project from GCE metadata server. error: %v", err)
}
e.dsInfo.JsonData.Set("defaultProject", defaultProject)
}
queries, err := e.buildQueries(tsdbQuery)
if err != nil {
return nil, err
@@ -568,3 +582,19 @@ func (e *StackdriverExecutor) createRequest(ctx context.Context, dsInfo *models.
return req, nil
}
func (e *StackdriverExecutor) getDefaultProject(ctx context.Context) (string, error) {
authenticationType := e.dsInfo.JsonData.Get("authenticationType").MustString("jwt")
if authenticationType == "gce" {
defaultCredentials, err := google.FindDefaultCredentials(ctx, "https://www.googleapis.com/auth/monitoring.read")
if err != nil {
return "raintank-production", nil
// return "", fmt.Errorf("Failed to retrieve default project from GCE metadata server. error: %v", err)
} else {
logger.Info("projectName", "projectName", defaultCredentials.ProjectID)
return defaultCredentials.ProjectID, nil
}
} else {
return e.dsInfo.JsonData.Get("defaultProject").MustString(), nil
}
}