From 5d64568f3e2488abd66d1295fa80232a6dfd231e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Thu, 29 Oct 2015 11:44:34 +0100 Subject: [PATCH] refactoring: some minor refactoring and changes to AWS profile PR #3053 --- pkg/api/cloudwatch/cloudwatch.go | 16 ++-- pkg/api/dataproxy.go | 2 +- .../datasource/cloudwatch/datasource.js | 3 - .../cloudwatch/partials/config.html | 94 +++++-------------- 4 files changed, 35 insertions(+), 80 deletions(-) diff --git a/pkg/api/cloudwatch/cloudwatch.go b/pkg/api/cloudwatch/cloudwatch.go index d436d0a6452..763eb3d489a 100644 --- a/pkg/api/cloudwatch/cloudwatch.go +++ b/pkg/api/cloudwatch/cloudwatch.go @@ -12,6 +12,7 @@ import ( "github.com/aws/aws-sdk-go/service/cloudwatch" "github.com/aws/aws-sdk-go/service/ec2" "github.com/grafana/grafana/pkg/middleware" + m "github.com/grafana/grafana/pkg/models" ) type actionHandler func(*cwRequest, *middleware.Context) @@ -19,10 +20,10 @@ type actionHandler func(*cwRequest, *middleware.Context) var actionHandlers map[string]actionHandler type cwRequest struct { - Region string `json:"region"` - Profile string `json:"profile"` - Action string `json:"action"` - Body []byte `json:"-"` + Region string `json:"region"` + Action string `json:"action"` + Body []byte `json:"-"` + DataSource *m.DataSource } func init() { @@ -41,7 +42,7 @@ func handleGetMetricStatistics(req *cwRequest, c *middleware.Context) { creds := credentials.NewChainCredentials( []credentials.Provider{ &credentials.EnvProvider{}, - &credentials.SharedCredentialsProvider{Filename: "", Profile: req.Profile}, + &credentials.SharedCredentialsProvider{Filename: "", Profile: req.DataSource.Database}, &ec2rolecreds.EC2RoleProvider{ExpiryWindow: 5 * time.Minute}, }) svc := cloudwatch.New(&aws.Config{ @@ -85,7 +86,7 @@ func handleListMetrics(req *cwRequest, c *middleware.Context) { creds := credentials.NewChainCredentials( []credentials.Provider{ &credentials.EnvProvider{}, - &credentials.SharedCredentialsProvider{Filename: "", Profile: req.Profile}, + &credentials.SharedCredentialsProvider{Filename: "", Profile: req.DataSource.Database}, &ec2rolecreds.EC2RoleProvider{ExpiryWindow: 5 * time.Minute}, }) svc := cloudwatch.New(&aws.Config{ @@ -145,9 +146,10 @@ func handleDescribeInstances(req *cwRequest, c *middleware.Context) { c.JSON(200, resp) } -func HandleRequest(c *middleware.Context) { +func HandleRequest(c *middleware.Context, ds *m.DataSource) { var req cwRequest req.Body, _ = ioutil.ReadAll(c.Req.Request.Body) + req.DataSource = ds json.Unmarshal(req.Body, &req) if handler, found := actionHandlers[req.Action]; !found { diff --git a/pkg/api/dataproxy.go b/pkg/api/dataproxy.go index 3aa215eac15..7193198155f 100644 --- a/pkg/api/dataproxy.go +++ b/pkg/api/dataproxy.go @@ -97,7 +97,7 @@ func ProxyDataSourceRequest(c *middleware.Context) { } if ds.Type == m.DS_CLOUDWATCH { - cloudwatch.HandleRequest(c) + cloudwatch.HandleRequest(c, ds) } else { proxyPath := c.Params("*") proxy := NewReverseProxy(ds, proxyPath, targetUrl) diff --git a/public/app/plugins/datasource/cloudwatch/datasource.js b/public/app/plugins/datasource/cloudwatch/datasource.js index 2b5f8c76440..9a9876b53ae 100644 --- a/public/app/plugins/datasource/cloudwatch/datasource.js +++ b/public/app/plugins/datasource/cloudwatch/datasource.js @@ -18,7 +18,6 @@ function (angular, _) { this.supportMetrics = true; this.proxyUrl = datasource.url; this.defaultRegion = datasource.jsonData.defaultRegion; - this.profile = datasource.jsonData.profile; } CloudWatchDatasource.prototype.query = function(options) { @@ -74,7 +73,6 @@ function (angular, _) { CloudWatchDatasource.prototype.performTimeSeriesQuery = function(query, start, end) { return this.awsRequest({ region: query.region, - profile: this.profile, action: 'GetMetricStatistics', parameters: { namespace: query.namespace, @@ -117,7 +115,6 @@ 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 8bd17a954c1..6f79f47f945 100644 --- a/public/app/plugins/datasource/cloudwatch/partials/config.html +++ b/public/app/plugins/datasource/cloudwatch/partials/config.html @@ -1,73 +1,29 @@
CloudWatch details
-
- -
+
+
+
+
    +
  • + Credentials profile nameCredentials profile name, as specified in ~/.aws/credentials, leave blank for default +
  • +
  • + +
  • +
+
+
+
+
    +
  • + Default RegionSpecify the region, such as for US West (Oregon) use ` us-west-2 ` as the region. +
  • +
  • + +
  • +
+
+
+
-
-
    -
  • - AWS Profile -
  • -
  • - -
  • -
-
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -