-
Notifications
You must be signed in to change notification settings - Fork 16
/
resources.go
280 lines (220 loc) · 7.98 KB
/
resources.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package awstest
import (
"fmt"
"log"
"os"
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/kinesis"
"github.com/aws/aws-sdk-go/service/kms"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/socialpoint-labs/bsk/awsx"
"github.com/socialpoint-labs/bsk/logx"
"github.com/socialpoint-labs/bsk/uuid"
)
const (
fifoSuffix = ".fifo"
// SQSFifoServiceName is the service name for SQS FIFO queues
SQSFifoServiceName = sqs.ServiceName + fifoSuffix
)
// Supported resource types
type s3ResourceService struct{}
type sqsResourceService struct{}
type sqsFifoResourceService struct{}
type kmsResourceService struct{}
type dynamodbResourceService struct{}
type kinesisResourceService struct{}
// ResourceService isolates creation and checking existence by session for each supported resource
type ResourceService interface {
// CreateResourceForSession creates a resource for testing purposes
CreateResourceForSession(sess *session.Session, name *string) (*string, error)
// AssertResourceExistsForSession check if a resource exists for a given session
AssertResourceExistsForSession(sess *session.Session, name *string) (bool, error)
}
// Map for getting ResourceService by name
var serviceNameToResources = map[string]ResourceService{
s3.ServiceName: s3ResourceService{},
sqs.ServiceName: sqsResourceService{},
SQSFifoServiceName: sqsFifoResourceService{},
kms.ServiceName: kmsResourceService{},
dynamodb.ServiceName: dynamodbResourceService{},
kinesis.ServiceName: kinesisResourceService{},
}
// GetResourceServiceByName returns a ResourceService if it's supported
func GetResourceServiceByName(r string) ResourceService {
resource, ok := serviceNameToResources[r]
if !ok {
log.Panicf("Resource %q not supported", r)
}
return resource
}
// NewSession creates a new AWS session, suited for testing
func NewSession() *session.Session {
return NewSessionWithRegion("us-east-1")
}
// NewSessionWithRegion creates a new AWS session configured for specific region, suited for testing
func NewSessionWithRegion(region string) *session.Session {
cfg := aws.NewConfig().
WithRegion(region).
WithCredentialsChainVerboseErrors(true).
WithS3ForcePathStyle(true)
awsEndpoint := Endpoint()
if awsEndpoint != "" {
cfg.WithEndpoint(awsEndpoint)
}
awsSecretAccessKey := os.Getenv("AWS_SECRET_ACCESS_KEY")
awsAccessKeyID := os.Getenv("AWS_ACCESS_KEY_ID")
if awsSecretAccessKey != "" || awsAccessKeyID != "" {
cfg.WithCredentials(credentials.NewStaticCredentials(awsAccessKeyID, awsSecretAccessKey, ""))
}
return session.Must(session.NewSession(cfg))
}
func Endpoint() string {
return os.Getenv("SP_BSK_AWS_ENDPOINT")
}
// CreateResource creates an AWS resource for testing purposes
func CreateResource(r string) *string {
name := aws.String("integration-test-" + uuid.New())
resource := GetResourceServiceByName(r)
sess := NewSession()
name, err := resource.CreateResourceForSession(sess, name)
if err != nil {
log.Panicf("Error %v creating resource %q", err, r)
}
return name
}
// AssertResourceExists asserts if the resource with the given name exists
func AssertResourceExists(t *testing.T, name *string, r string) bool {
resource := GetResourceServiceByName(r)
sess := NewSession()
response, err := resource.AssertResourceExistsForSession(sess, name)
if err != nil {
t.Errorf("Error %v checking resource %v", err, r)
}
return response
}
func (s3ResourceService) CreateResourceForSession(sess *session.Session, name *string) (*string, error) {
svc := s3.New(sess)
input := &s3.CreateBucketInput{Bucket: name}
_, err := svc.CreateBucket(input)
return name, err
}
func (s3ResourceService) AssertResourceExistsForSession(sess *session.Session, name *string) (bool, error) {
svc := s3.New(sess)
input := &s3.HeadBucketInput{Bucket: name}
_, err := svc.HeadBucket(input)
return err == nil, err
}
func (sqsResourceService) CreateResourceForSession(sess *session.Session, name *string) (*string, error) {
svc := sqs.New(sess)
input := &sqs.CreateQueueInput{QueueName: name}
_, err := svc.CreateQueue(input)
return name, err
}
func (sqsResourceService) AssertResourceExistsForSession(sess *session.Session, name *string) (bool, error) {
svc := sqs.New(sess)
input := &sqs.GetQueueUrlInput{QueueName: name}
_, err := svc.GetQueueUrl(input)
return err == nil, err
}
func (sqsFifoResourceService) CreateResourceForSession(sess *session.Session, name *string) (*string, error) {
svc := sqs.New(sess)
// Add FIFO suffix
name = aws.String(fmt.Sprintf("%s%s", *name, fifoSuffix))
input := &sqs.CreateQueueInput{
QueueName: name,
Attributes: map[string]*string{
"FifoQueue": aws.String("true"),
"ContentBasedDeduplication": aws.String("true"),
},
}
_, err := svc.CreateQueue(input)
return name, err
}
func (sqsFifoResourceService) AssertResourceExistsForSession(sess *session.Session, name *string) (bool, error) {
svc := sqs.New(sess)
input := &sqs.GetQueueUrlInput{QueueName: name}
_, err := svc.GetQueueUrl(input)
return err == nil, err
}
func (kmsResourceService) CreateResourceForSession(sess *session.Session, name *string) (*string, error) {
svc := kms.New(sess)
input := &kms.CreateKeyInput{
Description: name,
KeyUsage: aws.String("ENCRYPT_DECRYPT"),
}
res, err := svc.CreateKey(input)
if err == nil {
_, err = svc.CreateAlias(&kms.CreateAliasInput{
TargetKeyId: res.KeyMetadata.KeyId,
AliasName: aws.String("alias/" + aws.StringValue(name)),
})
name = res.KeyMetadata.KeyId
}
return name, err
}
func (kmsResourceService) AssertResourceExistsForSession(sess *session.Session, name *string) (bool, error) {
svc := kms.New(NewSession())
input := &kms.DescribeKeyInput{KeyId: name}
_, err := svc.DescribeKey(input)
return err == nil, err
}
func (dynamodbResourceService) CreateResourceForSession(sess *session.Session, name *string) (*string, error) {
svc := dynamodb.New(sess)
input := &dynamodb.CreateTableInput{
AttributeDefinitions: []*dynamodb.AttributeDefinition{
{
AttributeName: aws.String("KeySchemaAttributeName"),
AttributeType: aws.String(dynamodb.ScalarAttributeTypeS),
},
},
KeySchema: []*dynamodb.KeySchemaElement{
{
AttributeName: aws.String("KeySchemaAttributeName"),
KeyType: aws.String(dynamodb.KeyTypeHash),
},
},
ProvisionedThroughput: &dynamodb.ProvisionedThroughput{
ReadCapacityUnits: aws.Int64(1),
WriteCapacityUnits: aws.Int64(1),
},
TableName: name,
}
_, err := svc.CreateTable(input)
return name, err
}
func (dynamodbResourceService) AssertResourceExistsForSession(sess *session.Session, name *string) (bool, error) {
svc := dynamodb.New(sess)
input := &dynamodb.DescribeTableInput{TableName: name}
_, err := svc.DescribeTable(input)
return err == nil, err
}
// CreateResourceForSession always reuses the same stream because creating one is very slow
func (kinesisResourceService) CreateResourceForSession(sess *session.Session, name *string) (*string, error) {
kin := kinesis.New(sess)
// fixed name because aws takes up to 17s to be able to provide the shard info about a new stream
createStreamInput := kinesis.CreateStreamInput{
StreamName: name,
ShardCount: aws.Int64(1)}
_, err := kin.CreateStream(&createStreamInput)
if awsx.IsErrorCode(err, kinesis.ErrCodeResourceInUseException) {
err = nil // already exists
} else {
logx.New().Info(fmt.Sprintf(
"Stream %s does not exist yet. It will take around 15s to create it ", aws.StringValue(name)))
if errWait := kin.WaitUntilStreamExists(&kinesis.DescribeStreamInput{StreamName: name}); errWait != nil {
panic(errWait)
}
}
return name, err
}
func (kinesisResourceService) AssertResourceExistsForSession(sess *session.Session, name *string) (bool, error) {
kin := kinesis.New(sess)
input := &kinesis.DescribeStreamInput{StreamName: name}
err := kin.WaitUntilStreamExists(input)
return err == nil, err
}