Skip to content

Commit

Permalink
default vpc filter for dhcp option (#694)
Browse files Browse the repository at this point in the history
Co-authored-by: Sven Walter <s.walter@rebuy.com>
  • Loading branch information
ga-paul-t and svenwltr authored Dec 4, 2021
1 parent 56c3d68 commit b0bdf81
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 21 deletions.
23 changes: 15 additions & 8 deletions resources/ec2-dhcp-options.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
)

type EC2DHCPOption struct {
svc *ec2.EC2
id *string
tags []*ec2.Tag
svc *ec2.EC2
id *string
tags []*ec2.Tag
defaultVPC bool
}

func init() {
Expand All @@ -19,18 +20,23 @@ func init() {
func ListEC2DHCPOptions(sess *session.Session) ([]Resource, error) {
svc := ec2.New(sess)

resp, err := svc.DescribeDhcpOptions(nil)
resp, err := svc.DescribeDhcpOptions(&ec2.DescribeDhcpOptionsInput{})
if err != nil {
return nil, err
}

defVpcDhcpOptsId := ""
if defVpc := DefaultVpc(svc); defVpc != nil {
defVpcDhcpOptsId = *defVpc.DhcpOptionsId
}

resources := make([]Resource, 0)
for _, out := range resp.DhcpOptions {

resources = append(resources, &EC2DHCPOption{
svc: svc,
id: out.DhcpOptionsId,
tags: out.Tags,
svc: svc,
id: out.DhcpOptionsId,
tags: out.Tags,
defaultVPC: defVpcDhcpOptsId == *out.DhcpOptionsId,
})
}

Expand All @@ -55,6 +61,7 @@ func (e *EC2DHCPOption) Properties() types.Properties {
for _, tagValue := range e.tags {
properties.SetTag(tagValue.Key, tagValue.Value)
}
properties.Set("DefaultVPC", e.defaultVPC)
return properties
}

Expand Down
9 changes: 6 additions & 3 deletions resources/ec2-internet-gateways.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,25 @@ func ListEC2InternetGateways(sess *session.Session) ([]Resource, error) {
return nil, err
}

defVpcId := DefaultVpcID(svc)
defVpcId := ""
if defVpc := DefaultVpc(svc); defVpc != nil {
defVpcId = *defVpc.VpcId
}

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

return resources, nil
}

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

Expand Down
7 changes: 5 additions & 2 deletions resources/ec2-route-tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@ func ListEC2RouteTables(sess *session.Session) ([]Resource, error) {
return nil, err
}

defVpcId := DefaultVpcID(svc)
defVpcId := ""
if defVpc := DefaultVpc(svc); defVpc != nil {
defVpcId = *defVpc.VpcId
}

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

Expand Down
7 changes: 5 additions & 2 deletions resources/ec2-subnets.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,17 @@ func ListEC2Subnets(sess *session.Session) ([]Resource, error) {
return nil, err
}

defVpcId := DefaultVpcID(svc)
defVpcId := ""
if defVpc := DefaultVpc(svc); defVpc != nil {
defVpcId = *defVpc.VpcId
}

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

Expand Down
10 changes: 4 additions & 6 deletions resources/ec2-vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func ListEC2VPCs(sess *session.Session) ([]Resource, error) {
return resources, nil
}

func DefaultVpcID(svc *ec2.EC2) *string {
func DefaultVpc(svc *ec2.EC2) *ec2.Vpc {
resp, err := svc.DescribeVpcs(&ec2.DescribeVpcsInput{
Filters: []*ec2.Filter{
{
Expand All @@ -44,17 +44,15 @@ func DefaultVpcID(svc *ec2.EC2) *string {
},
},
})

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

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

return resp.Vpcs[0].VpcId
return resp.Vpcs[0]
}

func (e *EC2VPC) Remove() error {
Expand Down

0 comments on commit b0bdf81

Please sign in to comment.