-
-
Notifications
You must be signed in to change notification settings - Fork 353
/
ec2_dhcp_option.go
83 lines (70 loc) · 2.47 KB
/
ec2_dhcp_option.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
package resources
import (
"context"
awsgo "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
"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"
"github.com/pterm/pterm"
)
func (v *EC2DhcpOption) getAll(_ context.Context, configObj config.Config) ([]*string, error) {
var dhcpOptionIds []*string
err := v.Client.DescribeDhcpOptionsPages(&ec2.DescribeDhcpOptionsInput{}, func(page *ec2.DescribeDhcpOptionsOutput, lastPage bool) bool {
for _, dhcpOption := range page.DhcpOptions {
// No specific filters to apply at this point, we can think about introducing
// filtering with name tag in the future. In the initial version, we just getAll
// without filtering.
dhcpOptionIds = append(dhcpOptionIds, dhcpOption.DhcpOptionsId)
}
return !lastPage
})
if err != nil {
return nil, errors.WithStackTrace(err)
}
// checking the nukable permissions
v.VerifyNukablePermissions(dhcpOptionIds, func(id *string) error {
_, err := v.Client.DeleteDhcpOptions(&ec2.DeleteDhcpOptionsInput{
DhcpOptionsId: id,
DryRun: awsgo.Bool(true),
})
return err
})
return dhcpOptionIds, nil
}
func (v *EC2DhcpOption) nukeAll(identifiers []*string) error {
for _, identifier := range identifiers {
if nukable, reason := v.IsNukable(awsgo.StringValue(identifier)); !nukable {
logging.Debugf("[Skipping] %s nuke because %v", awsgo.StringValue(identifier), reason)
continue
}
err := nukeDhcpOption(v.Client, identifier)
if err != nil {
logging.Debugf("Failed to delete DHCP option w/ err: %s.", err)
} else {
logging.Infof("Successfully deleted DHCP option %s.", pterm.Green(*identifier))
}
// Record status of this resource
e := report.Entry{
Identifier: awsgo.StringValue(identifier),
ResourceType: v.ResourceName(),
Error: err,
}
report.Record(e)
}
return nil
}
func nukeDhcpOption(client ec2iface.EC2API, option *string) error {
logging.Debugf("Deleting DHCP Option %s", awsgo.StringValue(option))
_, err := client.DeleteDhcpOptions(&ec2.DeleteDhcpOptionsInput{
DhcpOptionsId: option,
})
if err != nil {
logging.Debugf("[Failed] Error deleting DHCP option %s: %s", awsgo.StringValue(option), err)
return errors.WithStackTrace(err)
}
logging.Debugf("[Ok] DHCP Option deleted successfully %s", awsgo.StringValue(option))
return nil
}