Commit 4a51d054 by Erik Sundell

stackdriver: add variable for gce authentication type

parent b9cb4649
...@@ -34,6 +34,7 @@ var ( ...@@ -34,6 +34,7 @@ var (
slog log.Logger slog log.Logger
legendKeyFormat *regexp.Regexp legendKeyFormat *regexp.Regexp
metricNameFormat *regexp.Regexp metricNameFormat *regexp.Regexp
gceAuthentication string
) )
// StackdriverExecutor executes queries for the Stackdriver datasource // StackdriverExecutor executes queries for the Stackdriver datasource
...@@ -60,6 +61,7 @@ func init() { ...@@ -60,6 +61,7 @@ func init() {
tsdb.RegisterTsdbQueryEndpoint("stackdriver", NewStackdriverExecutor) tsdb.RegisterTsdbQueryEndpoint("stackdriver", NewStackdriverExecutor)
legendKeyFormat = regexp.MustCompile(`\{\{\s*(.+?)\s*\}\}`) legendKeyFormat = regexp.MustCompile(`\{\{\s*(.+?)\s*\}\}`)
metricNameFormat = regexp.MustCompile(`([\w\d_]+)\.googleapis\.com/(.+)`) metricNameFormat = regexp.MustCompile(`([\w\d_]+)\.googleapis\.com/(.+)`)
gceAuthentication = "gce"
} }
// Query takes in the frontend queries, parses them into the Stackdriver query format // Query takes in the frontend queries, parses them into the Stackdriver query format
...@@ -92,7 +94,7 @@ func (e *StackdriverExecutor) executeTimeSeriesQuery(ctx context.Context, tsdbQu ...@@ -92,7 +94,7 @@ func (e *StackdriverExecutor) executeTimeSeriesQuery(ctx context.Context, tsdbQu
} }
authenticationType := e.dsInfo.JsonData.Get("authenticationType").MustString("jwt") authenticationType := e.dsInfo.JsonData.Get("authenticationType").MustString("jwt")
if authenticationType == "gce" { if authenticationType == gceAuthentication {
defaultProject, err := e.getDefaultProject(ctx) defaultProject, err := e.getDefaultProject(ctx)
if err != nil { if err != nil {
return nil, fmt.Errorf("Failed to retrieve default project from GCE metadata server. error: %v", err) return nil, fmt.Errorf("Failed to retrieve default project from GCE metadata server. error: %v", err)
...@@ -585,11 +587,10 @@ func (e *StackdriverExecutor) createRequest(ctx context.Context, dsInfo *models. ...@@ -585,11 +587,10 @@ func (e *StackdriverExecutor) createRequest(ctx context.Context, dsInfo *models.
func (e *StackdriverExecutor) getDefaultProject(ctx context.Context) (string, error) { func (e *StackdriverExecutor) getDefaultProject(ctx context.Context) (string, error) {
authenticationType := e.dsInfo.JsonData.Get("authenticationType").MustString("jwt") authenticationType := e.dsInfo.JsonData.Get("authenticationType").MustString("jwt")
if authenticationType == "gce" { if authenticationType == gceAuthentication {
defaultCredentials, err := google.FindDefaultCredentials(ctx, "https://www.googleapis.com/auth/monitoring.read") defaultCredentials, err := google.FindDefaultCredentials(ctx, "https://www.googleapis.com/auth/monitoring.read")
if err != nil { if err != nil {
return "raintank-production", nil return "", fmt.Errorf("Failed to retrieve default project from GCE metadata server. error: %v", err)
// return "", fmt.Errorf("Failed to retrieve default project from GCE metadata server. error: %v", err)
} else { } else {
logger.Info("projectName", "projectName", defaultCredentials.ProjectID) logger.Info("projectName", "projectName", defaultCredentials.ProjectID)
return defaultCredentials.ProjectID, nil return defaultCredentials.ProjectID, nil
......
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