mirror of https://github.com/grafana/grafana
cloudwatch: Consolidate client logic (#25555)
* cloudwatch: Consolidate client logic Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com>pull/26515/head^2
parent
80edbbe314
commit
43ef052d57
@ -1,106 +0,0 @@ |
||||
package cloudwatch |
||||
|
||||
import ( |
||||
"context" |
||||
|
||||
"github.com/aws/aws-sdk-go/aws" |
||||
"github.com/aws/aws-sdk-go/aws/request" |
||||
"github.com/aws/aws-sdk-go/service/cloudwatchlogs" |
||||
"github.com/aws/aws-sdk-go/service/cloudwatchlogs/cloudwatchlogsiface" |
||||
) |
||||
|
||||
type FakeLogsClient struct { |
||||
cloudwatchlogsiface.CloudWatchLogsAPI |
||||
Config aws.Config |
||||
} |
||||
|
||||
func (f FakeLogsClient) DescribeLogGroupsWithContext(ctx context.Context, input *cloudwatchlogs.DescribeLogGroupsInput, option ...request.Option) (*cloudwatchlogs.DescribeLogGroupsOutput, error) { |
||||
return &cloudwatchlogs.DescribeLogGroupsOutput{ |
||||
LogGroups: []*cloudwatchlogs.LogGroup{ |
||||
{ |
||||
LogGroupName: aws.String("group_a"), |
||||
}, |
||||
{ |
||||
LogGroupName: aws.String("group_b"), |
||||
}, |
||||
{ |
||||
LogGroupName: aws.String("group_c"), |
||||
}, |
||||
}, |
||||
}, nil |
||||
} |
||||
|
||||
func (f FakeLogsClient) GetLogGroupFieldsWithContext(ctx context.Context, input *cloudwatchlogs.GetLogGroupFieldsInput, option ...request.Option) (*cloudwatchlogs.GetLogGroupFieldsOutput, error) { |
||||
return &cloudwatchlogs.GetLogGroupFieldsOutput{ |
||||
LogGroupFields: []*cloudwatchlogs.LogGroupField{ |
||||
{ |
||||
Name: aws.String("field_a"), |
||||
Percent: aws.Int64(100), |
||||
}, |
||||
{ |
||||
Name: aws.String("field_b"), |
||||
Percent: aws.Int64(30), |
||||
}, |
||||
{ |
||||
Name: aws.String("field_c"), |
||||
Percent: aws.Int64(55), |
||||
}, |
||||
}, |
||||
}, nil |
||||
} |
||||
|
||||
func (f FakeLogsClient) StartQueryWithContext(ctx context.Context, input *cloudwatchlogs.StartQueryInput, option ...request.Option) (*cloudwatchlogs.StartQueryOutput, error) { |
||||
return &cloudwatchlogs.StartQueryOutput{ |
||||
QueryId: aws.String("abcd-efgh-ijkl-mnop"), |
||||
}, nil |
||||
} |
||||
|
||||
func (f FakeLogsClient) StopQueryWithContext(ctx context.Context, input *cloudwatchlogs.StopQueryInput, option ...request.Option) (*cloudwatchlogs.StopQueryOutput, error) { |
||||
return &cloudwatchlogs.StopQueryOutput{ |
||||
Success: aws.Bool(true), |
||||
}, nil |
||||
} |
||||
|
||||
func (f FakeLogsClient) GetQueryResultsWithContext(ctx context.Context, input *cloudwatchlogs.GetQueryResultsInput, option ...request.Option) (*cloudwatchlogs.GetQueryResultsOutput, error) { |
||||
return &cloudwatchlogs.GetQueryResultsOutput{ |
||||
Results: [][]*cloudwatchlogs.ResultField{ |
||||
{ |
||||
{ |
||||
Field: aws.String("@timestamp"), |
||||
Value: aws.String("2020-03-20 10:37:23.000"), |
||||
}, |
||||
{ |
||||
Field: aws.String("field_b"), |
||||
Value: aws.String("b_1"), |
||||
}, |
||||
{ |
||||
Field: aws.String("@ptr"), |
||||
Value: aws.String("abcdefg"), |
||||
}, |
||||
}, |
||||
|
||||
{ |
||||
{ |
||||
Field: aws.String("@timestamp"), |
||||
Value: aws.String("2020-03-20 10:40:43.000"), |
||||
}, |
||||
{ |
||||
Field: aws.String("field_b"), |
||||
Value: aws.String("b_2"), |
||||
}, |
||||
{ |
||||
Field: aws.String("@ptr"), |
||||
Value: aws.String("hijklmnop"), |
||||
}, |
||||
}, |
||||
}, |
||||
|
||||
Statistics: &cloudwatchlogs.QueryStatistics{ |
||||
BytesScanned: aws.Float64(512), |
||||
RecordsMatched: aws.Float64(256), |
||||
RecordsScanned: aws.Float64(1024), |
||||
}, |
||||
|
||||
Status: aws.String("Complete"), |
||||
}, nil |
||||
} |
@ -0,0 +1,133 @@ |
||||
package cloudwatch |
||||
|
||||
import ( |
||||
"context" |
||||
|
||||
"github.com/aws/aws-sdk-go/aws" |
||||
"github.com/aws/aws-sdk-go/aws/request" |
||||
"github.com/aws/aws-sdk-go/service/cloudwatch" |
||||
"github.com/aws/aws-sdk-go/service/cloudwatch/cloudwatchiface" |
||||
"github.com/aws/aws-sdk-go/service/cloudwatchlogs" |
||||
"github.com/aws/aws-sdk-go/service/cloudwatchlogs/cloudwatchlogsiface" |
||||
"github.com/aws/aws-sdk-go/service/ec2" |
||||
"github.com/aws/aws-sdk-go/service/ec2/ec2iface" |
||||
"github.com/aws/aws-sdk-go/service/resourcegroupstaggingapi" |
||||
"github.com/aws/aws-sdk-go/service/resourcegroupstaggingapi/resourcegroupstaggingapiiface" |
||||
"github.com/grafana/grafana/pkg/components/securejsondata" |
||||
"github.com/grafana/grafana/pkg/components/simplejson" |
||||
"github.com/grafana/grafana/pkg/models" |
||||
) |
||||
|
||||
func fakeDataSource() *models.DataSource { |
||||
jsonData := simplejson.New() |
||||
jsonData.Set("defaultRegion", "default") |
||||
return &models.DataSource{ |
||||
Id: 1, |
||||
Database: "default", |
||||
JsonData: jsonData, |
||||
SecureJsonData: securejsondata.SecureJsonData{}, |
||||
} |
||||
} |
||||
|
||||
type fakeCWLogsClient struct { |
||||
cloudwatchlogsiface.CloudWatchLogsAPI |
||||
logGroups cloudwatchlogs.DescribeLogGroupsOutput |
||||
logGroupFields cloudwatchlogs.GetLogGroupFieldsOutput |
||||
queryResults cloudwatchlogs.GetQueryResultsOutput |
||||
} |
||||
|
||||
func (m fakeCWLogsClient) GetQueryResultsWithContext(ctx context.Context, input *cloudwatchlogs.GetQueryResultsInput, option ...request.Option) (*cloudwatchlogs.GetQueryResultsOutput, error) { |
||||
return &m.queryResults, nil |
||||
} |
||||
|
||||
func (m fakeCWLogsClient) StartQueryWithContext(ctx context.Context, input *cloudwatchlogs.StartQueryInput, option ...request.Option) (*cloudwatchlogs.StartQueryOutput, error) { |
||||
return &cloudwatchlogs.StartQueryOutput{ |
||||
QueryId: aws.String("abcd-efgh-ijkl-mnop"), |
||||
}, nil |
||||
} |
||||
|
||||
func (m fakeCWLogsClient) StopQueryWithContext(ctx context.Context, input *cloudwatchlogs.StopQueryInput, option ...request.Option) (*cloudwatchlogs.StopQueryOutput, error) { |
||||
return &cloudwatchlogs.StopQueryOutput{ |
||||
Success: aws.Bool(true), |
||||
}, nil |
||||
} |
||||
|
||||
func (m fakeCWLogsClient) DescribeLogGroupsWithContext(ctx context.Context, input *cloudwatchlogs.DescribeLogGroupsInput, option ...request.Option) (*cloudwatchlogs.DescribeLogGroupsOutput, error) { |
||||
return &m.logGroups, nil |
||||
} |
||||
|
||||
func (m fakeCWLogsClient) GetLogGroupFieldsWithContext(ctx context.Context, input *cloudwatchlogs.GetLogGroupFieldsInput, option ...request.Option) (*cloudwatchlogs.GetLogGroupFieldsOutput, error) { |
||||
return &m.logGroupFields, nil |
||||
} |
||||
|
||||
type fakeCWClient struct { |
||||
cloudwatchiface.CloudWatchAPI |
||||
|
||||
metrics []*cloudwatch.Metric |
||||
} |
||||
|
||||
func (c fakeCWClient) ListMetricsPages(input *cloudwatch.ListMetricsInput, fn func(*cloudwatch.ListMetricsOutput, bool) bool) error { |
||||
fn(&cloudwatch.ListMetricsOutput{ |
||||
Metrics: c.metrics, |
||||
}, true) |
||||
return nil |
||||
} |
||||
|
||||
type fakeEC2Client struct { |
||||
ec2iface.EC2API |
||||
|
||||
regions []string |
||||
reservations []*ec2.Reservation |
||||
} |
||||
|
||||
func (c fakeEC2Client) DescribeRegions(*ec2.DescribeRegionsInput) (*ec2.DescribeRegionsOutput, error) { |
||||
regions := []*ec2.Region{} |
||||
for _, region := range c.regions { |
||||
regions = append(regions, &ec2.Region{ |
||||
RegionName: aws.String(region), |
||||
}) |
||||
} |
||||
return &ec2.DescribeRegionsOutput{ |
||||
Regions: regions, |
||||
}, nil |
||||
} |
||||
|
||||
func (c fakeEC2Client) DescribeInstancesPages(in *ec2.DescribeInstancesInput, |
||||
fn func(*ec2.DescribeInstancesOutput, bool) bool) error { |
||||
reservations := []*ec2.Reservation{} |
||||
for _, r := range c.reservations { |
||||
instances := []*ec2.Instance{} |
||||
for _, inst := range r.Instances { |
||||
if len(in.InstanceIds) == 0 { |
||||
instances = append(instances, inst) |
||||
continue |
||||
} |
||||
|
||||
for _, id := range in.InstanceIds { |
||||
if *inst.InstanceId == *id { |
||||
instances = append(instances, inst) |
||||
} |
||||
} |
||||
} |
||||
reservation := &ec2.Reservation{Instances: instances} |
||||
reservations = append(reservations, reservation) |
||||
} |
||||
fn(&ec2.DescribeInstancesOutput{ |
||||
Reservations: reservations, |
||||
}, true) |
||||
return nil |
||||
} |
||||
|
||||
type fakeRGTAClient struct { |
||||
resourcegroupstaggingapiiface.ResourceGroupsTaggingAPIAPI |
||||
|
||||
tagMapping []*resourcegroupstaggingapi.ResourceTagMapping |
||||
} |
||||
|
||||
func (c fakeRGTAClient) GetResourcesPages(in *resourcegroupstaggingapi.GetResourcesInput, |
||||
fn func(*resourcegroupstaggingapi.GetResourcesOutput, bool) bool) error { |
||||
fn(&resourcegroupstaggingapi.GetResourcesOutput{ |
||||
ResourceTagMappingList: c.tagMapping, |
||||
}, true) |
||||
return nil |
||||
} |
Loading…
Reference in new issue