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

This commit is contained in:
Torkel Ödegaard
2015-10-29 11:44:34 +01:00
parent 374fbad06d
commit 5d64568f3e
4 changed files with 35 additions and 80 deletions
+9 -7
View File
@@ -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 {
+1 -1
View File
@@ -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)