-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathcaller.go
73 lines (65 loc) · 1.57 KB
/
caller.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package lambroll
import (
"context"
"text/template"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/sts"
"github.com/google/go-jsonnet"
"github.com/google/go-jsonnet/ast"
)
type CallerIdentity struct {
data map[string]any
Resolver func(ctx context.Context) (*sts.GetCallerIdentityOutput, error)
}
func newCallerIdentity(cfg aws.Config) *CallerIdentity {
return &CallerIdentity{
Resolver: func(ctx context.Context) (*sts.GetCallerIdentityOutput, error) {
return sts.NewFromConfig(cfg).GetCallerIdentity(ctx, &sts.GetCallerIdentityInput{})
},
}
}
func (c *CallerIdentity) resolve(ctx context.Context) error {
if c.data != nil {
return nil
}
res, err := c.Resolver(ctx)
if err != nil {
return err
}
c.data = map[string]any{
"Account": *res.Account,
"Arn": *res.Arn,
"UserId": *res.UserId,
}
return nil
}
func (c *CallerIdentity) Account(ctx context.Context) string {
if err := c.resolve(ctx); err != nil {
return ""
}
return c.data["Account"].(string)
}
func (c *CallerIdentity) JsonnetNativeFuncs(ctx context.Context) []*jsonnet.NativeFunction {
return []*jsonnet.NativeFunction{
{
Name: "caller_identity",
Params: []ast.Identifier{},
Func: func(params []any) (any, error) {
if err := c.resolve(ctx); err != nil {
return nil, err
}
return c.data, nil
},
},
}
}
func (c *CallerIdentity) FuncMap(ctx context.Context) template.FuncMap {
return template.FuncMap{
"caller_identity": func() map[string]any {
if err := c.resolve(ctx); err != nil {
return nil
}
return c.data
},
}
}