-
Notifications
You must be signed in to change notification settings - Fork 50
/
client.go
74 lines (56 loc) · 2.18 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
74
package ecs
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/ecs"
"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 {
DescribeContainerInstances(ctx context.Context, params *ecs.DescribeContainerInstancesInput, optFns ...func(*ecs.Options)) (*ecs.DescribeContainerInstancesOutput, error)
DescribeTasks(ctx context.Context, params *ecs.DescribeTasksInput, optFns ...func(*ecs.Options)) (*ecs.DescribeTasksOutput, 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) (*ecs.Client, error) {
return appctx.Provide(ctx, clientAppCtxKey(name), func() (*ecs.Client, error) {
return NewClient(ctx, config, logger, name, optFns...)
})
}
func NewClient(ctx context.Context, config cfg.Config, logger log.Logger, name string, optFns ...ClientOption) (*ecs.Client, error) {
clientCfg := &ClientConfig{}
gosoAws.UnmarshalClientSettings(config, &clientCfg.Settings, "ecs", 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 := ecs.NewFromConfig(awsConfig)
gosoAws.LogNewClientCreated(ctx, logger, "ecs", name, clientCfg.Settings.ClientSettings)
return client, nil
}