-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
74 lines (63 loc) · 1.54 KB
/
session.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 awseph
import (
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/sqs"
awsephservice "github.com/hixi-hyi/aws-ephemeral-service-go/awseph/service"
)
type Session struct {
AwsSession *session.Session
Defers []func() error
}
func NewSession(sess *session.Session) *Session {
return &Session{
AwsSession: sess,
}
}
func NewScopedSession(sess *session.Session) (*Session, func()) {
m := NewSession(sess)
return m, m.Defer()
}
func (m *Session) Defer() func() {
return func() {
m.Teardown()
}
}
func (m *Session) Teardown() {
var errs []error
for _, f := range m.Defers {
err := f()
if err != nil {
errs = append(errs, err)
}
}
if len(errs) != 0 {
var msg string
for _, e := range errs {
msg += e.Error() + "\n"
}
panic(msg)
}
}
func (m *Session) AddService(i interface{}, f func() error, err error) interface{} {
if err != nil {
panic(err)
}
m.Defers = append(m.Defers, f)
return i
}
func (m *Session) S3BucketMustCreate(name string) *s3.CreateBucketOutput {
ret, f, err := awsephservice.S3BucketCreate(m.AwsSession, name)
m.AddService(nil, f, err)
return ret
}
func (m *Session) SQSQueueMustCreate(name string) *sqs.CreateQueueOutput {
ret, f, err := awsephservice.SQSQueueCreate(m.AwsSession, name)
m.AddService(nil, f, err)
return ret
}
func (m *Session) DynamoDBTableMustCreateByDynamo(name string, from interface{}) {
_, f, err := awsephservice.DynamoDBTableCreateByDynamo(m.AwsSession, name, from)
m.AddService(nil, f, err)
return
}