From 23599814a3d194e8fdd815e8fa24ca3a21b94d5b Mon Sep 17 00:00:00 2001 From: Jari Sukanen Date: Tue, 27 Oct 2015 16:28:43 +0200 Subject: [PATCH] cloudwatch: add support for defining AWS profile for CloudWatch datasource Add support for defining AWS profile for CloudWatch datasource to support pulling information from multiple different AWS accounts to single dashboard. With this change, it is possible to define multiple AWS credentials in ~/.aws/credentials file and connect different data sources to different AWS accounts. --- pkg/api/cloudwatch/cloudwatch.go | 33 +++++++++++++++---- .../datasource/cloudwatch/datasource.js | 3 ++ .../cloudwatch/partials/config.html | 11 +++++++ 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/pkg/api/cloudwatch/cloudwatch.go b/pkg/api/cloudwatch/cloudwatch.go index 6f2afdaa9e2..d436d0a6452 100644 --- a/pkg/api/cloudwatch/cloudwatch.go +++ b/pkg/api/cloudwatch/cloudwatch.go @@ -7,6 +7,8 @@ import ( "time" "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/credentials" + "github.com/aws/aws-sdk-go/aws/credentials/ec2rolecreds" "github.com/aws/aws-sdk-go/service/cloudwatch" "github.com/aws/aws-sdk-go/service/ec2" "github.com/grafana/grafana/pkg/middleware" @@ -17,9 +19,10 @@ type actionHandler func(*cwRequest, *middleware.Context) var actionHandlers map[string]actionHandler type cwRequest struct { - Region string `json:"region"` - Action string `json:"action"` - Body []byte `json:"-"` + Region string `json:"region"` + Profile string `json:"profile"` + Action string `json:"action"` + Body []byte `json:"-"` } func init() { @@ -35,7 +38,16 @@ func init() { } func handleGetMetricStatistics(req *cwRequest, c *middleware.Context) { - svc := cloudwatch.New(&aws.Config{Region: aws.String(req.Region)}) + creds := credentials.NewChainCredentials( + []credentials.Provider{ + &credentials.EnvProvider{}, + &credentials.SharedCredentialsProvider{Filename: "", Profile: req.Profile}, + &ec2rolecreds.EC2RoleProvider{ExpiryWindow: 5 * time.Minute}, + }) + svc := cloudwatch.New(&aws.Config{ + Region: aws.String(req.Region), + Credentials: creds, + }) reqParam := &struct { Parameters struct { @@ -70,7 +82,17 @@ func handleGetMetricStatistics(req *cwRequest, c *middleware.Context) { } func handleListMetrics(req *cwRequest, c *middleware.Context) { - svc := cloudwatch.New(&aws.Config{Region: aws.String(req.Region)}) + creds := credentials.NewChainCredentials( + []credentials.Provider{ + &credentials.EnvProvider{}, + &credentials.SharedCredentialsProvider{Filename: "", Profile: req.Profile}, + &ec2rolecreds.EC2RoleProvider{ExpiryWindow: 5 * time.Minute}, + }) + svc := cloudwatch.New(&aws.Config{ + Region: aws.String(req.Region), + Credentials: creds, + }) + reqParam := &struct { Parameters struct { Namespace string `json:"namespace"` @@ -78,7 +100,6 @@ func handleListMetrics(req *cwRequest, c *middleware.Context) { Dimensions []*cloudwatch.DimensionFilter `json:"dimensions"` } `json:"parameters"` }{} - json.Unmarshal(req.Body, reqParam) params := &cloudwatch.ListMetricsInput{ diff --git a/public/app/plugins/datasource/cloudwatch/datasource.js b/public/app/plugins/datasource/cloudwatch/datasource.js index 9a9876b53ae..2b5f8c76440 100644 --- a/public/app/plugins/datasource/cloudwatch/datasource.js +++ b/public/app/plugins/datasource/cloudwatch/datasource.js @@ -18,6 +18,7 @@ function (angular, _) { this.supportMetrics = true; this.proxyUrl = datasource.url; this.defaultRegion = datasource.jsonData.defaultRegion; + this.profile = datasource.jsonData.profile; } CloudWatchDatasource.prototype.query = function(options) { @@ -73,6 +74,7 @@ function (angular, _) { CloudWatchDatasource.prototype.performTimeSeriesQuery = function(query, start, end) { return this.awsRequest({ region: query.region, + profile: this.profile, action: 'GetMetricStatistics', parameters: { namespace: query.namespace, @@ -115,6 +117,7 @@ function (angular, _) { CloudWatchDatasource.prototype.getDimensionValues = function(region, namespace, metricName, dimensions) { var request = { region: templateSrv.replace(region), + profile: this.profile, action: 'ListMetrics', parameters: { namespace: templateSrv.replace(namespace), diff --git a/public/app/plugins/datasource/cloudwatch/partials/config.html b/public/app/plugins/datasource/cloudwatch/partials/config.html index 627b2829418..4be4debe072 100644 --- a/public/app/plugins/datasource/cloudwatch/partials/config.html +++ b/public/app/plugins/datasource/cloudwatch/partials/config.html @@ -12,6 +12,17 @@
+
+ +
+