forked from aws-samples/aurora-dsql-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_util.go
45 lines (34 loc) · 932 Bytes
/
client_util.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
package main
import (
"context"
"errors"
"github.com/aws/aws-sdk-go-v2/config"
dsql "github.com/aws/aws-sdk-go-v2/service/dsql"
)
type ClientUtil struct {
clients map[string]*dsql.Client
}
func (clientUtil *ClientUtil) setRegion(region string) func(*dsql.Options) {
return func(options *dsql.Options) {
options.Region = region
}
}
func (clientUtil *ClientUtil) GetInstance(region string) (client *dsql.Client, err error) {
if clientUtil.clients == nil {
clientUtil.clients = map[string]*dsql.Client{}
}
_, isExists := clientUtil.clients[region]
if !isExists {
cfg, err := config.LoadDefaultConfig(context.Background())
if err != nil {
return nil, err
}
newClient := dsql.NewFromConfig(cfg, clientUtil.setRegion(region))
if newClient == nil {
return nil, errors.New("failed to get a new client")
}
clientUtil.clients[region] = newClient
}
client = clientUtil.clients[region]
return
}