Commit b9cb4649 by Erik Sundell

stackdriver: get default project from backend. also ensure default project could…

stackdriver: get default project from backend. also ensure default project could be retrieved when authentication type is gce
parent 09fb1760
package stackdriver
import (
"context"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/tsdb"
)
func (e *StackdriverExecutor) getGceDefaultProject(ctx context.Context, tsdbQuery *tsdb.TsdbQuery) (*tsdb.Response, error) {
queryResult := &tsdb.QueryResult{Meta: simplejson.New(), RefId: tsdbQuery.Queries[0].RefId}
result := &tsdb.Response{
Results: make(map[string]*tsdb.QueryResult),
}
defaultProject, err := e.getDefaultProject(ctx)
if err != nil {
return nil, err
}
queryResult.Meta.Set("defaultProject", defaultProject)
result.Results[tsdbQuery.Queries[0].RefId] = queryResult
return result, nil
}
...@@ -16,8 +16,10 @@ import ( ...@@ -16,8 +16,10 @@ import (
"time" "time"
"golang.org/x/net/context/ctxhttp" "golang.org/x/net/context/ctxhttp"
"golang.org/x/oauth2/google"
"github.com/grafana/grafana/pkg/api/pluginproxy" "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/null"
"github.com/grafana/grafana/pkg/components/simplejson" "github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/log" "github.com/grafana/grafana/pkg/log"
...@@ -71,8 +73,10 @@ func (e *StackdriverExecutor) Query(ctx context.Context, dsInfo *models.DataSour ...@@ -71,8 +73,10 @@ func (e *StackdriverExecutor) Query(ctx context.Context, dsInfo *models.DataSour
switch queryType { switch queryType {
case "annotationQuery": case "annotationQuery":
result, err = e.executeAnnotationQuery(ctx, tsdbQuery) result, err = e.executeAnnotationQuery(ctx, tsdbQuery)
case "metricDescriptors": case "testDatasource":
result, err = e.executeTestDataSource(ctx, tsdbQuery) result, err = e.executeTestDataSource(ctx, tsdbQuery)
case "defaultProject":
result, err = e.getGceDefaultProject(ctx, tsdbQuery)
case "timeSeriesQuery": case "timeSeriesQuery":
fallthrough fallthrough
default: default:
...@@ -87,6 +91,16 @@ func (e *StackdriverExecutor) executeTimeSeriesQuery(ctx context.Context, tsdbQu ...@@ -87,6 +91,16 @@ func (e *StackdriverExecutor) executeTimeSeriesQuery(ctx context.Context, tsdbQu
Results: make(map[string]*tsdb.QueryResult), 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) queries, err := e.buildQueries(tsdbQuery)
if err != nil { if err != nil {
return nil, err return nil, err
...@@ -568,3 +582,19 @@ func (e *StackdriverExecutor) createRequest(ctx context.Context, dsInfo *models. ...@@ -568,3 +582,19 @@ func (e *StackdriverExecutor) createRequest(ctx context.Context, dsInfo *models.
return req, nil 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
}
}
...@@ -8,27 +8,26 @@ import ( ...@@ -8,27 +8,26 @@ import (
"net/http" "net/http"
"strings" "strings"
"github.com/grafana/grafana/pkg/cmd/grafana-cli/logger"
"github.com/grafana/grafana/pkg/components/simplejson" "github.com/grafana/grafana/pkg/components/simplejson"
"golang.org/x/net/context/ctxhttp" "golang.org/x/net/context/ctxhttp"
"golang.org/x/oauth2/google"
"github.com/grafana/grafana/pkg/tsdb" "github.com/grafana/grafana/pkg/tsdb"
) )
func (e *StackdriverExecutor) executeTestDataSource(ctx context.Context, tsdbQuery *tsdb.TsdbQuery) (*tsdb.Response, error) { func (e *StackdriverExecutor) executeTestDataSource(ctx context.Context, tsdbQuery *tsdb.TsdbQuery) (*tsdb.Response, error) {
queryResult := &tsdb.QueryResult{Meta: simplejson.New(), RefId: tsdbQuery.Queries[0].RefId}
result := &tsdb.Response{
Results: make(map[string]*tsdb.QueryResult),
}
authenticationType := e.dsInfo.JsonData.Get("authenticationType").MustString("jwt") authenticationType := e.dsInfo.JsonData.Get("authenticationType").MustString("jwt")
if authenticationType == "gce" { if authenticationType == "gce" {
defaultProject, err := e.getDefaultProject(ctx) defaultProject, err := e.getDefaultProject(ctx)
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("Failed to retrieve default project from GCE metadata server. error: %v", err)
}
e.dsInfo.JsonData.Set("defaultProject", defaultProject)
} }
queryResult := &tsdb.QueryResult{Meta: simplejson.New(), RefId: tsdbQuery.Queries[0].RefId} e.dsInfo.JsonData.Set("defaultProject", defaultProject)
result := &tsdb.Response{
Results: make(map[string]*tsdb.QueryResult),
} }
req, err := e.createRequest(ctx, e.dsInfo, "metricDescriptors") req, err := e.createRequest(ctx, e.dsInfo, "metricDescriptors")
...@@ -57,16 +56,6 @@ func (e *StackdriverExecutor) executeTestDataSource(ctx context.Context, tsdbQue ...@@ -57,16 +56,6 @@ func (e *StackdriverExecutor) executeTestDataSource(ctx context.Context, tsdbQue
return result, nil return result, nil
} }
func (e *StackdriverExecutor) getDefaultProject(ctx context.Context) (string, error) {
defaultCredentials, err := google.FindDefaultCredentials(ctx, "https://www.googleapis.com/auth/monitoring.read")
if err != nil {
return "", err
} else {
logger.Info("projectName", "projectName", defaultCredentials.ProjectID)
return defaultCredentials.ProjectID, nil
}
}
func transformMetricDescriptorResponseToTable(data MetricDescriptorsResponse) *tsdb.Table { func transformMetricDescriptorResponseToTable(data MetricDescriptorsResponse) *tsdb.Table {
table := &tsdb.Table{ table := &tsdb.Table{
Columns: make([]tsdb.TableColumn, 1), Columns: make([]tsdb.TableColumn, 1),
......
...@@ -221,8 +221,25 @@ export default class StackdriverDatasource { ...@@ -221,8 +221,25 @@ export default class StackdriverDatasource {
async getDefaultProject() { async getDefaultProject() {
try { try {
await this.queryPromise; if (!this.projectName) {
const { data } = await this.backendSrv.datasourceRequest({
url: '/api/tsdb/query',
method: 'POST',
data: {
queries: [
{
refId: 'defaultProject',
type: 'defaultProject',
datasourceId: this.id,
},
],
},
});
this.projectName = data.results.defaultProject.meta.defaultProject;
return this.projectName; return this.projectName;
} else {
return this.projectName;
}
} 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 + ': ' : '';
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment