forked from gruntwork-io/cloud-nuke
/
asg.go
94 lines (81 loc) · 2.68 KB
/
asg.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
package resources
import (
"context"
"github.com/andrewderr/cloud-nuke-a1/config"
"github.com/andrewderr/cloud-nuke-a1/logging"
"github.com/andrewderr/cloud-nuke-a1/report"
"github.com/andrewderr/cloud-nuke-a1/telemetry"
"github.com/andrewderr/cloud-nuke-a1/util"
awsgo "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/gruntwork-io/go-commons/errors"
commonTelemetry "github.com/gruntwork-io/go-commons/telemetry"
)
// Returns a formatted string of ASG Names
func (ag *ASGroups) getAll(c context.Context, configObj config.Config) ([]*string, error) {
result, err := ag.Client.DescribeAutoScalingGroups(&autoscaling.DescribeAutoScalingGroupsInput{})
if err != nil {
return nil, errors.WithStackTrace(err)
}
var groupNames []*string
for _, group := range result.AutoScalingGroups {
if configObj.AutoScalingGroup.ShouldInclude(config.ResourceValue{
Time: group.CreatedTime,
Name: group.AutoScalingGroupName,
Tags: util.ConvertAutoScalingTagsToMap(group.Tags),
}) {
groupNames = append(groupNames, group.AutoScalingGroupName)
}
}
return groupNames, nil
}
// Deletes all Auto Scaling Groups
func (ag *ASGroups) nukeAll(groupNames []*string) error {
if len(groupNames) == 0 {
logging.Debugf("No Auto Scaling Groups to nuke in region %s", ag.Region)
return nil
}
logging.Debugf("Deleting all Auto Scaling Groups in region %s", ag.Region)
var deletedGroupNames []*string
for _, groupName := range groupNames {
params := &autoscaling.DeleteAutoScalingGroupInput{
AutoScalingGroupName: groupName,
ForceDelete: awsgo.Bool(true),
}
_, err := ag.Client.DeleteAutoScalingGroup(params)
// Record status of this resource
e := report.Entry{
Identifier: *groupName,
ResourceType: "Auto-Scaling Group",
Error: err,
}
report.Record(e)
if err != nil {
logging.Debugf("[Failed] %s", err)
telemetry.TrackEvent(commonTelemetry.EventContext{
EventName: "Error Nuking ASG",
}, map[string]interface{}{
"region": ag.Region,
})
} else {
deletedGroupNames = append(deletedGroupNames, groupName)
logging.Debugf("Deleted Auto Scaling Group: %s", *groupName)
}
}
if len(deletedGroupNames) > 0 {
err := ag.Client.WaitUntilGroupNotExists(&autoscaling.DescribeAutoScalingGroupsInput{
AutoScalingGroupNames: deletedGroupNames,
})
if err != nil {
logging.Errorf("[Failed] %s", err)
telemetry.TrackEvent(commonTelemetry.EventContext{
EventName: "Error Nuking ASG",
}, map[string]interface{}{
"region": ag.Region,
})
return errors.WithStackTrace(err)
}
}
logging.Debugf("[OK] %d Auto Scaling Group(s) deleted in %s", len(deletedGroupNames), ag.Region)
return nil
}