diff --git a/pkg/tsdb/cloudwatch/annotation_query.go b/pkg/tsdb/cloudwatch/annotation_query.go index 2283e12b64e..3736c19b100 100644 --- a/pkg/tsdb/cloudwatch/annotation_query.go +++ b/pkg/tsdb/cloudwatch/annotation_query.go @@ -6,7 +6,6 @@ import ( "time" "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/cloudwatch" "github.com/grafana/grafana/pkg/components/simplejson" "github.com/grafana/grafana/pkg/tsdb" @@ -34,16 +33,10 @@ func (e *CloudWatchExecutor) executeAnnotationQuery(ctx context.Context, queryCo actionPrefix := parameters.Get("actionPrefix").MustString("") alarmNamePrefix := parameters.Get("alarmNamePrefix").MustString("") - dsInfo := e.getDsInfo(region) - cfg, err := getAwsConfig(dsInfo) + svc, err := e.getClient(region) if err != nil { - return nil, errors.New("Failed to call cloudwatch:ListMetrics") + return nil, err } - sess, err := session.NewSession(cfg) - if err != nil { - return nil, errors.New("Failed to call cloudwatch:ListMetrics") - } - svc := cloudwatch.New(sess, cfg) var alarmNames []*string if usePrefixMatch { diff --git a/pkg/tsdb/cloudwatch/cloudwatch.go b/pkg/tsdb/cloudwatch/cloudwatch.go index 17bf8ccd08a..6f4cad3dec6 100644 --- a/pkg/tsdb/cloudwatch/cloudwatch.go +++ b/pkg/tsdb/cloudwatch/cloudwatch.go @@ -15,7 +15,6 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/request" - "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/cloudwatch" "github.com/grafana/grafana/pkg/components/null" "github.com/grafana/grafana/pkg/components/simplejson" @@ -26,6 +25,17 @@ type CloudWatchExecutor struct { *models.DataSource } +type DatasourceInfo struct { + Profile string + Region string + AuthType string + AssumeRoleArn string + Namespace string + + AccessKey string + SecretKey string +} + func NewCloudWatchExecutor(dsInfo *models.DataSource) (tsdb.TsdbQueryEndpoint, error) { return &CloudWatchExecutor{}, nil } @@ -113,47 +123,6 @@ func (e *CloudWatchExecutor) executeTimeSeriesQuery(ctx context.Context, queryCo return result, nil } -func (e *CloudWatchExecutor) getClient(region string) (*cloudwatch.CloudWatch, error) { - assumeRoleArn := e.DataSource.JsonData.Get("assumeRoleArn").MustString() - - accessKey := "" - secretKey := "" - for key, value := range e.DataSource.SecureJsonData.Decrypt() { - if key == "accessKey" { - accessKey = value - } - if key == "secretKey" { - secretKey = value - } - } - - datasourceInfo := &DatasourceInfo{ - Region: region, - Profile: e.DataSource.Database, - AssumeRoleArn: assumeRoleArn, - AccessKey: accessKey, - SecretKey: secretKey, - } - - credentials, err := GetCredentials(datasourceInfo) - if err != nil { - return nil, err - } - - cfg := &aws.Config{ - Region: aws.String(region), - Credentials: credentials, - } - - sess, err := session.NewSession(cfg) - if err != nil { - return nil, err - } - - client := cloudwatch.New(sess, cfg) - return client, nil -} - func (e *CloudWatchExecutor) executeQuery(ctx context.Context, parameters *simplejson.Json, queryContext *tsdb.TsdbQuery) (*tsdb.QueryResult, error) { query, err := parseQuery(parameters) if err != nil { diff --git a/pkg/tsdb/cloudwatch/credentials.go b/pkg/tsdb/cloudwatch/credentials.go index 882d2e3320a..81e0262a3d4 100644 --- a/pkg/tsdb/cloudwatch/credentials.go +++ b/pkg/tsdb/cloudwatch/credentials.go @@ -13,20 +13,10 @@ import ( "github.com/aws/aws-sdk-go/aws/credentials/endpointcreds" "github.com/aws/aws-sdk-go/aws/ec2metadata" "github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/service/cloudwatch" "github.com/aws/aws-sdk-go/service/sts" ) -type DatasourceInfo struct { - Profile string - Region string - AuthType string - AssumeRoleArn string - Namespace string - - AccessKey string - SecretKey string -} - type cache struct { credential *credentials.Credentials expiration *time.Time @@ -150,7 +140,31 @@ func ec2RoleProvider(sess *session.Session) credentials.Provider { return &ec2rolecreds.EC2RoleProvider{Client: ec2metadata.New(sess), ExpiryWindow: 5 * time.Minute} } -func getAwsConfig(dsInfo *DatasourceInfo) (*aws.Config, error) { +func (e *CloudWatchExecutor) getDsInfo(region string) *DatasourceInfo { + assumeRoleArn := e.DataSource.JsonData.Get("assumeRoleArn").MustString() + accessKey := "" + secretKey := "" + for key, value := range e.DataSource.SecureJsonData.Decrypt() { + if key == "accessKey" { + accessKey = value + } + if key == "secretKey" { + secretKey = value + } + } + + datasourceInfo := &DatasourceInfo{ + Region: region, + Profile: e.DataSource.Database, + AssumeRoleArn: assumeRoleArn, + AccessKey: accessKey, + SecretKey: secretKey, + } + + return datasourceInfo +} + +func (e *CloudWatchExecutor) getAwsConfig(dsInfo *DatasourceInfo) (*aws.Config, error) { creds, err := GetCredentials(dsInfo) if err != nil { return nil, err @@ -162,3 +176,19 @@ func getAwsConfig(dsInfo *DatasourceInfo) (*aws.Config, error) { } return cfg, nil } + +func (e *CloudWatchExecutor) getClient(region string) (*cloudwatch.CloudWatch, error) { + datasourceInfo := e.getDsInfo(region) + cfg, err := e.getAwsConfig(datasourceInfo) + if err != nil { + return nil, err + } + + sess, err := session.NewSession(cfg) + if err != nil { + return nil, err + } + + client := cloudwatch.New(sess, cfg) + return client, nil +} diff --git a/pkg/tsdb/cloudwatch/metric_find_query.go b/pkg/tsdb/cloudwatch/metric_find_query.go index 12ca4ed2d78..bc5525965d8 100644 --- a/pkg/tsdb/cloudwatch/metric_find_query.go +++ b/pkg/tsdb/cloudwatch/metric_find_query.go @@ -210,30 +210,6 @@ func transformToTable(data []suggestData, result *tsdb.QueryResult) { result.Meta.Set("rowCount", len(data)) } -func (e *CloudWatchExecutor) getDsInfo(region string) *DatasourceInfo { - assumeRoleArn := e.DataSource.JsonData.Get("assumeRoleArn").MustString() - accessKey := "" - secretKey := "" - for key, value := range e.DataSource.SecureJsonData.Decrypt() { - if key == "accessKey" { - accessKey = value - } - if key == "secretKey" { - secretKey = value - } - } - - datasourceInfo := &DatasourceInfo{ - Region: region, - Profile: e.DataSource.Database, - AssumeRoleArn: assumeRoleArn, - AccessKey: accessKey, - SecretKey: secretKey, - } - - return datasourceInfo -} - // Whenever this list is updated, frontend list should also be updated. // Please update the region list in public/app/plugins/datasource/cloudwatch/partials/config.html func (e *CloudWatchExecutor) handleGetRegions(ctx context.Context, parameters *simplejson.Json, queryContext *tsdb.TsdbQuery) ([]suggestData, error) { @@ -460,16 +436,10 @@ func (e *CloudWatchExecutor) handleGetEc2InstanceAttribute(ctx context.Context, } func (e *CloudWatchExecutor) cloudwatchListMetrics(region string, namespace string, metricName string, dimensions []*cloudwatch.DimensionFilter) (*cloudwatch.ListMetricsOutput, error) { - dsInfo := e.getDsInfo(region) - cfg, err := getAwsConfig(dsInfo) + svc, err := e.getClient(region) if err != nil { - return nil, errors.New("Failed to call cloudwatch:ListMetrics") + return nil, err } - sess, err := session.NewSession(cfg) - if err != nil { - return nil, errors.New("Failed to call cloudwatch:ListMetrics") - } - svc := cloudwatch.New(sess, cfg) params := &cloudwatch.ListMetricsInput{ Namespace: aws.String(namespace), @@ -496,7 +466,7 @@ func (e *CloudWatchExecutor) cloudwatchListMetrics(region string, namespace stri func (e *CloudWatchExecutor) ec2DescribeInstances(region string, filters []*ec2.Filter, instanceIds []*string) (*ec2.DescribeInstancesOutput, error) { dsInfo := e.getDsInfo(region) - cfg, err := getAwsConfig(dsInfo) + cfg, err := e.getAwsConfig(dsInfo) if err != nil { return nil, errors.New("Failed to call ec2:DescribeInstances") }