Cloudwatch: Refactor dimension keys resource request (#57148)
* use new layered architecture in get dimension keys request * go lint fixes * pr feedback * more pr feedback * remove not used code * refactor route middleware * change signature * add integration tests for the dimension keys route * use request suffix instead of query * use typed args also in frontend * remove unused import * harmonize naming * fix merge conflict
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
package mocks
|
||||
|
||||
import (
|
||||
"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"
|
||||
)
|
||||
|
||||
type FakeMetricsAPI struct {
|
||||
cloudwatchiface.CloudWatchAPI
|
||||
cloudwatch.GetMetricDataOutput
|
||||
|
||||
Metrics []*cloudwatch.Metric
|
||||
MetricsPerPage int
|
||||
|
||||
CallsGetMetricDataWithContext []*cloudwatch.GetMetricDataInput
|
||||
}
|
||||
|
||||
func (c *FakeMetricsAPI) GetMetricDataWithContext(ctx aws.Context, input *cloudwatch.GetMetricDataInput, opts ...request.Option) (*cloudwatch.GetMetricDataOutput, error) {
|
||||
c.CallsGetMetricDataWithContext = append(c.CallsGetMetricDataWithContext, input)
|
||||
|
||||
return &c.GetMetricDataOutput, nil
|
||||
}
|
||||
|
||||
func (c *FakeMetricsAPI) ListMetricsPages(input *cloudwatch.ListMetricsInput, fn func(*cloudwatch.ListMetricsOutput, bool) bool) error {
|
||||
if c.MetricsPerPage == 0 {
|
||||
c.MetricsPerPage = 1000
|
||||
}
|
||||
chunks := chunkSlice(c.Metrics, c.MetricsPerPage)
|
||||
|
||||
for i, metrics := range chunks {
|
||||
response := fn(&cloudwatch.ListMetricsOutput{
|
||||
Metrics: metrics,
|
||||
}, 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
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package mocks
|
||||
|
||||
import (
|
||||
"github.com/grafana/grafana/pkg/tsdb/cloudwatch/models"
|
||||
"github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
type ListMetricsServiceMock struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
func (a *ListMetricsServiceMock) GetDimensionKeysByDimensionFilter(*models.DimensionKeysRequest) ([]string, error) {
|
||||
args := a.Called()
|
||||
|
||||
return args.Get(0).([]string), args.Error(1)
|
||||
}
|
||||
|
||||
func (a *ListMetricsServiceMock) GetDimensionKeysByNamespace(string) ([]string, error) {
|
||||
args := a.Called()
|
||||
|
||||
return args.Get(0).([]string), args.Error(1)
|
||||
}
|
||||
|
||||
func (a *ListMetricsServiceMock) GetHardCodedDimensionKeysByNamespace(string) ([]string, error) {
|
||||
args := a.Called()
|
||||
|
||||
return args.Get(0).([]string), args.Error(1)
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package mocks
|
||||
|
||||
import (
|
||||
"github.com/aws/aws-sdk-go/service/cloudwatch"
|
||||
"github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
type FakeMetricsClient struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
func (m *FakeMetricsClient) ListMetricsWithPageLimit(params *cloudwatch.ListMetricsInput) ([]*cloudwatch.Metric, error) {
|
||||
args := m.Called(params)
|
||||
return args.Get(0).([]*cloudwatch.Metric), args.Error(1)
|
||||
}
|
||||
Reference in New Issue
Block a user