-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaws.go
104 lines (84 loc) · 2.72 KB
/
aws.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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/grafana/sobek"
"go.k6.io/k6/js/common"
)
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 sobek.ConstructorCall) *sobek.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 sobek.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: cfg.Endpoint.URL,
SigningRegion: cfg.Endpoint.SigningRegion,
}, nil
},
)))
}
awsCfg, err := config.LoadDefaultConfig(context.TODO(), optFns...)
if err != nil {
common.Throw(rt, err)
}
return awsCfg
}