-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathclient.go
91 lines (75 loc) · 2.24 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package cloudtrail
import (
"github.com/aws/aws-sdk-go/aws/session"
SDK "github.com/aws/aws-sdk-go/service/cloudtrail"
"github.com/evalphobia/aws-sdk-go-wrapper/config"
"github.com/evalphobia/aws-sdk-go-wrapper/log"
)
const (
serviceName = "CloudTrail"
)
// CloudTrail has CloudTrail client.
type CloudTrail struct {
client *SDK.CloudTrail
logger log.Logger
}
// New returns initialized *CloudTrail.
func New(conf config.Config) (*CloudTrail, error) {
sess, err := conf.Session()
if err != nil {
return nil, err
}
return NewFromSession(sess), nil
}
// NewFromSession returns initialized *CloudTrail from aws.Session.
func NewFromSession(sess *session.Session) *CloudTrail {
return &CloudTrail{
client: SDK.New(sess),
logger: log.DefaultLogger,
}
}
// GetClient gets aws client.
func (svc *CloudTrail) GetClient() *SDK.CloudTrail {
return svc.client
}
// SetLogger sets logger.
func (svc *CloudTrail) SetLogger(logger log.Logger) {
svc.logger = logger
}
// LookupEventsAll executes LookupEvents operation and fetch all of events.
func (svc *CloudTrail) LookupEventsAll(input LookupEventsInput) (LookupEventsResult, error) {
in := input
result := LookupEventsResult{}
for {
res, err := svc.LookupEvents(in)
if err != nil {
return result, err
}
result.Events = append(result.Events, res.Events...)
if res.NextToken == "" {
return result, nil
}
in.NextToken = res.NextToken
}
}
// LookupEvents executes LookupEvents operation with customized input.
func (svc *CloudTrail) LookupEvents(input LookupEventsInput) (LookupEventsResult, error) {
return svc.DoLookupEvents(input.ToInput())
}
// DoLookupEvents executes LookupEvents operation.
func (svc *CloudTrail) DoLookupEvents(in *SDK.LookupEventsInput) (LookupEventsResult, error) {
out, err := svc.client.LookupEvents(in)
if err != nil {
svc.Errorf("error on `LookupEvents` operation; error=%s;", err.Error())
return LookupEventsResult{}, err
}
return NewLookupEventsResult(out), nil
}
// Infof logging information.
func (svc *CloudTrail) Infof(format string, v ...interface{}) {
svc.logger.Infof(serviceName, format, v...)
}
// Errorf logging error information.
func (svc *CloudTrail) Errorf(format string, v ...interface{}) {
svc.logger.Errorf(serviceName, format, v...)
}