-
Notifications
You must be signed in to change notification settings - Fork 4
/
sqs.go
57 lines (46 loc) · 1.3 KB
/
sqs.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
package sqs
import (
"context"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/sqs"
"github.com/mitchellh/mapstructure"
"go.k6.io/k6/js/modules"
"os"
)
func init() {
modules.Register("k6/x/sqs", new(Sqs))
}
type Sqs struct{}
func (*Sqs) NewClient() *sqs.Client {
cfg, err := getAwsConfig()
if err != nil {
panic("configuration error, " + err.Error())
}
client := sqs.NewFromConfig(cfg)
return client
}
func getAwsConfig() (aws.Config, error) {
awsEndpoint := os.Getenv("AWS_ENDPOINT")
if awsEndpoint != "" {
customResolver := aws.EndpointResolverFunc(func(service, region string) (aws.Endpoint, error) {
return aws.Endpoint{
PartitionID: "aws",
URL: awsEndpoint,
SigningRegion: "eu-west-1",
}, nil
})
cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithEndpointResolver(customResolver))
return cfg, err
}
cfg, err := config.LoadDefaultConfig(context.TODO())
return cfg, err
}
func (s *Sqs) Send(sqsClient *sqs.Client, messageInput interface{}) {
var sqsMessageInput sqs.SendMessageInput
_ = mapstructure.Decode(messageInput, &sqsMessageInput)
_, err := sqsClient.SendMessage(context.TODO(), &sqsMessageInput)
if err != nil {
panic("unable to send message, " + err.Error())
}
}