Skip to content

Commit

Permalink
advertise association with default vpc through properties (#691)
Browse files Browse the repository at this point in the history
* advertise association with default vpc through properties

* rename property and adopt naming convention
  • Loading branch information
ga-paul-t committed Sep 30, 2021
1 parent 9efd910 commit 93471de
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 22 deletions.
23 changes: 13 additions & 10 deletions resources/ec2-internet-gateway-attachments.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import (
)

type EC2InternetGatewayAttachment struct {
svc *ec2.EC2
vpcId *string
vpcTags []*ec2.Tag
igwId *string
igwTags []*ec2.Tag
svc *ec2.EC2
vpcId *string
vpcTags []*ec2.Tag
igwId *string
igwTags []*ec2.Tag
defaultVPC bool
}

func init() {
Expand Down Expand Up @@ -47,11 +48,12 @@ func ListEC2InternetGatewayAttachments(sess *session.Session) ([]Resource, error

for _, igw := range resp.InternetGateways {
resources = append(resources, &EC2InternetGatewayAttachment{
svc: svc,
vpcId: vpc.VpcId,
vpcTags: vpc.Tags,
igwId: igw.InternetGatewayId,
igwTags: igw.Tags,
svc: svc,
vpcId: vpc.VpcId,
vpcTags: vpc.Tags,
igwId: igw.InternetGatewayId,
igwTags: igw.Tags,
defaultVPC: *vpc.IsDefault,
})
}
}
Expand Down Expand Up @@ -81,6 +83,7 @@ func (e *EC2InternetGatewayAttachment) Properties() types.Properties {
for _, tagValue := range e.vpcTags {
properties.SetTagWithPrefix("vpc", tagValue.Key, tagValue.Value)
}
properties.Set("DefaultVPC", e.defaultVPC)
return properties
}

Expand Down
26 changes: 22 additions & 4 deletions resources/ec2-internet-gateways.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (
)

type EC2InternetGateway struct {
svc *ec2.EC2
igw *ec2.InternetGateway
svc *ec2.EC2
igw *ec2.InternetGateway
defaultVPC bool
}

func init() {
Expand All @@ -23,17 +24,33 @@ func ListEC2InternetGateways(sess *session.Session) ([]Resource, error) {
return nil, err
}

defVpcId := DefaultVpcID(svc)

resources := make([]Resource, 0)
for _, igw := range resp.InternetGateways {
resources = append(resources, &EC2InternetGateway{
svc: svc,
igw: igw,
svc: svc,
igw: igw,
defaultVPC: HasVpcAttachment(defVpcId, igw.Attachments),
})
}

return resources, nil
}

func HasVpcAttachment(vpcId *string, attachments []*ec2.InternetGatewayAttachment) bool {
if vpcId == nil {
return false
}

for _, attach := range attachments {
if *vpcId == *attach.VpcId {
return true
}
}
return false
}

func (e *EC2InternetGateway) Remove() error {
params := &ec2.DeleteInternetGatewayInput{
InternetGatewayId: e.igw.InternetGatewayId,
Expand All @@ -52,6 +69,7 @@ func (e *EC2InternetGateway) Properties() types.Properties {
for _, tagValue := range e.igw.Tags {
properties.SetTag(tagValue.Key, tagValue.Value)
}
properties.Set("DefaultVPC", e.defaultVPC)
return properties
}

Expand Down
5 changes: 5 additions & 0 deletions resources/ec2-route-tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
type EC2RouteTable struct {
svc *ec2.EC2
routeTable *ec2.RouteTable
defaultVPC bool
}

func init() {
Expand All @@ -23,11 +24,14 @@ func ListEC2RouteTables(sess *session.Session) ([]Resource, error) {
return nil, err
}

defVpcId := DefaultVpcID(svc)

resources := make([]Resource, 0)
for _, out := range resp.RouteTables {
resources = append(resources, &EC2RouteTable{
svc: svc,
routeTable: out,
defaultVPC: *defVpcId == *out.VpcId,
})
}

Expand All @@ -52,6 +56,7 @@ func (e *EC2RouteTable) Properties() types.Properties {
for _, tagValue := range e.routeTable.Tags {
properties.SetTag(tagValue.Key, tagValue.Value)
}
properties.Set("DefaultVPC", e.defaultVPC)
return properties
}

Expand Down
13 changes: 9 additions & 4 deletions resources/ec2-subnets.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (
)

type EC2Subnet struct {
svc *ec2.EC2
subnet *ec2.Subnet
svc *ec2.EC2
subnet *ec2.Subnet
defaultVPC bool
}

func init() {
Expand All @@ -24,11 +25,14 @@ func ListEC2Subnets(sess *session.Session) ([]Resource, error) {
return nil, err
}

defVpcId := DefaultVpcID(svc)

resources := make([]Resource, 0)
for _, out := range resp.Subnets {
resources = append(resources, &EC2Subnet{
svc: svc,
subnet: out,
svc: svc,
subnet: out,
defaultVPC: *defVpcId == *out.VpcId,
})
}

Expand All @@ -54,6 +58,7 @@ func (e *EC2Subnet) Properties() types.Properties {
properties.SetTag(tagValue.Key, tagValue.Value)
}
properties.Set("DefaultForAz", e.subnet.DefaultForAz)
properties.Set("DefaultVPC", e.defaultVPC)
return properties
}

Expand Down
31 changes: 27 additions & 4 deletions resources/ec2-vpc.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package resources

import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/rebuy-de/aws-nuke/pkg/types"
)

type EC2VPC struct {
svc *ec2.EC2
vpc *ec2.Vpc
svc *ec2.EC2
vpc *ec2.Vpc
}

func init() {
Expand All @@ -26,14 +27,36 @@ func ListEC2VPCs(sess *session.Session) ([]Resource, error) {
resources := make([]Resource, 0)
for _, vpc := range resp.Vpcs {
resources = append(resources, &EC2VPC{
svc: svc,
vpc: vpc,
svc: svc,
vpc: vpc,
})
}

return resources, nil
}

func DefaultVpcID(svc *ec2.EC2) *string {
resp, err := svc.DescribeVpcs(&ec2.DescribeVpcsInput{
Filters: []*ec2.Filter{
{
Name: aws.String("is-default"),
Values: aws.StringSlice([]string{"true"}),
},
},
})

noVpc := ""
if err != nil {
return &noVpc
}

if len(resp.Vpcs) == 0 {
return &noVpc
}

return resp.Vpcs[0].VpcId
}

func (e *EC2VPC) Remove() error {
params := &ec2.DeleteVpcInput{
VpcId: e.vpc.VpcId,
Expand Down

0 comments on commit 93471de

Please sign in to comment.