mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
CloudWatch: Make deeplinks work for us-gov and china regions (#64080)
This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws/endpoints"
|
||||
"github.com/google/uuid"
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||
|
||||
@@ -41,7 +42,12 @@ const (
|
||||
GMDApiModeSQLExpression
|
||||
)
|
||||
|
||||
const defaultRegion = "default"
|
||||
const (
|
||||
defaultRegion = "default"
|
||||
defaultConsoleURL = "console.aws.amazon.com"
|
||||
usGovConsoleURL = "console.amazonaws-us-gov.com"
|
||||
chinaConsoleURL = "console.amazonaws.cn"
|
||||
)
|
||||
|
||||
type CloudWatchQuery struct {
|
||||
logger log.Logger
|
||||
@@ -187,7 +193,7 @@ func (q *CloudWatchQuery) BuildDeepLink(startTime time.Time, endTime time.Time,
|
||||
return "", fmt.Errorf("could not marshal link: %w", err)
|
||||
}
|
||||
|
||||
url, err := url.Parse(fmt.Sprintf(`https://%s.console.aws.amazon.com/cloudwatch/deeplink.js`, q.Region))
|
||||
url, err := url.Parse(fmt.Sprintf(`https://%s/cloudwatch/deeplink.js`, getEndpoint(q.Region)))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("unable to parse CloudWatch console deep link")
|
||||
}
|
||||
@@ -485,3 +491,15 @@ func sortDimensions(dimensions map[string][]string) map[string][]string {
|
||||
}
|
||||
return sortedDimensions
|
||||
}
|
||||
|
||||
func getEndpoint(region string) string {
|
||||
partition, _ := endpoints.PartitionForRegion(endpoints.DefaultPartitions(), region)
|
||||
url := defaultConsoleURL
|
||||
if partition.ID() == endpoints.AwsUsGovPartitionID {
|
||||
url = usGovConsoleURL
|
||||
}
|
||||
if partition.ID() == endpoints.AwsCnPartitionID {
|
||||
url = chinaConsoleURL
|
||||
}
|
||||
return fmt.Sprintf("%s.%s", region, url)
|
||||
}
|
||||
|
||||
@@ -1300,3 +1300,20 @@ func Test_ParseMetricDataQueries_ApplyMacros(t *testing.T) {
|
||||
assert.Equal(t, "SEARCH('{AWS/EC2,InstanceId}', 'Average', $__period_auto)", actual[0].Expression)
|
||||
})
|
||||
}
|
||||
|
||||
func TestGetEndpoint(t *testing.T) {
|
||||
testcases := []struct {
|
||||
region string
|
||||
expectedEndpoint string
|
||||
}{
|
||||
{"us-east-1", "us-east-1.console.aws.amazon.com"},
|
||||
{"us-gov-east-1", "us-gov-east-1.console.amazonaws-us-gov.com"},
|
||||
{"cn-northwest-1", "cn-northwest-1.console.amazonaws.cn"},
|
||||
}
|
||||
for _, ts := range testcases {
|
||||
t.Run(fmt.Sprintf("should create correct endpoint for %s", ts), func(t *testing.T) {
|
||||
actual := getEndpoint(ts.region)
|
||||
assert.Equal(t, ts.expectedEndpoint, actual)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user