-
-
Notifications
You must be signed in to change notification settings - Fork 354
/
ec2_dedicated_host.go
111 lines (92 loc) · 3.01 KB
/
ec2_dedicated_host.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package resources
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go/aws"
awsgo "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"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/cloud-nuke/util"
"github.com/gruntwork-io/go-commons/errors"
)
func (h *EC2DedicatedHosts) getAll(c context.Context, configObj config.Config) ([]*string, error) {
var hostIds []*string
describeHostsInput := &ec2.DescribeHostsInput{
Filter: []*ec2.Filter{
{
Name: awsgo.String("state"),
Values: []*string{
awsgo.String("available"),
awsgo.String("under-assessment"),
awsgo.String("permanent-failure"),
},
},
},
}
err := h.Client.DescribeHostsPages(
describeHostsInput,
func(page *ec2.DescribeHostsOutput, lastPage bool) bool {
for _, host := range page.Hosts {
if shouldIncludeHostId(host, configObj) {
hostIds = append(hostIds, host.HostId)
}
}
return !lastPage
},
)
if err != nil {
return nil, errors.WithStackTrace(err)
}
return hostIds, nil
}
func shouldIncludeHostId(host *ec2.Host, configObj config.Config) bool {
if host == nil {
return false
}
// If an instance is using the host allocation we cannot release it
if len(host.Instances) != 0 {
logging.Debugf("Host %s has instance(s) still associated, unable to nuke.", *host.HostId)
return false
}
// If Name is unset, GetEC2ResourceNameTagValue returns error and zero value string
// Ignore this error and pass empty string to config.ShouldInclude
hostNameTagValue := util.GetEC2ResourceNameTagValue(host.Tags)
return configObj.EC2DedicatedHosts.ShouldInclude(config.ResourceValue{
Name: hostNameTagValue,
Time: host.AllocationTime,
})
}
func (h *EC2DedicatedHosts) nukeAll(hostIds []*string) error {
if len(hostIds) == 0 {
logging.Debugf("No EC2 dedicated hosts to nuke in region %s", h.Region)
return nil
}
logging.Debugf("Releasing all EC2 dedicated host allocations in region %s", h.Region)
input := &ec2.ReleaseHostsInput{HostIds: hostIds}
releaseResult, err := h.Client.ReleaseHosts(input)
if err != nil {
logging.Debugf("[Failed] %s", err)
return errors.WithStackTrace(err)
}
// Report successes and failures from release host request
for _, hostSuccess := range releaseResult.Successful {
logging.Debugf("[OK] Dedicated host %s was released in %s", aws.StringValue(hostSuccess), h.Region)
e := report.Entry{
Identifier: aws.StringValue(hostSuccess),
ResourceType: "EC2 Dedicated Host",
}
report.Record(e)
}
for _, hostFailed := range releaseResult.Unsuccessful {
logging.Debugf("[ERROR] Unable to release dedicated host %s in %s: %s", aws.StringValue(hostFailed.ResourceId), h.Region, aws.StringValue(hostFailed.Error.Message))
e := report.Entry{
Identifier: aws.StringValue(hostFailed.ResourceId),
ResourceType: "EC2 Dedicated Host",
Error: fmt.Errorf(*hostFailed.Error.Message),
}
report.Record(e)
}
return nil
}