Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
fujiwara committed Aug 29, 2024
1 parent 1c03e1b commit 8ca36e6
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 63 deletions.
73 changes: 73 additions & 0 deletions caller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
},
}
}
25 changes: 25 additions & 0 deletions caller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package lambroll_test

import (
"context"
"testing"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/sts"
"github.com/fujiwara/lambroll"
)

func TestCallerIdentity(t *testing.T) {
c := lambroll.NewCallerIdentity(aws.Config{})
c.Resolver = func(_ context.Context) (*sts.GetCallerIdentityOutput, error) {
return &sts.GetCallerIdentityOutput{
Account: aws.String("123456789012"),
Arn: aws.String("arn:aws:iam::123456789012:user/test-user"),
UserId: aws.String("AIXXXXXXXXX"),
}, nil
}
ctx := context.Background()
if c.Account(ctx) != "123456789012" {
t.Errorf("unexpected account id: %s", c.Account(ctx))
}
}
1 change: 1 addition & 0 deletions export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var (
JSONStr = jsonStr
MarshalJSON = marshalJSON
NewFunctionFrom = newFunctionFrom
NewCallerIdentity = newCallerIdentity
)

type VersionsOutput = versionsOutput
Expand Down
69 changes: 6 additions & 63 deletions lambroll.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/fujiwara/ssm-lookup/ssm"
"github.com/fujiwara/tfstate-lookup/tfstate"
"github.com/google/go-jsonnet"
"github.com/google/go-jsonnet/ast"
"github.com/hashicorp/go-envparse"
"github.com/kayac/go-config"
"github.com/shogo82148/go-retry"
Expand Down Expand Up @@ -87,60 +86,6 @@ var (
CurrentAliasName = "current"
)

type CallerIdentity struct {
data map[string]any
Resolver func(ctx context.Context) (*sts.GetCallerIdentityOutput, error)
}

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
},
}
}

// App represents lambroll application
type App struct {
callerIdentity *CallerIdentity
Expand Down Expand Up @@ -245,7 +190,12 @@ func New(ctx context.Context, opt *Option) (*App, error) {
loader.Funcs(prefixedFuncs)
}

callerIdentity := newCallerIdentity(v2cfg)
nativeFuncs = append(nativeFuncs, callerIdentity.JsonnetNativeFuncs(ctx)...)
loader.Funcs(callerIdentity.FuncMap(ctx))

app := &App{
callerIdentity: callerIdentity,
profile: profile,
loader: loader,
awsConfig: v2cfg,
Expand All @@ -254,14 +204,7 @@ func New(ctx context.Context, opt *Option) (*App, error) {
nativeFuncs: nativeFuncs,
extStr: opt.ExtStr,
extCode: opt.ExtCode,
callerIdentity: &CallerIdentity{
Resolver: func(ctx context.Context) (*sts.GetCallerIdentityOutput, error) {
return sts.NewFromConfig(v2cfg).GetCallerIdentity(ctx, &sts.GetCallerIdentityInput{})
},
},
}
app.nativeFuncs = append(app.nativeFuncs, app.callerIdentity.JsonnetNativeFuncs(ctx)...)
app.loader.Funcs(app.callerIdentity.FuncMap(ctx))
}
return app, nil
}

Expand Down

0 comments on commit 8ca36e6

Please sign in to comment.