Commit fe9fca38 by Mitsuhiro Tanda

move cloudwatch crendential related code

parent 4f5f38f4
...@@ -17,7 +17,6 @@ import ( ...@@ -17,7 +17,6 @@ import (
"github.com/aws/aws-sdk-go/aws/request" "github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudwatch" "github.com/aws/aws-sdk-go/service/cloudwatch"
cwapi "github.com/grafana/grafana/pkg/api/cloudwatch"
"github.com/grafana/grafana/pkg/components/null" "github.com/grafana/grafana/pkg/components/null"
"github.com/grafana/grafana/pkg/components/simplejson" "github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/metrics" "github.com/grafana/grafana/pkg/metrics"
...@@ -128,7 +127,7 @@ func (e *CloudWatchExecutor) getClient(region string) (*cloudwatch.CloudWatch, e ...@@ -128,7 +127,7 @@ func (e *CloudWatchExecutor) getClient(region string) (*cloudwatch.CloudWatch, e
} }
} }
datasourceInfo := &cwapi.DatasourceInfo{ datasourceInfo := &DatasourceInfo{
Region: region, Region: region,
Profile: e.DataSource.Database, Profile: e.DataSource.Database,
AssumeRoleArn: assumeRoleArn, AssumeRoleArn: assumeRoleArn,
...@@ -136,7 +135,7 @@ func (e *CloudWatchExecutor) getClient(region string) (*cloudwatch.CloudWatch, e ...@@ -136,7 +135,7 @@ func (e *CloudWatchExecutor) getClient(region string) (*cloudwatch.CloudWatch, e
SecretKey: secretKey, SecretKey: secretKey,
} }
credentials, err := cwapi.GetCredentials(datasourceInfo) credentials, err := GetCredentials(datasourceInfo)
if err != nil { if err != nil {
return nil, err return nil, err
} }
......
...@@ -183,14 +183,14 @@ func ec2RoleProvider(sess *session.Session) credentials.Provider { ...@@ -183,14 +183,14 @@ func ec2RoleProvider(sess *session.Session) credentials.Provider {
return &ec2rolecreds.EC2RoleProvider{Client: ec2metadata.New(sess), ExpiryWindow: 5 * time.Minute} return &ec2rolecreds.EC2RoleProvider{Client: ec2metadata.New(sess), ExpiryWindow: 5 * time.Minute}
} }
func getAwsConfig(req *cwRequest) (*aws.Config, error) { func getAwsConfig(dsInfo *DatasourceInfo) (*aws.Config, error) {
creds, err := GetCredentials(req.GetDatasourceInfo()) creds, err := GetCredentials(dsInfo)
if err != nil { if err != nil {
return nil, err return nil, err
} }
cfg := &aws.Config{ cfg := &aws.Config{
Region: aws.String(req.Region), Region: aws.String(dsInfo.Region),
Credentials: creds, Credentials: creds,
} }
return cfg, nil return cfg, nil
......
...@@ -14,7 +14,6 @@ import ( ...@@ -14,7 +14,6 @@ import (
"github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/aws/session"
"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"
cwapi "github.com/grafana/grafana/pkg/api/cloudwatch"
"github.com/grafana/grafana/pkg/components/simplejson" "github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/metrics" "github.com/grafana/grafana/pkg/metrics"
"github.com/grafana/grafana/pkg/tsdb" "github.com/grafana/grafana/pkg/tsdb"
...@@ -211,7 +210,7 @@ func transformToTable(data []suggestData, result *tsdb.QueryResult) { ...@@ -211,7 +210,7 @@ func transformToTable(data []suggestData, result *tsdb.QueryResult) {
result.Meta.Set("rowCount", len(data)) result.Meta.Set("rowCount", len(data))
} }
func (e *CloudWatchExecutor) getDsInfo(region string) *cwapi.DatasourceInfo { func (e *CloudWatchExecutor) getDsInfo(region string) *DatasourceInfo {
assumeRoleArn := e.DataSource.JsonData.Get("assumeRoleArn").MustString() assumeRoleArn := e.DataSource.JsonData.Get("assumeRoleArn").MustString()
accessKey := "" accessKey := ""
secretKey := "" secretKey := ""
...@@ -224,7 +223,7 @@ func (e *CloudWatchExecutor) getDsInfo(region string) *cwapi.DatasourceInfo { ...@@ -224,7 +223,7 @@ func (e *CloudWatchExecutor) getDsInfo(region string) *cwapi.DatasourceInfo {
} }
} }
datasourceInfo := &cwapi.DatasourceInfo{ datasourceInfo := &DatasourceInfo{
Region: region, Region: region,
Profile: e.DataSource.Database, Profile: e.DataSource.Database,
AssumeRoleArn: assumeRoleArn, AssumeRoleArn: assumeRoleArn,
...@@ -460,19 +459,6 @@ func (e *CloudWatchExecutor) handleGetEc2InstanceAttribute(ctx context.Context, ...@@ -460,19 +459,6 @@ func (e *CloudWatchExecutor) handleGetEc2InstanceAttribute(ctx context.Context,
return result, nil return result, nil
} }
func getAwsConfig(dsInfo *cwapi.DatasourceInfo) (*aws.Config, error) {
creds, err := cwapi.GetCredentials(dsInfo)
if err != nil {
return nil, err
}
cfg := &aws.Config{
Region: aws.String(dsInfo.Region),
Credentials: creds,
}
return cfg, nil
}
func (e *CloudWatchExecutor) cloudwatchListMetrics(region string, namespace string, metricName string, dimensions []*cloudwatch.DimensionFilter) (*cloudwatch.ListMetricsOutput, error) { func (e *CloudWatchExecutor) cloudwatchListMetrics(region string, namespace string, metricName string, dimensions []*cloudwatch.DimensionFilter) (*cloudwatch.ListMetricsOutput, error) {
dsInfo := e.getDsInfo(region) dsInfo := e.getDsInfo(region)
cfg, err := getAwsConfig(dsInfo) cfg, err := getAwsConfig(dsInfo)
...@@ -541,8 +527,8 @@ func (e *CloudWatchExecutor) ec2DescribeInstances(region string, filters []*ec2. ...@@ -541,8 +527,8 @@ func (e *CloudWatchExecutor) ec2DescribeInstances(region string, filters []*ec2.
return &resp, nil return &resp, nil
} }
func getAllMetrics(cwData *cwapi.DatasourceInfo) (cloudwatch.ListMetricsOutput, error) { func getAllMetrics(cwData *DatasourceInfo) (cloudwatch.ListMetricsOutput, error) {
creds, err := cwapi.GetCredentials(cwData) creds, err := GetCredentials(cwData)
if err != nil { if err != nil {
return cloudwatch.ListMetricsOutput{}, err return cloudwatch.ListMetricsOutput{}, err
} }
...@@ -579,7 +565,7 @@ func getAllMetrics(cwData *cwapi.DatasourceInfo) (cloudwatch.ListMetricsOutput, ...@@ -579,7 +565,7 @@ func getAllMetrics(cwData *cwapi.DatasourceInfo) (cloudwatch.ListMetricsOutput,
var metricsCacheLock sync.Mutex var metricsCacheLock sync.Mutex
func getMetricsForCustomMetrics(dsInfo *cwapi.DatasourceInfo, getAllMetrics func(*cwapi.DatasourceInfo) (cloudwatch.ListMetricsOutput, error)) ([]string, error) { func getMetricsForCustomMetrics(dsInfo *DatasourceInfo, getAllMetrics func(*DatasourceInfo) (cloudwatch.ListMetricsOutput, error)) ([]string, error) {
metricsCacheLock.Lock() metricsCacheLock.Lock()
defer metricsCacheLock.Unlock() defer metricsCacheLock.Unlock()
...@@ -616,7 +602,7 @@ func getMetricsForCustomMetrics(dsInfo *cwapi.DatasourceInfo, getAllMetrics func ...@@ -616,7 +602,7 @@ func getMetricsForCustomMetrics(dsInfo *cwapi.DatasourceInfo, getAllMetrics func
var dimensionsCacheLock sync.Mutex var dimensionsCacheLock sync.Mutex
func getDimensionsForCustomMetrics(dsInfo *cwapi.DatasourceInfo, getAllMetrics func(*cwapi.DatasourceInfo) (cloudwatch.ListMetricsOutput, error)) ([]string, error) { func getDimensionsForCustomMetrics(dsInfo *DatasourceInfo, getAllMetrics func(*DatasourceInfo) (cloudwatch.ListMetricsOutput, error)) ([]string, error) {
dimensionsCacheLock.Lock() dimensionsCacheLock.Lock()
defer dimensionsCacheLock.Unlock() defer dimensionsCacheLock.Unlock()
......
...@@ -5,20 +5,19 @@ import ( ...@@ -5,20 +5,19 @@ import (
"github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudwatch" "github.com/aws/aws-sdk-go/service/cloudwatch"
cwapi "github.com/grafana/grafana/pkg/api/cloudwatch"
. "github.com/smartystreets/goconvey/convey" . "github.com/smartystreets/goconvey/convey"
) )
func TestCloudWatchMetrics(t *testing.T) { func TestCloudWatchMetrics(t *testing.T) {
Convey("When calling getMetricsForCustomMetrics", t, func() { Convey("When calling getMetricsForCustomMetrics", t, func() {
dsInfo := &cwapi.DatasourceInfo{ dsInfo := &DatasourceInfo{
Region: "us-east-1", Region: "us-east-1",
Namespace: "Foo", Namespace: "Foo",
Profile: "default", Profile: "default",
AssumeRoleArn: "", AssumeRoleArn: "",
} }
f := func(dsInfo *cwapi.DatasourceInfo) (cloudwatch.ListMetricsOutput, error) { f := func(dsInfo *DatasourceInfo) (cloudwatch.ListMetricsOutput, error) {
return cloudwatch.ListMetricsOutput{ return cloudwatch.ListMetricsOutput{
Metrics: []*cloudwatch.Metric{ Metrics: []*cloudwatch.Metric{
{ {
...@@ -40,13 +39,13 @@ func TestCloudWatchMetrics(t *testing.T) { ...@@ -40,13 +39,13 @@ func TestCloudWatchMetrics(t *testing.T) {
}) })
Convey("When calling getDimensionsForCustomMetrics", t, func() { Convey("When calling getDimensionsForCustomMetrics", t, func() {
dsInfo := &cwapi.DatasourceInfo{ dsInfo := &DatasourceInfo{
Region: "us-east-1", Region: "us-east-1",
Namespace: "Foo", Namespace: "Foo",
Profile: "default", Profile: "default",
AssumeRoleArn: "", AssumeRoleArn: "",
} }
f := func(dsInfo *cwapi.DatasourceInfo) (cloudwatch.ListMetricsOutput, error) { f := func(dsInfo *DatasourceInfo) (cloudwatch.ListMetricsOutput, error) {
return cloudwatch.ListMetricsOutput{ return cloudwatch.ListMetricsOutput{
Metrics: []*cloudwatch.Metric{ Metrics: []*cloudwatch.Metric{
{ {
......
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