-
Notifications
You must be signed in to change notification settings - Fork 50
/
client.go
73 lines (55 loc) · 2.03 KB
/
client.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
package rds
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/retry"
awsCfg "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/rds"
"github.com/justtrackio/gosoline/pkg/appctx"
"github.com/justtrackio/gosoline/pkg/cfg"
gosoAws "github.com/justtrackio/gosoline/pkg/cloud/aws"
"github.com/justtrackio/gosoline/pkg/log"
)
//go:generate mockery --name Client
type Client interface {
DescribeDBInstances(ctx context.Context, params *rds.DescribeDBInstancesInput, optFns ...func(*rds.Options)) (*rds.DescribeDBInstancesOutput, error)
}
type ClientSettings struct {
gosoAws.ClientSettings
}
type ClientConfig struct {
Settings ClientSettings
LoadOptions []func(options *awsCfg.LoadOptions) error
}
func (c ClientConfig) GetSettings() gosoAws.ClientSettings {
return c.Settings.ClientSettings
}
func (c ClientConfig) GetLoadOptions() []func(options *awsCfg.LoadOptions) error {
return c.LoadOptions
}
func (c ClientConfig) GetRetryOptions() []func(*retry.StandardOptions) {
return nil
}
type ClientOption func(cfg *ClientConfig)
type clientAppCtxKey string
func ProvideClient(ctx context.Context, config cfg.Config, logger log.Logger, name string, optFns ...ClientOption) (*rds.Client, error) {
return appctx.Provide(ctx, clientAppCtxKey(name), func() (*rds.Client, error) {
return NewClient(ctx, config, logger, name, optFns...)
})
}
func NewClient(ctx context.Context, config cfg.Config, logger log.Logger, name string, optFns ...ClientOption) (*rds.Client, error) {
clientCfg := &ClientConfig{}
gosoAws.UnmarshalClientSettings(config, &clientCfg.Settings, "rds", name)
for _, opt := range optFns {
opt(clientCfg)
}
var err error
var awsConfig aws.Config
if awsConfig, err = gosoAws.DefaultClientConfig(ctx, config, logger, clientCfg); err != nil {
return nil, fmt.Errorf("can not initialize config: %w", err)
}
client := rds.NewFromConfig(awsConfig)
gosoAws.LogNewClientCreated(ctx, logger, "rds", name, clientCfg.Settings.ClientSettings)
return client, nil
}