Skip to content

Commit 3668d15

Browse files
committed
Format extract cloudfront distribution id from arn
1 parent 1705743 commit 3668d15

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

format/aws.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import "regexp"
1212

1313
var arnEc2InstanceRe = regexp.MustCompile(`^arn:aws:ec2:.+/([^/]+)`)
1414
var arnEcsTaskToClusterRe = regexp.MustCompile(`^(arn:aws:ecs:[^:]+:\d+):task/([^/]+)/.*`)
15+
var arnCloudFrontDistributionRe = regexp.MustCompile(`^arn:aws:cloudfront::\d+:distribution/([^/]+)`)
1516

1617
func Ec2InstanceIdFromArn(arn string) string {
1718
return arnEc2InstanceRe.ReplaceAllString(arn, "${1}")
@@ -20,3 +21,7 @@ func Ec2InstanceIdFromArn(arn string) string {
2021
func EcsTaskArnToClusterArn(arn string) string {
2122
return arnEcsTaskToClusterRe.ReplaceAllString(arn, "${1}:cluster/${2}")
2223
}
24+
25+
func CloudFrontDistributionIdFromArn(arn string) string {
26+
return arnCloudFrontDistributionRe.ReplaceAllString(arn, "${1}")
27+
}

format/aws_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,14 @@ func TestEcsTaskArnToClusterArnErr(t *testing.T) {
2727
result := EcsTaskArnToClusterArn(arn)
2828
assert.Equal(t, arn, result)
2929
}
30+
31+
func TestCloudFrontDistributionIdFromArn(t *testing.T) {
32+
result := CloudFrontDistributionIdFromArn("arn:aws:cloudfront::1234567890:distribution/E1234567890ABC")
33+
assert.Equal(t, "E1234567890ABC", result)
34+
}
35+
36+
func TestCloudFrontDistributionIdFromArnErr(t *testing.T) {
37+
arn := "invalid-arn"
38+
result := CloudFrontDistributionIdFromArn(arn)
39+
assert.Equal(t, arn, result)
40+
}

0 commit comments

Comments
 (0)