/
kinesis.go
77 lines (61 loc) · 2.32 KB
/
kinesis.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
package kinesis
import (
"context"
"errors"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/kinesis"
"github.com/aws/aws-sdk-go-v2/service/kinesis/types"
"github.com/justtrackio/gosoline/pkg/cfg"
"github.com/justtrackio/gosoline/pkg/dx"
"github.com/justtrackio/gosoline/pkg/log"
)
type StreamDescription struct {
StreamArn string
StreamName string
OpenShardCount int
}
// CreateKinesisStream ensures a kinesis stream exists if dx.auto_create is set to true.
func CreateKinesisStream(ctx context.Context, config cfg.Config, logger log.Logger, client Client, streamName string) (*StreamDescription, error) {
logger.Info("looking for kinesis stream: %s", streamName)
summary, err := client.DescribeStreamSummary(ctx, &kinesis.DescribeStreamSummaryInput{
StreamName: aws.String(streamName),
})
if err == nil {
logger.Info("found kinesis stream: %s", streamName)
streamDescription := StreamDescription{
StreamArn: *summary.StreamDescriptionSummary.StreamARN,
StreamName: *summary.StreamDescriptionSummary.StreamName,
OpenShardCount: int(*summary.StreamDescriptionSummary.OpenShardCount),
}
return &streamDescription, nil
}
var errResourceNotFoundException *types.ResourceNotFoundException
if !errors.As(err, &errResourceNotFoundException) && err != nil {
return nil, fmt.Errorf("failed to describe kinesis streams: %w", err)
}
if !dx.ShouldAutoCreate(config) {
return nil, fmt.Errorf("kinesis stream %s does not exist and auto create is disabled", streamName)
}
logger.Info("trying to create kinesis stream: %s", streamName)
_, err = client.CreateStream(ctx, &kinesis.CreateStreamInput{
ShardCount: aws.Int32(1),
StreamName: aws.String(streamName),
})
if err != nil {
return nil, fmt.Errorf("failed to create kinesis stream %s: %w", streamName, err)
}
logger.Info("created kinesis stream: %s", streamName)
summary, err = client.DescribeStreamSummary(ctx, &kinesis.DescribeStreamSummaryInput{
StreamName: aws.String(streamName),
})
if err == nil {
streamDescription := StreamDescription{
StreamArn: *summary.StreamDescriptionSummary.StreamARN,
StreamName: *summary.StreamDescriptionSummary.StreamName,
OpenShardCount: int(*summary.StreamDescriptionSummary.OpenShardCount),
}
return &streamDescription, nil
}
return nil, err
}