-
-
Notifications
You must be signed in to change notification settings - Fork 354
/
cloudtrail.go
72 lines (58 loc) · 1.75 KB
/
cloudtrail.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
package resources
import (
"context"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudtrail"
"github.com/gruntwork-io/cloud-nuke/config"
"github.com/gruntwork-io/cloud-nuke/logging"
"github.com/gruntwork-io/cloud-nuke/report"
"github.com/gruntwork-io/go-commons/errors"
)
func (ct *CloudtrailTrail) getAll(c context.Context, configObj config.Config) ([]*string, error) {
param := &cloudtrail.ListTrailsInput{}
trailIds := []*string{}
paginator := func(output *cloudtrail.ListTrailsOutput, lastPage bool) bool {
for _, trailInfo := range output.Trails {
if configObj.CloudtrailTrail.ShouldInclude(config.ResourceValue{
Name: trailInfo.Name,
}) {
trailIds = append(trailIds, trailInfo.TrailARN)
}
}
return !lastPage
}
err := ct.Client.ListTrailsPages(param, paginator)
if err != nil {
return trailIds, errors.WithStackTrace(err)
}
return trailIds, nil
}
func (ct *CloudtrailTrail) nukeAll(arns []*string) error {
if len(arns) == 0 {
logging.Debugf("No Cloudtrail Trails to nuke in region %s", ct.Region)
return nil
}
logging.Debugf("Deleting all Cloudtrail Trails in region %s", ct.Region)
var deletedArns []*string
for _, arn := range arns {
params := &cloudtrail.DeleteTrailInput{
Name: arn,
}
_, err := ct.Client.DeleteTrail(params)
// Record status of this resource
e := report.Entry{
Identifier: aws.StringValue(arn),
ResourceType: "Cloudtrail Trail",
Error: err,
}
report.Record(e)
if err != nil {
logging.Debugf("[Failed] %s", err)
} else {
deletedArns = append(deletedArns, arn)
logging.Debugf("Deleted Cloudtrail Trail: %s", aws.StringValue(arn))
}
}
logging.Debugf("[OK] %d Cloudtrail Trail deleted in %s", len(deletedArns), ct.Region)
return nil
}