从 ARN 中提取资源 ID

huangapple go评论108阅读模式
英文:

Extract resource-id from ARN

问题

我在AWS文档中看到ARN格式如下:

  1. arn:partition:service:region:account-id:resource-id
  2. arn:partition:service:region:account-id:resource-type/resource-id
  3. arn:partition:service:region:account-id:resource-type:resource-id

我正在尝试从ARN中提取resource-id

以下代码可以工作,但不够优雅...

我正在寻找如何改进它:

  1. func GetResourceNameFromARN(roleARN string) string {
  2. if parsedARN, err := arn.Parse(roleARN); err == nil {
  3. return parsedARN.Resource
  4. }
  5. return ""
  6. }
  7. func extractResourceId(arn string) string {
  8. resource := GetResourceNameFromARN(arn)
  9. switch len(strings.Split(resource, "/")) {
  10. case 1:
  11. switch len(strings.Split(resource, ":")) {
  12. case 2:
  13. return strings.Split(resource, ":")[1]
  14. }
  15. case 2:
  16. return strings.Split(resource, "/")[1]
  17. }
  18. return resource
  19. }
英文:

I saw in AWS documentation that ARN formats are:

  1. arn:partition:service:region:account-id:resource-id
  2. arn:partition:service:region:account-id:resource-type/resource-id
  3. arn:partition:service:region:account-id:resource-type:resource-id

I'm trying to fetch the resource-id from the ARN.

The following code works, but ugly...

I'm searching for how to improve it:

  1. func GetResourceNameFromARN(roleARN string) string {
  2. if parsedARN, err := arn.Parse(roleARN); err == nil {
  3. return parsedARN.Resource
  4. }
  5. return ""
  6. }
  7. func extractResourceId(arn string) string {
  8. resource := GetResourceNameFromARN(arn)
  9. switch len(strings.Split(resource, "/")) {
  10. case 1:
  11. switch len(strings.Split(resource, ":")) {
  12. case 2:
  13. return strings.Split(resource, ":")[1]
  14. }
  15. case 2:
  16. return strings.Split(resource, "/")[1]
  17. }
  18. return resource
  19. }

答案1

得分: 2

我建议使用一个简单的正则表达式:

  1. package main
  2. import (
  3. "fmt"
  4. "regexp"
  5. )
  6. func main() {
  7. // 编译表达式,通常在初始化时进行
  8. // 使用原始字符串避免需要转义反斜杠
  9. var validID = regexp.MustCompile(`[^:/]*$`)
  10. fmt.Println(validID.FindString("arn:partition:service:region:account-id:resource-id"))
  11. fmt.Println(validID.FindString("arn:partition:service:region:account-id:resource-type/resource-id"))
  12. fmt.Println(validID.FindString("arn:partition:service:region:account-id:resource-type:resource-id"))
  13. }

在这里查看演示:链接

英文:

I would suggest a simple regular expression:

  1. package main
  2. import (
  3. "fmt"
  4. "regexp"
  5. )
  6. func main() {
  7. // Compile the expression once, usually at init time.
  8. // Use raw strings to avoid having to quote the backslashes.
  9. var validID = regexp.MustCompile(`[^:/]*$`)
  10. fmt.Println(validID.FindString("arn:partition:service:region:account-id:resource-id"))
  11. fmt.Println(validID.FindString("arn:partition:service:region:account-id:resource-type/resource-id"))
  12. fmt.Println(validID.FindString("arn:partition:service:region:account-id:resource-type:resource-id"))
  13. }

See the demo here

huangapple
  • 本文由 发表于 2022年2月17日 17:30:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/71155485.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定