Skip to content

Commit

Permalink
Parameterize client initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
joanlopez committed May 31, 2024
1 parent 0390c46 commit bcd427c
Show file tree
Hide file tree
Showing 13 changed files with 619 additions and 889 deletions.
98 changes: 89 additions & 9 deletions aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,103 @@ package aws

import (
"context"
"fmt"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/dop251/goja"
"go.k6.io/k6/js/common"
)

func defaultConfig(ctx context.Context) (cfg aws.Config, err error) {
return config.LoadDefaultConfig(ctx,
config.WithRegion("us-east-1"),
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider("test", "test", "")),
config.WithEndpointResolverWithOptions(aws.EndpointResolverWithOptionsFunc(
type Config struct {
Region string

AccessKeyID string
SecretAccessKey string
SessionToken string

Endpoint Endpoint
}

type Endpoint struct {
URL string
SigningRegion string
}

func (a *AWS) newConfig(call goja.ConstructorCall) *goja.Object {
if len(call.Arguments) == 0 {
return a.vu.Runtime().ToValue(&Config{}).ToObject(a.vu.Runtime())
}

if len(call.Arguments) != 1 || !isObject(call.Arguments[0]) {
panic(a.vu.Runtime().NewTypeError("AWSConfig constructor expects exactly one object argument"))
}

var (
cfg Config
obj = call.Arguments[0].ToObject(a.vu.Runtime())
)
if err := a.vu.Runtime().ExportTo(obj, &cfg); err != nil {
panic(a.vu.Runtime().NewTypeError(err.Error()))
}

return a.vu.Runtime().ToValue(cfg).ToObject(a.vu.Runtime())
}

func (a *AWS) constructorCallToConfig(id string, call goja.ConstructorCall) aws.Config {
rt := a.vu.Runtime()

// If no arguments are passed, we'll use the default config.
if len(call.Arguments) == 0 {
awsCfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
common.Throw(rt, err)
}
return awsCfg
}

// If more than one argument is passed, we'll throw an error.
if len(call.Arguments) != 1 {
panic(a.vu.Runtime().NewTypeError(fmt.Sprintf("%s constructor expects exactly one argument", id)))
}

// If the first argument is not a [Config], we'll throw an error.
cfg, ok := call.Arguments[0].Export().(Config)
if !ok {
panic(a.vu.Runtime().NewTypeError(fmt.Sprintf("%s constructor first argument must be an AWSConfig", id)))
}

optFns := make([]func(*config.LoadOptions) error, 0)

// If region is set, we'll use it.
if len(cfg.Region) > 0 {
optFns = append(optFns, config.WithRegion(cfg.Region))
}

// If any credential field is set, we'll use it.
if len(cfg.AccessKeyID) > 0 || len(cfg.SecretAccessKey) > 0 || len(cfg.SessionToken) > 0 {
optFns = append(optFns, config.WithCredentialsProvider(
credentials.NewStaticCredentialsProvider(cfg.AccessKeyID, cfg.SecretAccessKey, cfg.SessionToken),
))
}

// If endpoint is set, we'll use it.
if len(cfg.Endpoint.URL) > 0 {
optFns = append(optFns, config.WithEndpointResolverWithOptions(aws.EndpointResolverWithOptionsFunc(
func(service, region string, options ...interface{}) (aws.Endpoint, error) {
return aws.Endpoint{
URL: "http://localhost:4566",
SigningRegion: "us-east-1",
URL: cfg.Endpoint.URL,
SigningRegion: cfg.Endpoint.SigningRegion,
}, nil
},
)),
)
)))
}

awsCfg, err := config.LoadDefaultConfig(context.TODO(), optFns...)
if err != nil {
common.Throw(rt, err)
}

return awsCfg
}
29 changes: 21 additions & 8 deletions eventbridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,37 @@ import (
"github.com/aws/aws-sdk-go-v2/service/eventbridge"
)
{{ range . }}
func (a *AWS) {{ .Name }}({{ .FunctionCall }}) goja.Value {
cfg, err := defaultConfig(context.TODO())
if err != nil {
panic(err)
type EventBridgeClient struct {
*AWS
sdk *eventbridge.Client
}
func (a *AWS) newEventBridgeClient(call goja.ConstructorCall) *goja.Object {
awsCfg := a.constructorCallToConfig("EventBridgeClient", call)
sdk := eventbridge.NewFromConfig(awsCfg)
client := &EventBridgeClient{
AWS: a,
sdk: sdk,
}
return a.vu.Runtime().ToValue(client).ToObject(a.vu.Runtime())
}
{{ range . }}
func (c *EventBridgeClient) {{ .Name }}({{ .FunctionCall }}) goja.Value {
in := &{{.InputType}}{}
if err := fromGojaObject(a.vu.Runtime(), obj, in); err != nil {
if err := fromGojaObject(c.vu.Runtime(), obj, in); err != nil {
panic(err)
}
out, err := eventbridge.NewFromConfig(cfg).{{ .InnerFunctionCall }}
out, err := c.sdk.{{ .InnerFunctionCall }}
if err != nil {
panic(err)
}
return a.vu.Runtime().ToValue(out)
return c.vu.Runtime().ToValue(out)
}
{{ end }}
`
Expand Down
Loading

0 comments on commit bcd427c

Please sign in to comment.