forked from gruntwork-io/cloud-nuke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelbv2.go
69 lines (56 loc) · 1.8 KB
/
elbv2.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
package aws
import (
"time"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/elbv2"
"github.com/gruntwork-io/cloud-nuke/logging"
"github.com/gruntwork-io/gruntwork-cli/errors"
)
// Returns a formatted string of ELBv2 Arns
func getAllElbv2Instances(session *session.Session, region string, excludeAfter time.Time) ([]*string, error) {
svc := elbv2.New(session)
result, err := svc.DescribeLoadBalancers(&elbv2.DescribeLoadBalancersInput{})
if err != nil {
return nil, errors.WithStackTrace(err)
}
var arns []*string
for _, balancer := range result.LoadBalancers {
if excludeAfter.After(*balancer.CreatedTime) {
arns = append(arns, balancer.LoadBalancerArn)
}
}
return arns, nil
}
// Deletes all Elastic Load Balancers
func nukeAllElbv2Instances(session *session.Session, arns []*string) error {
svc := elbv2.New(session)
if len(arns) == 0 {
logging.Logger.Infof("No V2 Elastic Load Balancers to nuke in region %s", *session.Config.Region)
return nil
}
logging.Logger.Infof("Deleting all V2 Elastic Load Balancers in region %s", *session.Config.Region)
var deletedArns []*string
for _, arn := range arns {
params := &elbv2.DeleteLoadBalancerInput{
LoadBalancerArn: arn,
}
_, err := svc.DeleteLoadBalancer(params)
if err != nil {
logging.Logger.Errorf("[Failed] %s", err)
} else {
deletedArns = append(deletedArns, arn)
logging.Logger.Infof("Deleted ELBv2: %s", *arn)
}
}
if len(deletedArns) > 0 {
err := svc.WaitUntilLoadBalancersDeleted(&elbv2.DescribeLoadBalancersInput{
LoadBalancerArns: deletedArns,
})
if err != nil {
logging.Logger.Errorf("[Failed] %s", err)
return errors.WithStackTrace(err)
}
}
logging.Logger.Infof("[OK] %d V2 Elastic Load Balancer(s) deleted in %s", len(deletedArns), *session.Config.Region)
return nil
}