Commit 5d64568f by Torkel Ödegaard

refactoring: some minor refactoring and changes to AWS profile PR #3053

parent 374fbad0
...@@ -12,6 +12,7 @@ import ( ...@@ -12,6 +12,7 @@ import (
"github.com/aws/aws-sdk-go/service/cloudwatch" "github.com/aws/aws-sdk-go/service/cloudwatch"
"github.com/aws/aws-sdk-go/service/ec2" "github.com/aws/aws-sdk-go/service/ec2"
"github.com/grafana/grafana/pkg/middleware" "github.com/grafana/grafana/pkg/middleware"
m "github.com/grafana/grafana/pkg/models"
) )
type actionHandler func(*cwRequest, *middleware.Context) type actionHandler func(*cwRequest, *middleware.Context)
...@@ -20,9 +21,9 @@ var actionHandlers map[string]actionHandler ...@@ -20,9 +21,9 @@ var actionHandlers map[string]actionHandler
type cwRequest struct { type cwRequest struct {
Region string `json:"region"` Region string `json:"region"`
Profile string `json:"profile"`
Action string `json:"action"` Action string `json:"action"`
Body []byte `json:"-"` Body []byte `json:"-"`
DataSource *m.DataSource
} }
func init() { func init() {
...@@ -41,7 +42,7 @@ func handleGetMetricStatistics(req *cwRequest, c *middleware.Context) { ...@@ -41,7 +42,7 @@ func handleGetMetricStatistics(req *cwRequest, c *middleware.Context) {
creds := credentials.NewChainCredentials( creds := credentials.NewChainCredentials(
[]credentials.Provider{ []credentials.Provider{
&credentials.EnvProvider{}, &credentials.EnvProvider{},
&credentials.SharedCredentialsProvider{Filename: "", Profile: req.Profile}, &credentials.SharedCredentialsProvider{Filename: "", Profile: req.DataSource.Database},
&ec2rolecreds.EC2RoleProvider{ExpiryWindow: 5 * time.Minute}, &ec2rolecreds.EC2RoleProvider{ExpiryWindow: 5 * time.Minute},
}) })
svc := cloudwatch.New(&aws.Config{ svc := cloudwatch.New(&aws.Config{
...@@ -85,7 +86,7 @@ func handleListMetrics(req *cwRequest, c *middleware.Context) { ...@@ -85,7 +86,7 @@ func handleListMetrics(req *cwRequest, c *middleware.Context) {
creds := credentials.NewChainCredentials( creds := credentials.NewChainCredentials(
[]credentials.Provider{ []credentials.Provider{
&credentials.EnvProvider{}, &credentials.EnvProvider{},
&credentials.SharedCredentialsProvider{Filename: "", Profile: req.Profile}, &credentials.SharedCredentialsProvider{Filename: "", Profile: req.DataSource.Database},
&ec2rolecreds.EC2RoleProvider{ExpiryWindow: 5 * time.Minute}, &ec2rolecreds.EC2RoleProvider{ExpiryWindow: 5 * time.Minute},
}) })
svc := cloudwatch.New(&aws.Config{ svc := cloudwatch.New(&aws.Config{
...@@ -145,9 +146,10 @@ func handleDescribeInstances(req *cwRequest, c *middleware.Context) { ...@@ -145,9 +146,10 @@ func handleDescribeInstances(req *cwRequest, c *middleware.Context) {
c.JSON(200, resp) c.JSON(200, resp)
} }
func HandleRequest(c *middleware.Context) { func HandleRequest(c *middleware.Context, ds *m.DataSource) {
var req cwRequest var req cwRequest
req.Body, _ = ioutil.ReadAll(c.Req.Request.Body) req.Body, _ = ioutil.ReadAll(c.Req.Request.Body)
req.DataSource = ds
json.Unmarshal(req.Body, &req) json.Unmarshal(req.Body, &req)
if handler, found := actionHandlers[req.Action]; !found { if handler, found := actionHandlers[req.Action]; !found {
......
...@@ -97,7 +97,7 @@ func ProxyDataSourceRequest(c *middleware.Context) { ...@@ -97,7 +97,7 @@ func ProxyDataSourceRequest(c *middleware.Context) {
} }
if ds.Type == m.DS_CLOUDWATCH { if ds.Type == m.DS_CLOUDWATCH {
cloudwatch.HandleRequest(c) cloudwatch.HandleRequest(c, ds)
} else { } else {
proxyPath := c.Params("*") proxyPath := c.Params("*")
proxy := NewReverseProxy(ds, proxyPath, targetUrl) proxy := NewReverseProxy(ds, proxyPath, targetUrl)
......
...@@ -18,7 +18,6 @@ function (angular, _) { ...@@ -18,7 +18,6 @@ function (angular, _) {
this.supportMetrics = true; this.supportMetrics = true;
this.proxyUrl = datasource.url; this.proxyUrl = datasource.url;
this.defaultRegion = datasource.jsonData.defaultRegion; this.defaultRegion = datasource.jsonData.defaultRegion;
this.profile = datasource.jsonData.profile;
} }
CloudWatchDatasource.prototype.query = function(options) { CloudWatchDatasource.prototype.query = function(options) {
...@@ -74,7 +73,6 @@ function (angular, _) { ...@@ -74,7 +73,6 @@ function (angular, _) {
CloudWatchDatasource.prototype.performTimeSeriesQuery = function(query, start, end) { CloudWatchDatasource.prototype.performTimeSeriesQuery = function(query, start, end) {
return this.awsRequest({ return this.awsRequest({
region: query.region, region: query.region,
profile: this.profile,
action: 'GetMetricStatistics', action: 'GetMetricStatistics',
parameters: { parameters: {
namespace: query.namespace, namespace: query.namespace,
...@@ -117,7 +115,6 @@ function (angular, _) { ...@@ -117,7 +115,6 @@ function (angular, _) {
CloudWatchDatasource.prototype.getDimensionValues = function(region, namespace, metricName, dimensions) { CloudWatchDatasource.prototype.getDimensionValues = function(region, namespace, metricName, dimensions) {
var request = { var request = {
region: templateSrv.replace(region), region: templateSrv.replace(region),
profile: this.profile,
action: 'ListMetrics', action: 'ListMetrics',
parameters: { parameters: {
namespace: templateSrv.replace(namespace), namespace: templateSrv.replace(namespace),
......
<br> <br>
<h5>CloudWatch details</h5> <h5>CloudWatch details</h5>
<div class="tight-form last"> <div class="editor-row">
<div class="section tight-form-container" style="margin-bottom: 20px">
<div class="tight-form">
<ul class="tight-form-list"> <ul class="tight-form-list">
<li class="tight-form-item" style="width: 160px"> <li class="tight-form-item" style="width: 200px">
Default Region<tip>Specify the region, such as for US West (Oregon) use ` us-west-2 ` as the region.</tip> Credentials profile name<tip>Credentials profile name, as specified in ~/.aws/credentials, leave blank for default</tip>
</li> </li>
<li> <li>
<input type="text" class="tight-form-input input-xlarge" ng-model='current.jsonData.defaultRegion' placeholder="" required></input> <input type="text" class="tight-form-input input-large last" ng-model='current.database' placeholder="default" required></input>
</li> </li>
</ul> </ul>
<div class="clearfix"></div> <div class="clearfix"></div>
</div> </div>
<div class="tight-form last"> <div class="tight-form">
<ul class="tight-form-list"> <ul class="tight-form-list">
<li class="tight-form-item" style="width: 160px"> <li class="tight-form-item" style="width: 200px">
AWS Profile Default Region<tip>Specify the region, such as for US West (Oregon) use ` us-west-2 ` as the region.</tip>
</li> </li>
<li> <li>
<input type="text" class="tight-form-input input-xlarge" ng-model='current.jsonData.profile' placeholder="default" required></input> <input type="text" class="tight-form-input input-large last" ng-model='current.jsonData.defaultRegion' placeholder="" required></input>
</li> </li>
</ul> </ul>
<div class="clearfix"></div> <div class="clearfix"></div>
</div>
</div>
</div> </div>
<!-- <div class="tight&#45;form" ng&#45;show="current.jsonData.access === 'direct'"> -->
<!-- <ul class="tight&#45;form&#45;list"> -->
<!-- <li class="tight&#45;form&#45;item" style="width: 160px"> -->
<!-- </li> -->
<!-- <li> -->
<!-- <input type="text" class="tight&#45;form&#45;input input&#45;xlarge" ng&#45;model='current.jsonData.accessKeyId' placeholder=""></input> -->
<!-- </li> -->
<!-- </ul> -->
<!-- <div class="clearfix"></div> -->
<!-- </div> -->
<!-- <div class="tight&#45;form" ng&#45;show="current.jsonData.access === 'direct'"> -->
<!-- <ul class="tight&#45;form&#45;list"> -->
<!-- <li class="tight&#45;form&#45;item" style="width: 160px"> -->
<!-- Access Key Id -->
<!-- </li> -->
<!-- <li> -->
<!-- <input type="text" class="tight&#45;form&#45;input input&#45;xlarge" ng&#45;model='current.jsonData.accessKeyId' placeholder=""></input> -->
<!-- </li> -->
<!-- </ul> -->
<!-- <div class="clearfix"></div> -->
<!-- </div> -->
<!-- <div class="tight&#45;form" ng&#45;show="current.jsonData.access === 'direct'"> -->
<!-- <ul class="tight&#45;form&#45;list"> -->
<!-- <li class="tight&#45;form&#45;item" style="width: 160px"> -->
<!-- Secret Access Key -->
<!-- </li> -->
<!-- <li> -->
<!-- <input type="password" class="tight&#45;form&#45;input input&#45;xlarge" ng&#45;model='current.jsonData.secretAccessKey' placeholder=""></input> -->
<!-- </li> -->
<!-- </ul> -->
<!-- <div class="clearfix"></div> -->
<!-- </div> -->
<!-- <div class="tight&#45;form last"> -->
<!-- <ul class="tight&#45;form&#45;list"> -->
<!-- <li class="tight&#45;form&#45;item" style="width: 160px"> -->
<!-- Custom Metric Attributes -->
<!-- </li> -->
<!-- <li> -->
<!-- <input type="text" class="tight&#45;form&#45;input input&#45;xlarge" -->
<!-- ng&#45;model='current.jsonData.customMetricsAttributes[0]' -->
<!-- ng&#45;init="current.jsonData.customMetricsAttributes = current.jsonData.customMetricsAttributes || []" placeholder="JSON url" bs&#45;tooltip="'Set JSON url of the result, \'aws cloudwatch list&#45;metrics &#45;&#45;output json\''"> -->
<!-- </input> -->
<!-- </li> -->
<!-- </ul> -->
<!-- <div class="clearfix"></div> -->
<!-- </div> -->
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