2020-07-23 11:52:22 -05:00
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2020-10-12 10:58:58 -05:00
|
|
|
type fakeDataSourceCfg struct {
|
|
|
|
assumeRoleARN string
|
|
|
|
externalID string
|
|
|
|
}
|
|
|
|
|
|
|
|
func fakeDataSource(cfgs ...fakeDataSourceCfg) *models.DataSource {
|
2020-07-23 11:52:22 -05:00
|
|
|
jsonData := simplejson.New()
|
2020-10-12 10:58:58 -05:00
|
|
|
jsonData.Set("defaultRegion", defaultRegion)
|
|
|
|
jsonData.Set("authType", "default")
|
|
|
|
for _, cfg := range cfgs {
|
|
|
|
if cfg.assumeRoleARN != "" {
|
|
|
|
jsonData.Set("assumeRoleArn", cfg.assumeRoleARN)
|
|
|
|
}
|
|
|
|
if cfg.externalID != "" {
|
|
|
|
jsonData.Set("externalId", cfg.externalID)
|
|
|
|
}
|
|
|
|
}
|
2020-07-23 11:52:22 -05:00
|
|
|
return &models.DataSource{
|
|
|
|
Id: 1,
|
|
|
|
Database: "default",
|
|
|
|
JsonData: jsonData,
|
|
|
|
SecureJsonData: securejsondata.SecureJsonData{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-07 04:36:13 -06:00
|
|
|
type FakeCWLogsClient struct {
|
2020-07-23 11:52:22 -05:00
|
|
|
cloudwatchlogsiface.CloudWatchLogsAPI
|
|
|
|
logGroups cloudwatchlogs.DescribeLogGroupsOutput
|
|
|
|
logGroupFields cloudwatchlogs.GetLogGroupFieldsOutput
|
|
|
|
queryResults cloudwatchlogs.GetQueryResultsOutput
|
|
|
|
}
|
|
|
|
|
2021-01-07 04:36:13 -06:00
|
|
|
func (m FakeCWLogsClient) GetQueryResultsWithContext(ctx context.Context, input *cloudwatchlogs.GetQueryResultsInput, option ...request.Option) (*cloudwatchlogs.GetQueryResultsOutput, error) {
|
2020-07-23 11:52:22 -05:00
|
|
|
return &m.queryResults, nil
|
|
|
|
}
|
|
|
|
|
2021-01-07 04:36:13 -06:00
|
|
|
func (m FakeCWLogsClient) StartQueryWithContext(ctx context.Context, input *cloudwatchlogs.StartQueryInput, option ...request.Option) (*cloudwatchlogs.StartQueryOutput, error) {
|
2020-07-23 11:52:22 -05:00
|
|
|
return &cloudwatchlogs.StartQueryOutput{
|
|
|
|
QueryId: aws.String("abcd-efgh-ijkl-mnop"),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-01-07 04:36:13 -06:00
|
|
|
func (m FakeCWLogsClient) StopQueryWithContext(ctx context.Context, input *cloudwatchlogs.StopQueryInput, option ...request.Option) (*cloudwatchlogs.StopQueryOutput, error) {
|
2020-07-23 11:52:22 -05:00
|
|
|
return &cloudwatchlogs.StopQueryOutput{
|
|
|
|
Success: aws.Bool(true),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-01-07 04:36:13 -06:00
|
|
|
func (m FakeCWLogsClient) DescribeLogGroupsWithContext(ctx context.Context, input *cloudwatchlogs.DescribeLogGroupsInput, option ...request.Option) (*cloudwatchlogs.DescribeLogGroupsOutput, error) {
|
2020-07-23 11:52:22 -05:00
|
|
|
return &m.logGroups, nil
|
|
|
|
}
|
|
|
|
|
2021-01-07 04:36:13 -06:00
|
|
|
func (m FakeCWLogsClient) GetLogGroupFieldsWithContext(ctx context.Context, input *cloudwatchlogs.GetLogGroupFieldsInput, option ...request.Option) (*cloudwatchlogs.GetLogGroupFieldsOutput, error) {
|
2020-07-23 11:52:22 -05:00
|
|
|
return &m.logGroupFields, nil
|
|
|
|
}
|
|
|
|
|
2021-01-07 04:36:13 -06:00
|
|
|
type FakeCWClient struct {
|
2020-07-23 11:52:22 -05:00
|
|
|
cloudwatchiface.CloudWatchAPI
|
|
|
|
|
2021-01-07 04:36:13 -06:00
|
|
|
Metrics []*cloudwatch.Metric
|
2020-07-23 11:52:22 -05:00
|
|
|
}
|
|
|
|
|
2021-01-07 04:36:13 -06:00
|
|
|
func (c FakeCWClient) ListMetricsPages(input *cloudwatch.ListMetricsInput, fn func(*cloudwatch.ListMetricsOutput, bool) bool) error {
|
2020-07-23 11:52:22 -05:00
|
|
|
fn(&cloudwatch.ListMetricsOutput{
|
2021-01-07 04:36:13 -06:00
|
|
|
Metrics: c.Metrics,
|
2020-07-23 11:52:22 -05:00
|
|
|
}, 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
|
|
|
|
}
|