forked from gruntwork-io/terratest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudwatch.go
59 lines (49 loc) · 1.68 KB
/
cloudwatch.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
package aws
import (
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
)
// GetCloudWatchLogEntries returns the CloudWatch log messages in the given region for the given log stream and log group.
func GetCloudWatchLogEntries(t *testing.T, awsRegion string, logStreamName string, logGroupName string) []string {
out, err := GetCloudWatchLogEntriesE(t, awsRegion, logStreamName, logGroupName)
if err != nil {
t.Fatal(err)
}
return out
}
// GetCloudWatchLogEntriesE returns the CloudWatch log messages in the given region for the given log stream and log group.
func GetCloudWatchLogEntriesE(t *testing.T, awsRegion string, logStreamName string, logGroupName string) ([]string, error) {
client, err := NewCloudWatchLogsClientE(t, awsRegion)
if err != nil {
return nil, err
}
output, err := client.GetLogEvents(&cloudwatchlogs.GetLogEventsInput{
LogGroupName: aws.String(logGroupName),
LogStreamName: aws.String(logStreamName),
})
if err != nil {
return nil, err
}
entries := []string{}
for _, event := range output.Events {
entries = append(entries, *event.Message)
}
return entries, nil
}
// NewCloudWatchLogsClient creates a new CloudWatch Logs client.
func NewCloudWatchLogsClient(t *testing.T, region string) *cloudwatchlogs.CloudWatchLogs {
client, err := NewCloudWatchLogsClientE(t, region)
if err != nil {
t.Fatal(err)
}
return client
}
// NewCloudWatchLogsClientE creates a new CloudWatch Logs client.
func NewCloudWatchLogsClientE(t *testing.T, region string) (*cloudwatchlogs.CloudWatchLogs, error) {
sess, err := NewAuthenticatedSession(region)
if err != nil {
return nil, err
}
return cloudwatchlogs.New(sess), nil
}