[v9.3.x] CloudWatch: Cross-account querying support (#59389)
* add cross-account querying support * fix tests
This commit is contained in:
@@ -3,11 +3,11 @@ package cloudwatch
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
||||
"github.com/grafana/grafana/pkg/tsdb/cloudwatch/models"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestMetricDataQueryBuilder(t *testing.T) {
|
||||
@@ -24,6 +24,27 @@ func TestMetricDataQueryBuilder(t *testing.T) {
|
||||
assert.Equal(t, query.Namespace, *mdq.MetricStat.Metric.Namespace)
|
||||
})
|
||||
|
||||
t.Run("should pass AccountId in metric stat query", func(t *testing.T) {
|
||||
executor := newExecutor(nil, newTestConfig(), &fakeSessionCache{}, featuremgmt.WithFeatures())
|
||||
query := getBaseQuery()
|
||||
query.MetricEditorMode = models.MetricEditorModeBuilder
|
||||
query.MetricQueryType = models.MetricQueryTypeSearch
|
||||
query.AccountId = aws.String("some account id")
|
||||
mdq, err := executor.buildMetricDataQuery(logger, query)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "some account id", *mdq.AccountId)
|
||||
})
|
||||
|
||||
t.Run("should leave AccountId in metric stat query", func(t *testing.T) {
|
||||
executor := newExecutor(nil, newTestConfig(), &fakeSessionCache{}, featuremgmt.WithFeatures())
|
||||
query := getBaseQuery()
|
||||
query.MetricEditorMode = models.MetricEditorModeBuilder
|
||||
query.MetricQueryType = models.MetricQueryTypeSearch
|
||||
mdq, err := executor.buildMetricDataQuery(logger, query)
|
||||
require.NoError(t, err)
|
||||
assert.Nil(t, mdq.AccountId)
|
||||
})
|
||||
|
||||
t.Run("should use custom built expression", func(t *testing.T) {
|
||||
executor := newExecutor(nil, newTestConfig(), &fakeSessionCache{}, featuremgmt.WithFeatures())
|
||||
query := getBaseQuery()
|
||||
@@ -112,6 +133,42 @@ func TestMetricDataQueryBuilder(t *testing.T) {
|
||||
assert.Nil(t, mdq.Label)
|
||||
})
|
||||
}
|
||||
|
||||
t.Run(`should not specify accountId when it is "all"`, func(t *testing.T) {
|
||||
executor := newExecutor(nil, newTestConfig(), &fakeSessionCache{}, featuremgmt.WithFeatures(featuremgmt.FlagCloudWatchDynamicLabels))
|
||||
query := &models.CloudWatchQuery{
|
||||
Namespace: "AWS/EC2",
|
||||
MetricName: "CPUUtilization",
|
||||
Statistic: "Average",
|
||||
Period: 60,
|
||||
MatchExact: false,
|
||||
AccountId: aws.String("all"),
|
||||
}
|
||||
|
||||
mdq, err := executor.buildMetricDataQuery(logger, query)
|
||||
|
||||
assert.NoError(t, err)
|
||||
require.Nil(t, mdq.MetricStat)
|
||||
assert.Equal(t, `REMOVE_EMPTY(SEARCH('Namespace="AWS/EC2" MetricName="CPUUtilization"', 'Average', 60))`, *mdq.Expression)
|
||||
})
|
||||
|
||||
t.Run("should set accountId when it is specified", func(t *testing.T) {
|
||||
executor := newExecutor(nil, newTestConfig(), &fakeSessionCache{}, featuremgmt.WithFeatures(featuremgmt.FlagCloudWatchDynamicLabels))
|
||||
query := &models.CloudWatchQuery{
|
||||
Namespace: "AWS/EC2",
|
||||
MetricName: "CPUUtilization",
|
||||
Statistic: "Average",
|
||||
Period: 60,
|
||||
MatchExact: false,
|
||||
AccountId: aws.String("12345"),
|
||||
}
|
||||
|
||||
mdq, err := executor.buildMetricDataQuery(logger, query)
|
||||
|
||||
assert.NoError(t, err)
|
||||
require.Nil(t, mdq.MetricStat)
|
||||
assert.Equal(t, `REMOVE_EMPTY(SEARCH('Namespace="AWS/EC2" MetricName="CPUUtilization" :aws.AccountId="12345"', 'Average', 60))`, *mdq.Expression)
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("Query should be matched exact", func(t *testing.T) {
|
||||
@@ -199,6 +256,24 @@ func TestMetricDataQueryBuilder(t *testing.T) {
|
||||
assert.Equal(t, `REMOVE_EMPTY(SEARCH('{"AWS/EC2","InstanceId","LoadBalancer"} MetricName="CPUUtilization" "LoadBalancer"=("lb1" OR "lb2" OR "lb3")', 'Average', 300))`, res)
|
||||
})
|
||||
|
||||
t.Run("Query has multiple dimensions and an account Id", func(t *testing.T) {
|
||||
query := &models.CloudWatchQuery{
|
||||
Namespace: "AWS/EC2",
|
||||
MetricName: "CPUUtilization",
|
||||
Dimensions: map[string][]string{
|
||||
"LoadBalancer": {"lb1", "lb2", "lb3"},
|
||||
"InstanceId": {"i-123", "*", "i-789"},
|
||||
},
|
||||
Period: 300,
|
||||
Expression: "",
|
||||
MatchExact: matchExact,
|
||||
AccountId: aws.String("some account id"),
|
||||
}
|
||||
|
||||
res := buildSearchExpression(query, "Average")
|
||||
assert.Equal(t, `REMOVE_EMPTY(SEARCH('{"AWS/EC2","InstanceId","LoadBalancer"} MetricName="CPUUtilization" "LoadBalancer"=("lb1" OR "lb2" OR "lb3") :aws.AccountId="some account id"', 'Average', 300))`, res)
|
||||
})
|
||||
|
||||
t.Run("Query has a dimension key with a space", func(t *testing.T) {
|
||||
query := &models.CloudWatchQuery{
|
||||
Namespace: "AWS/Kafka",
|
||||
@@ -301,6 +376,24 @@ func TestMetricDataQueryBuilder(t *testing.T) {
|
||||
res := buildSearchExpression(query, "Average")
|
||||
assert.Equal(t, `REMOVE_EMPTY(SEARCH('Namespace="AWS/EC2" MetricName="CPUUtilization" "LoadBalancer"=("lb1" OR "lb2" OR "lb3") "InstanceId"', 'Average', 300))`, res)
|
||||
})
|
||||
|
||||
t.Run("query has multiple dimensions and an account Id", func(t *testing.T) {
|
||||
query := &models.CloudWatchQuery{
|
||||
Namespace: "AWS/EC2",
|
||||
MetricName: "CPUUtilization",
|
||||
Dimensions: map[string][]string{
|
||||
"LoadBalancer": {"lb1", "lb2", "lb3"},
|
||||
"InstanceId": {"i-123", "*", "i-789"},
|
||||
},
|
||||
Period: 300,
|
||||
Expression: "",
|
||||
MatchExact: matchExact,
|
||||
AccountId: aws.String("some account id"),
|
||||
}
|
||||
|
||||
res := buildSearchExpression(query, "Average")
|
||||
assert.Equal(t, `REMOVE_EMPTY(SEARCH('Namespace="AWS/EC2" MetricName="CPUUtilization" "LoadBalancer"=("lb1" OR "lb2" OR "lb3") "InstanceId" :aws.AccountId="some account id"', 'Average', 300))`, res)
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("Query has invalid characters in dimension values", func(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user