CloudWatch: fix custom namespace for listing dimension keys, refactor to non-pointer types, add test assertions, rename packages (#59106)
Co-authored-by: Erik Sundell <erik.sundell87@gmail.com>
This commit is contained in:
@@ -4,7 +4,7 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/grafana/grafana/pkg/tsdb/cloudwatch/constants"
|
||||
"github.com/grafana/grafana/pkg/tsdb/cloudwatch/models"
|
||||
"github.com/grafana/grafana/pkg/tsdb/cloudwatch/models/resources"
|
||||
)
|
||||
|
||||
var GetHardCodedDimensionKeysByNamespace = func(namespace string) ([]string, error) {
|
||||
@@ -16,8 +16,8 @@ var GetHardCodedDimensionKeysByNamespace = func(namespace string) ([]string, err
|
||||
return dimensionKeys, nil
|
||||
}
|
||||
|
||||
var GetHardCodedMetricsByNamespace = func(namespace string) ([]models.Metric, error) {
|
||||
response := []models.Metric{}
|
||||
var GetHardCodedMetricsByNamespace = func(namespace string) ([]resources.Metric, error) {
|
||||
response := []resources.Metric{}
|
||||
exists := false
|
||||
var metrics []string
|
||||
if metrics, exists = constants.NamespaceMetricsMap[namespace]; !exists {
|
||||
@@ -25,17 +25,17 @@ var GetHardCodedMetricsByNamespace = func(namespace string) ([]models.Metric, er
|
||||
}
|
||||
|
||||
for _, metric := range metrics {
|
||||
response = append(response, models.Metric{Namespace: namespace, Name: metric})
|
||||
response = append(response, resources.Metric{Namespace: namespace, Name: metric})
|
||||
}
|
||||
|
||||
return response, nil
|
||||
}
|
||||
|
||||
var GetAllHardCodedMetrics = func() []models.Metric {
|
||||
response := []models.Metric{}
|
||||
var GetAllHardCodedMetrics = func() []resources.Metric {
|
||||
response := []resources.Metric{}
|
||||
for namespace, metrics := range constants.NamespaceMetricsMap {
|
||||
for _, metric := range metrics {
|
||||
response = append(response, models.Metric{Namespace: namespace, Name: metric})
|
||||
response = append(response, resources.Metric{Namespace: namespace, Name: metric})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ package services
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/grafana/grafana/pkg/tsdb/cloudwatch/models"
|
||||
"github.com/grafana/grafana/pkg/tsdb/cloudwatch/models/resources"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
@@ -34,6 +34,6 @@ func TestHardcodedMetrics_GetHardCodedMetricsByNamespace(t *testing.T) {
|
||||
t.Run("Should return metrics if namespace exist", func(t *testing.T) {
|
||||
resp, err := GetHardCodedMetricsByNamespace("AWS/IoTAnalytics")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, []models.Metric{{Name: "ActionExecution", Namespace: "AWS/IoTAnalytics"}, {Name: "ActivityExecutionError", Namespace: "AWS/IoTAnalytics"}, {Name: "IncomingMessages", Namespace: "AWS/IoTAnalytics"}}, resp)
|
||||
assert.Equal(t, []resources.Metric{{Name: "ActionExecution", Namespace: "AWS/IoTAnalytics"}, {Name: "ActivityExecutionError", Namespace: "AWS/IoTAnalytics"}, {Name: "IncomingMessages", Namespace: "AWS/IoTAnalytics"}}, resp)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/service/cloudwatch"
|
||||
"github.com/grafana/grafana/pkg/tsdb/cloudwatch/models"
|
||||
"github.com/grafana/grafana/pkg/tsdb/cloudwatch/models/request"
|
||||
"github.com/grafana/grafana/pkg/tsdb/cloudwatch/models/resources"
|
||||
)
|
||||
|
||||
type ListMetricsService struct {
|
||||
@@ -18,7 +18,7 @@ func NewListMetricsService(metricsClient models.MetricsClientProvider) models.Li
|
||||
return &ListMetricsService{metricsClient}
|
||||
}
|
||||
|
||||
func (l *ListMetricsService) GetDimensionKeysByDimensionFilter(r *request.DimensionKeysRequest) ([]string, error) {
|
||||
func (l *ListMetricsService) GetDimensionKeysByDimensionFilter(r resources.DimensionKeysRequest) ([]string, error) {
|
||||
input := &cloudwatch.ListMetricsInput{}
|
||||
if r.Namespace != "" {
|
||||
input.Namespace = aws.String(r.Namespace)
|
||||
@@ -63,7 +63,7 @@ func (l *ListMetricsService) GetDimensionKeysByDimensionFilter(r *request.Dimens
|
||||
return dimensionKeys, nil
|
||||
}
|
||||
|
||||
func (l *ListMetricsService) GetDimensionValuesByDimensionFilter(r *request.DimensionValuesRequest) ([]string, error) {
|
||||
func (l *ListMetricsService) GetDimensionValuesByDimensionFilter(r resources.DimensionValuesRequest) ([]string, error) {
|
||||
input := &cloudwatch.ListMetricsInput{
|
||||
Namespace: aws.String(r.Namespace),
|
||||
MetricName: aws.String(r.MetricName),
|
||||
@@ -116,26 +116,26 @@ func (l *ListMetricsService) GetDimensionKeysByNamespace(namespace string) ([]st
|
||||
return dimensionKeys, nil
|
||||
}
|
||||
|
||||
func (l *ListMetricsService) GetMetricsByNamespace(namespace string) ([]models.Metric, error) {
|
||||
func (l *ListMetricsService) GetMetricsByNamespace(namespace string) ([]resources.Metric, error) {
|
||||
metrics, err := l.ListMetricsWithPageLimit(&cloudwatch.ListMetricsInput{Namespace: aws.String(namespace)})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
response := []models.Metric{}
|
||||
response := []resources.Metric{}
|
||||
dupCheck := make(map[string]struct{})
|
||||
for _, metric := range metrics {
|
||||
if _, exists := dupCheck[*metric.MetricName]; exists {
|
||||
continue
|
||||
}
|
||||
dupCheck[*metric.MetricName] = struct{}{}
|
||||
response = append(response, models.Metric{Name: *metric.MetricName, Namespace: *metric.Namespace})
|
||||
response = append(response, resources.Metric{Name: *metric.MetricName, Namespace: *metric.Namespace})
|
||||
}
|
||||
|
||||
return response, nil
|
||||
}
|
||||
|
||||
func setDimensionFilter(input *cloudwatch.ListMetricsInput, dimensionFilter []*request.Dimension) {
|
||||
func setDimensionFilter(input *cloudwatch.ListMetricsInput, dimensionFilter []*resources.Dimension) {
|
||||
for _, dimension := range dimensionFilter {
|
||||
df := &cloudwatch.DimensionFilter{
|
||||
Name: aws.String(dimension.Name),
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/service/cloudwatch"
|
||||
"github.com/grafana/grafana/pkg/tsdb/cloudwatch/mocks"
|
||||
"github.com/grafana/grafana/pkg/tsdb/cloudwatch/models/request"
|
||||
"github.com/grafana/grafana/pkg/tsdb/cloudwatch/models/resources"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
"github.com/stretchr/testify/require"
|
||||
@@ -47,11 +47,11 @@ func TestListMetricsService_GetDimensionKeysByDimensionFilter(t *testing.T) {
|
||||
fakeMetricsClient.On("ListMetricsWithPageLimit", mock.Anything).Return(metricResponse, nil)
|
||||
listMetricsService := NewListMetricsService(fakeMetricsClient)
|
||||
|
||||
resp, err := listMetricsService.GetDimensionKeysByDimensionFilter(&request.DimensionKeysRequest{
|
||||
ResourceRequest: &request.ResourceRequest{Region: "us-east-1"},
|
||||
resp, err := listMetricsService.GetDimensionKeysByDimensionFilter(resources.DimensionKeysRequest{
|
||||
ResourceRequest: &resources.ResourceRequest{Region: "us-east-1"},
|
||||
Namespace: "AWS/EC2",
|
||||
MetricName: "CPUUtilization",
|
||||
DimensionFilter: []*request.Dimension{
|
||||
DimensionFilter: []*resources.Dimension{
|
||||
{Name: "InstanceId", Value: ""},
|
||||
},
|
||||
})
|
||||
@@ -80,12 +80,12 @@ func TestListMetricsService_GetDimensionValuesByDimensionFilter(t *testing.T) {
|
||||
fakeMetricsClient.On("ListMetricsWithPageLimit", mock.Anything).Return(metricResponse, nil)
|
||||
listMetricsService := NewListMetricsService(fakeMetricsClient)
|
||||
|
||||
resp, err := listMetricsService.GetDimensionValuesByDimensionFilter(&request.DimensionValuesRequest{
|
||||
ResourceRequest: &request.ResourceRequest{Region: "us-east-1"},
|
||||
resp, err := listMetricsService.GetDimensionValuesByDimensionFilter(resources.DimensionValuesRequest{
|
||||
ResourceRequest: &resources.ResourceRequest{Region: "us-east-1"},
|
||||
Namespace: "AWS/EC2",
|
||||
MetricName: "CPUUtilization",
|
||||
DimensionKey: "InstanceId",
|
||||
DimensionFilter: []*request.Dimension{
|
||||
DimensionFilter: []*resources.Dimension{
|
||||
{Name: "InstanceId", Value: ""},
|
||||
},
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user