Skip to content

Commit

Permalink
Delete gcp firewalls that belong to a network that matches the filter.
Browse files Browse the repository at this point in the history
- For #80
  • Loading branch information
Genevieve Lesperance committed Dec 23, 2019
1 parent 262092e commit acc62ad
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 16 deletions.
29 changes: 25 additions & 4 deletions gcp/compute/fakes/firewalls_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package fakes
import (
"sync"

gcp "google.golang.org/api/compute/v1"
gcpcompute "google.golang.org/api/compute/v1"
)

type FirewallsClient struct {
Expand All @@ -18,14 +18,25 @@ type FirewallsClient struct {
}
Stub func(string) error
}
GetNetworkNameCall struct {
sync.Mutex
CallCount int
Receives struct {
Url string
}
Returns struct {
Name string
}
Stub func(string) string
}
ListFirewallsCall struct {
sync.Mutex
CallCount int
Returns struct {
FirewallSlice []*gcp.Firewall
FirewallSlice []*gcpcompute.Firewall
Error error
}
Stub func() ([]*gcp.Firewall, error)
Stub func() ([]*gcpcompute.Firewall, error)
}
}

Expand All @@ -39,7 +50,17 @@ func (f *FirewallsClient) DeleteFirewall(param1 string) error {
}
return f.DeleteFirewallCall.Returns.Error
}
func (f *FirewallsClient) ListFirewalls() ([]*gcp.Firewall, error) {
func (f *FirewallsClient) GetNetworkName(param1 string) string {
f.GetNetworkNameCall.Lock()
defer f.GetNetworkNameCall.Unlock()
f.GetNetworkNameCall.CallCount++
f.GetNetworkNameCall.Receives.Url = param1
if f.GetNetworkNameCall.Stub != nil {
return f.GetNetworkNameCall.Stub(param1)
}
return f.GetNetworkNameCall.Returns.Name
}
func (f *FirewallsClient) ListFirewalls() ([]*gcpcompute.Firewall, error) {
f.ListFirewallsCall.Lock()
defer f.ListFirewallsCall.Unlock()
f.ListFirewallsCall.CallCount++
Expand Down
22 changes: 15 additions & 7 deletions gcp/compute/firewall.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,28 @@ package compute
import "fmt"

type Firewall struct {
client firewallsClient
name string
client firewallsClient
name string
clearerName string
}

func NewFirewall(client firewallsClient, name string) Firewall {
func NewFirewall(client firewallsClient, name, network string) Firewall {
clearerName := name

networkName := client.GetNetworkName(network)
if len(networkName) > 0 {
clearerName = fmt.Sprintf("%s (%s)", name, networkName)
}

return Firewall{
client: client,
name: name,
client: client,
name: name,
clearerName: clearerName,
}
}

func (f Firewall) Delete() error {
err := f.client.DeleteFirewall(f.name)

if err != nil {
return fmt.Errorf("Delete: %s", err)
}
Expand All @@ -25,7 +33,7 @@ func (f Firewall) Delete() error {
}

func (f Firewall) Name() string {
return f.name
return f.clearerName
}

func (f Firewall) Type() string {
Expand Down
13 changes: 9 additions & 4 deletions gcp/compute/firewall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package compute_test

import (
"errors"
"fmt"

"github.com/genevieve/leftovers/gcp/compute"
"github.com/genevieve/leftovers/gcp/compute/fakes"
Expand All @@ -12,17 +13,21 @@ import (

var _ = Describe("Firewall", func() {
var (
client *fakes.FirewallsClient
name string
client *fakes.FirewallsClient
name string
network string

firewall compute.Firewall
)

BeforeEach(func() {
client = &fakes.FirewallsClient{}
name = "banana"
network = "global/networks/kiwi-network"

firewall = compute.NewFirewall(client, name)
client.GetNetworkNameCall.Returns.Name = "kiwi-network"

firewall = compute.NewFirewall(client, name, network)
})

Describe("Delete", func() {
Expand All @@ -48,7 +53,7 @@ var _ = Describe("Firewall", func() {

Describe("Name", func() {
It("returns the name", func() {
Expect(firewall.Name()).To(Equal(name))
Expect(firewall.Name()).To(Equal(fmt.Sprintf("%s (kiwi-network)", name)))
})
})

Expand Down
3 changes: 2 additions & 1 deletion gcp/compute/firewalls.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

//go:generate faux --interface firewallsClient --output fakes/firewalls_client.go
type firewallsClient interface {
GetNetworkName(url string) (name string)
ListFirewalls() ([]*gcpcompute.Firewall, error)
DeleteFirewall(firewall string) error
}
Expand All @@ -35,7 +36,7 @@ func (f Firewalls) List(filter string) ([]common.Deletable, error) {

var resources []common.Deletable
for _, firewall := range firewalls {
resource := NewFirewall(f.client, firewall.Name)
resource := NewFirewall(f.client, firewall.Name, firewall.Network)

if strings.Contains(resource.Name(), "default") {
continue
Expand Down
17 changes: 17 additions & 0 deletions gcp/compute/firewalls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,23 @@ var _ = Describe("Firewalls", func() {
Expect(list).To(HaveLen(1))
})

Context("when the firewall name does not contain the filter but the network does", func() {
BeforeEach(func() {
client.ListFirewallsCall.Returns.FirewallSlice = []*gcpcompute.Firewall{{
Name: "banana-firewall",
Network: "global/networks/kiwi-network",
}}
client.GetNetworkNameCall.Returns.Name = "kiwi-network"
})

It("returns it in the list to delete", func() {
list, err := firewalls.List("kiwi")
Expect(err).NotTo(HaveOccurred())

Expect(list).To(HaveLen(1))
})
})

Context("when the client fails to list firewalls", func() {
BeforeEach(func() {
client.ListFirewallsCall.Returns.Error = errors.New("some error")
Expand Down

0 comments on commit acc62ad

Please sign in to comment.