forked from mantzas/patron
/
aws.go
93 lines (76 loc) · 2.65 KB
/
aws.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package aws
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/sns"
"github.com/aws/aws-sdk-go-v2/service/sqs"
)
type SQSAPI interface {
CreateQueue(ctx context.Context, params *sqs.CreateQueueInput, optFns ...func(*sqs.Options)) (*sqs.CreateQueueOutput, error)
ReceiveMessage(ctx context.Context, params *sqs.ReceiveMessageInput, optFns ...func(*sqs.Options)) (*sqs.ReceiveMessageOutput, error)
}
type SNSAPI interface {
CreateTopic(ctx context.Context, params *sns.CreateTopicInput, optFns ...func(*sns.Options)) (*sns.CreateTopicOutput, error)
}
// CreateSNSAPI helper function.
func CreateSNSAPI(region, endpoint string) (*sns.Client, error) {
cfg, err := createConfig(sns.ServiceID, region, endpoint)
if err != nil {
return nil, err
}
api := sns.NewFromConfig(cfg)
return api, nil
}
// CreateSNSTopic helper function.
func CreateSNSTopic(api SNSAPI, topic string) (string, error) {
out, err := api.CreateTopic(context.Background(), &sns.CreateTopicInput{
Name: aws.String(topic),
})
if err != nil {
return "", fmt.Errorf("failed to create topic %s: %w", topic, err)
}
return *out.TopicArn, nil
}
// CreateSQSAPI helper function.
func CreateSQSAPI(region, endpoint string) (*sqs.Client, error) {
cfg, err := createConfig(sqs.ServiceID, region, endpoint)
if err != nil {
return nil, err
}
api := sqs.NewFromConfig(cfg)
return api, nil
}
// CreateSQSQueue helper function.
func CreateSQSQueue(api SQSAPI, queueName string) (string, error) {
out, err := api.CreateQueue(context.Background(), &sqs.CreateQueueInput{
QueueName: aws.String(queueName),
})
if err != nil {
return "", err
}
return *out.QueueUrl, nil
}
func createConfig(awsServiceID, awsRegion, awsEndpoint string) (aws.Config, error) {
customResolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
if service == awsServiceID && region == awsRegion {
return aws.Endpoint{
URL: awsEndpoint,
SigningRegion: awsRegion,
}, nil
}
// returning EndpointNotFoundError will allow the service to fallback to it's default resolution
return aws.Endpoint{}, &aws.EndpointNotFoundError{}
})
cfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithRegion(awsRegion),
config.WithEndpointResolverWithOptions(customResolver),
config.WithCredentialsProvider(aws.NewCredentialsCache(credentials.NewStaticCredentialsProvider("test", "test", ""))),
)
if err != nil {
return aws.Config{}, fmt.Errorf("failed to create AWS config: %w", err)
}
return cfg, nil
}