/
vpc_subnet.go
121 lines (99 loc) · 2.9 KB
/
vpc_subnet.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
package aws
import (
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
log "github.com/sirupsen/logrus"
"github.com/Qovery/pleco/pkg/common"
)
type Subnet struct {
Id string
CreationDate time.Time
ttl int64
IsProtected bool
}
func getSubnetsByVpcId(ec2Session *ec2.EC2, vpcId string) []*ec2.Subnet {
input := &ec2.DescribeSubnetsInput{
Filters: []*ec2.Filter{
{
Name: aws.String("vpc-id"),
Values: []*string{aws.String(vpcId)},
},
},
}
subnets, err := ec2Session.DescribeSubnets(input)
if err != nil {
log.Error(err)
}
return subnets.Subnets
}
func GetSubnetsIdsByVpcId(ec2Session *ec2.EC2, vpcId string, tagName string) []Subnet {
var subnetsStruct []Subnet
subnets := getSubnetsByVpcId(ec2Session, vpcId)
for _, subnet := range subnets {
essentialTags := common.GetEssentialTags(subnet.Tags, tagName)
var subnetStruct = Subnet{
Id: *subnet.SubnetId,
CreationDate: essentialTags.CreationDate,
ttl: essentialTags.TTL,
IsProtected: essentialTags.IsProtected,
}
subnetsStruct = append(subnetsStruct, subnetStruct)
}
return subnetsStruct
}
func DeleteSubnetsByIds(ec2Session *ec2.EC2, subnets []Subnet) {
for _, subnet := range subnets {
if !subnet.IsProtected {
_, err := ec2Session.DeleteSubnet(
&ec2.DeleteSubnetInput{
SubnetId: aws.String(subnet.Id),
},
)
if err != nil {
log.Error(err)
} else {
log.Debugf("Subnet %s in %s deleted.", subnet.Id, *ec2Session.Config.Region)
}
}
}
}
// DeleteVPCLinkedResourcesWithQuota is used to delete some resources linked to a vpc without deleting the vpc itself.
// This will avoid quota issues on some resources
func DeleteVPCLinkedResourcesWithQuota(sessions AWSSessions, options AwsOptions) {
vpcs, err := listTaggedVPC(sessions.EC2, &options)
if err != nil {
log.Errorf("can't list VPC: %s\n", err)
return
}
region := *sessions.EC2.Config.Region
if err != nil {
log.Errorf("Can't list instances: %s\n", err)
return
}
securityGroupCount := 0
subnetCount := 0
routeTableCount := 0
for _, vpc := range vpcs {
securityGroupCount += len(vpc.SecurityGroups)
subnetCount += len(vpc.Subnets)
routeTableCount += len(vpc.RouteTables)
}
sgCount, sgStart := common.ElemToDeleteFormattedInfos("expired VPC Security Group", securityGroupCount, region)
sCount, sStart := common.ElemToDeleteFormattedInfos("expired VPC Subnet", subnetCount, region)
rtCount, rtStart := common.ElemToDeleteFormattedInfos("expired VPC Route Table", routeTableCount, region)
log.Info(sgCount)
log.Info(sCount)
log.Info(rtCount)
if options.DryRun || len(vpcs) == 0 {
return
}
log.Info(sgStart)
log.Info(sStart)
log.Info(rtStart)
for _, vpc := range vpcs {
DeleteSecurityGroupsByIds(sessions.EC2, vpc.SecurityGroups)
DeleteSubnetsByIds(sessions.EC2, vpc.Subnets)
DeleteRouteTablesByIds(sessions.EC2, vpc.RouteTables)
}
}