-
Notifications
You must be signed in to change notification settings - Fork 787
/
delete_aws.go
181 lines (161 loc) · 5 KB
/
delete_aws.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package cmd
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/jenkins-x/jx/pkg/cloud/amazon"
"github.com/jenkins-x/jx/pkg/log"
logger "github.com/sirupsen/logrus"
"gopkg.in/AlecAivazis/survey.v1/terminal"
"io"
"strings"
"time"
"github.com/aws/aws-sdk-go/service/elbv2"
"github.com/spf13/cobra"
)
const gatewayDetachAttempts = 10
type DeleteAwsOptions struct {
CommonOptions
Profile string
Region string
VpcId string
}
func NewCmdDeleteAws(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := &DeleteAwsOptions{
CommonOptions: CommonOptions{
Factory: f,
In: in,
Out: out,
Err: errOut,
},
}
cmd := &cobra.Command{
Use: "aws",
Short: "Deletes given AWS VPC and resources associated with it (like elastic load balancers or subnets)",
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
cmd.Flags().StringVarP(&options.LogLevel, "log-level", "", logger.InfoLevel.String(), "Logging level. Possible values - panic, fatal, error, warning, info, debug.")
cmd.Flags().StringVarP(&options.Profile, "profile", "", "", "AWS profile to use.")
cmd.Flags().StringVarP(&options.Region, "region", "", "", "AWS region to use.")
cmd.Flags().StringVarP(&options.VpcId, "vpc-id", "", "", "ID of VPC to delete.")
return cmd
}
func (o *DeleteAwsOptions) Run() error {
log.ConfigureLog(o.LogLevel)
vpcid := o.VpcId
session, err := amazon.NewAwsSession(o.Profile, o.Region)
if err != nil {
return err
}
svc := ec2.New(session)
// Delete elastic load balancers assigned to VPC
elbSvc := elbv2.New(session)
loadBalancers, err := elbSvc.DescribeLoadBalancers(&elbv2.DescribeLoadBalancersInput{})
if err != nil {
return err
}
for _, loadBalancer := range loadBalancers.LoadBalancers {
if *loadBalancer.VpcId == vpcid {
log.Infof("Deleting load balancer %s...\n", *loadBalancer.LoadBalancerName)
_, err = elbSvc.DeleteLoadBalancer(&elbv2.DeleteLoadBalancerInput{LoadBalancerArn: loadBalancer.LoadBalancerArn})
if err != nil {
return err
}
log.Infof("Load balancer %s deleted.\n", *loadBalancer.LoadBalancerName)
}
}
// Detached and delete internet gateways associated with given VPC
internetGateways, err := svc.DescribeInternetGateways(&ec2.DescribeInternetGatewaysInput{
Filters: []*ec2.Filter{
{
Name: aws.String("attachment.vpc-id"),
Values: []*string{
aws.String(vpcid),
},
},
},
})
if err != nil {
return err
}
for _, internetGateway := range internetGateways.InternetGateways {
if len(internetGateway.Attachments) > 0 {
err = o.retryUntilFatalError(gatewayDetachAttempts, 10 * time.Second, func() (fatalError *FatalError, e error) {
_, err = svc.DetachInternetGateway(&ec2.DetachInternetGatewayInput{InternetGatewayId: internetGateway.InternetGatewayId, VpcId: aws.String(vpcid)})
log.Infof("Detaching internet gateway %s from VPC %s...\n", *internetGateway.InternetGatewayId, vpcid)
if err != nil {
if strings.Contains(err.Error(), "Please unmap those public address(es) before detaching the gateway") {
log.Info("Waiting for public address to be unmapped from internet gateway.")
return nil, err
}
return &FatalError{E: err}, nil
}
return nil, nil
})
if err != nil {
return err
}
log.Infof("Internet gateway %s detached successfully from VPC %s...\n", *internetGateway.InternetGatewayId, vpcid)
}
_, err = svc.DeleteInternetGateway(&ec2.DeleteInternetGatewayInput{InternetGatewayId: internetGateway.InternetGatewayId})
log.Infof("Deleting internet gateway %s...\n", *internetGateway.InternetGatewayId)
if err != nil {
return err
}
}
// Delete subnets assigned to VPC
subnets, err := svc.DescribeSubnets(&ec2.DescribeSubnetsInput{
Filters: []*ec2.Filter{
{
Name: aws.String("vpc-id"),
Values: []*string{
aws.String(vpcid),
},
},
},
})
for _, subnet := range subnets.Subnets {
interfaces, err := svc.DescribeNetworkInterfaces(&ec2.DescribeNetworkInterfacesInput{
Filters: []*ec2.Filter{
{
Name: aws.String("subnet-id"),
Values: []*string{
subnet.SubnetId,
},
},
},
})
if err != nil {
return err
}
for _, iface := range interfaces.NetworkInterfaces {
if iface.Attachment != nil {
log.Infof("Detaching interface %s\n", *iface.NetworkInterfaceId)
_, err = svc.DetachNetworkInterface(&ec2.DetachNetworkInterfaceInput{AttachmentId: iface.Attachment.AttachmentId})
if err != nil {
return err
}
}
_, err := svc.DeleteNetworkInterface(&ec2.DeleteNetworkInterfaceInput{NetworkInterfaceId: iface.NetworkInterfaceId})
if err != nil {
return err
}
}
_, err = svc.DeleteSubnet(&ec2.DeleteSubnetInput{SubnetId: subnet.SubnetId})
if err != nil {
return err
}
}
// Delete VPC
_, err = svc.DeleteVpc(&ec2.DeleteVpcInput{
VpcId: aws.String(vpcid),
})
if err != nil {
return err
}
return nil
}