Cloudwatch: Revert aws sdk go v2 (#103644)
* Revert "CloudWatch: Import new grafana-aws-sdk with PDC fix (#103249)" This reverts commitf2b5b4e0c0. * Revert "CloudWatch: Migrate to aws-sdk-go-v2 (#103106)" This reverts commita65cc0df93. * make update-workspace
This commit is contained in:
@@ -1,65 +1,71 @@
|
||||
package mocks
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/aws"
|
||||
"github.com/aws/aws-sdk-go-v2/service/cloudwatch"
|
||||
cloudwatchtypes "github.com/aws/aws-sdk-go-v2/service/cloudwatch/types"
|
||||
|
||||
"github.com/grafana/grafana/pkg/tsdb/cloudwatch/models"
|
||||
|
||||
"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/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
type FakeMetricsAPI struct {
|
||||
models.CWClient
|
||||
|
||||
Metrics []cloudwatchtypes.Metric
|
||||
OwningAccounts []string
|
||||
Metrics []*cloudwatch.Metric
|
||||
OwningAccounts []*string
|
||||
MetricsPerPage int
|
||||
|
||||
cursor int
|
||||
}
|
||||
|
||||
func (c *FakeMetricsAPI) ListMetrics(_ context.Context, _ *cloudwatch.ListMetricsInput, _ ...func(*cloudwatch.Options)) (*cloudwatch.ListMetricsOutput, error) {
|
||||
func (c *FakeMetricsAPI) ListMetricsPagesWithContext(ctx aws.Context, input *cloudwatch.ListMetricsInput, fn func(*cloudwatch.ListMetricsOutput, bool) bool, opts ...request.Option) error {
|
||||
if c.MetricsPerPage == 0 {
|
||||
c.MetricsPerPage = 1000
|
||||
}
|
||||
var metrics []cloudwatchtypes.Metric
|
||||
nextToken := aws.String("yes")
|
||||
if c.cursor < len(c.Metrics) {
|
||||
end := c.cursor + c.MetricsPerPage
|
||||
if end > len(c.Metrics) {
|
||||
end = len(c.Metrics)
|
||||
nextToken = nil
|
||||
}
|
||||
metrics = c.Metrics[c.cursor:end]
|
||||
}
|
||||
c.cursor += c.MetricsPerPage
|
||||
chunks := chunkSlice(c.Metrics, c.MetricsPerPage)
|
||||
|
||||
return &cloudwatch.ListMetricsOutput{
|
||||
Metrics: metrics,
|
||||
OwningAccounts: c.OwningAccounts,
|
||||
NextToken: nextToken,
|
||||
}, nil
|
||||
for i, metrics := range chunks {
|
||||
response := fn(&cloudwatch.ListMetricsOutput{
|
||||
Metrics: metrics,
|
||||
OwningAccounts: c.OwningAccounts,
|
||||
}, i+1 == len(chunks))
|
||||
if !response {
|
||||
break
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func chunkSlice(slice []*cloudwatch.Metric, chunkSize int) [][]*cloudwatch.Metric {
|
||||
var chunks [][]*cloudwatch.Metric
|
||||
for {
|
||||
if len(slice) == 0 {
|
||||
break
|
||||
}
|
||||
if len(slice) < chunkSize {
|
||||
chunkSize = len(slice)
|
||||
}
|
||||
|
||||
chunks = append(chunks, slice[0:chunkSize])
|
||||
slice = slice[chunkSize:]
|
||||
}
|
||||
|
||||
return chunks
|
||||
}
|
||||
|
||||
type MetricsAPI struct {
|
||||
cloudwatchiface.CloudWatchAPI
|
||||
mock.Mock
|
||||
models.CWClient
|
||||
|
||||
Metrics []cloudwatchtypes.Metric
|
||||
Metrics []*cloudwatch.Metric
|
||||
}
|
||||
|
||||
func (m *MetricsAPI) GetMetricData(ctx context.Context, input *cloudwatch.GetMetricDataInput, optFns ...func(*cloudwatch.Options)) (*cloudwatch.GetMetricDataOutput, error) {
|
||||
args := m.Called(ctx, input, optFns)
|
||||
func (m *MetricsAPI) GetMetricDataWithContext(ctx aws.Context, input *cloudwatch.GetMetricDataInput, opts ...request.Option) (*cloudwatch.GetMetricDataOutput, error) {
|
||||
args := m.Called(ctx, input, opts)
|
||||
|
||||
return args.Get(0).(*cloudwatch.GetMetricDataOutput), args.Error(1)
|
||||
}
|
||||
|
||||
func (m *MetricsAPI) ListMetrics(_ context.Context, _ *cloudwatch.ListMetricsInput, _ ...func(*cloudwatch.Options)) (*cloudwatch.ListMetricsOutput, error) {
|
||||
return &cloudwatch.ListMetricsOutput{
|
||||
func (m *MetricsAPI) ListMetricsPagesWithContext(ctx aws.Context, input *cloudwatch.ListMetricsInput, fn func(*cloudwatch.ListMetricsOutput, bool) bool, opts ...request.Option) error {
|
||||
fn(&cloudwatch.ListMetricsOutput{
|
||||
Metrics: m.Metrics,
|
||||
}, m.Called().Error(0)
|
||||
}, true)
|
||||
|
||||
return m.Called().Error(0)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user