-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharnparse.go
59 lines (51 loc) · 1.22 KB
/
arnparse.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package arnparse
import (
"errors"
"strings"
)
// ErrMalformedArn arn string is malformed
var ErrMalformedArn = errors.New("malformed amazon resource name")
// Arn represents an parsed arn string instance
type Arn struct {
Partition string
Service string
Region string
AccountID string
ResourceType string
Resource string
}
// Parse ARN
func Parse(arn string) (*Arn, error) {
if !strings.Contains(arn, "arn:") {
return nil, ErrMalformedArn
}
var (
arnObj = &Arn{}
elems = strings.SplitN(arn, ":", 6)
)
arnObj.Resource = elems[5]
arnObj.Service = elems[2]
in := func(target string, services []string) bool {
for _, srv := range services {
if target == srv {
return true
}
}
return false
}
if !in(arnObj.Service, []string{"s3", "sns", "apigateway", "execute-api"}) {
if strings.Contains(arnObj.Resource, "/") {
r := strings.SplitN(arnObj.Resource, "/", 2)
arnObj.ResourceType = r[0]
arnObj.Resource = r[1]
} else if strings.Contains(arnObj.Resource, ":") {
r := strings.SplitN(arnObj.Resource, ":", 2)
arnObj.ResourceType = r[0]
arnObj.Resource = r[1]
}
}
arnObj.Partition = elems[1]
arnObj.Region = elems[3]
arnObj.AccountID = elems[4]
return arnObj, nil
}