CloudWatch: Make deeplinks work for us-gov and china regions (#64080)

This commit is contained in:
Isabella Siu 2023-03-06 16:00:59 -05:00 committed by GitHub
parent e7cd629e79
commit 4f3d64e8b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 74 additions and 6 deletions

View File

@ -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)
}

View File

@ -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)
})
}
}

View File

@ -0,0 +1,18 @@
import { getLogsEndpoint } from './aws_url';
describe('getEndpoint', () => {
it('should return the default url for normal regions', () => {
const result = getLogsEndpoint('us-east-1');
expect(result).toBe('us-east-1.console.aws.amazon.com');
});
it('should return the us-gov url for us-gov regions', () => {
const result = getLogsEndpoint('us-gov-east-1');
expect(result).toBe('us-gov-east-1.console.amazonaws-us-gov.com');
});
it('should return the china url for cn regions', () => {
const result = getLogsEndpoint('cn-northwest-1');
expect(result).toBe('cn-northwest-1.console.amazonaws.cn');
});
});

View File

@ -11,8 +11,23 @@ export interface AwsUrl {
source: string[];
}
export function encodeUrl(obj: AwsUrl, region: string): string {
return `https://${region}.console.aws.amazon.com/cloudwatch/home?region=${region}#logs-insights:queryDetail=${JSURL.stringify(
obj
)}`;
const defaultURL = 'console.aws.amazon.com';
const usGovURL = 'console.amazonaws-us-gov.com';
const chinaURL = 'console.amazonaws.cn';
export function getLogsEndpoint(region: string): string {
let url = defaultURL;
if (region.startsWith('us-gov-')) {
url = usGovURL;
}
if (region.startsWith('cn-')) {
url = chinaURL;
}
return `${region}.${url}`;
}
export function encodeUrl(obj: AwsUrl, region: string): string {
return `https://${getLogsEndpoint(
region
)}/cloudwatch/home?region=${region}#logs-insights:queryDetail=${JSURL.stringify(obj)}`;
}