Commit fe9fca38 by Mitsuhiro Tanda

move cloudwatch crendential related code

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