Skip to content

Commit

Permalink
If gcp routes belong to network that contain filter, delete them.
Browse files Browse the repository at this point in the history
- For #80.
  • Loading branch information
Genevieve Lesperance committed Dec 23, 2019
1 parent 920be18 commit 90065fd
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 29 deletions.
10 changes: 10 additions & 0 deletions gcp/compute/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package compute

import (
"fmt"
"strings"
"time"

gcpcompute "google.golang.org/api/compute/v1"
Expand Down Expand Up @@ -79,6 +80,15 @@ func NewClient(project string, service *gcpcompute.Service, logger logger) clien
}
}

func (c client) GetNetworkName(url string) string {
a := strings.Split(url, "/networks/")
if len(a) > 1 {
name := a[1]
return name
}
return ""
}

func (c client) ListAddresses(region string) ([]*gcpcompute.Address, error) {
var token string
list := []*gcpcompute.Address{}
Expand Down
29 changes: 25 additions & 4 deletions gcp/compute/fakes/instances_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package fakes
import (
"sync"

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

type InstancesClient struct {
Expand All @@ -19,17 +19,28 @@ type InstancesClient struct {
}
Stub func(string, string) error
}
GetNetworkNameCall struct {
sync.Mutex
CallCount int
Receives struct {
Url string
}
Returns struct {
Name string
}
Stub func(string) string
}
ListInstancesCall struct {
sync.Mutex
CallCount int
Receives struct {
Zone string
}
Returns struct {
InstanceSlice []*compute.Instance
InstanceSlice []*gcpcompute.Instance
Error error
}
Stub func(string) ([]*compute.Instance, error)
Stub func(string) ([]*gcpcompute.Instance, error)
}
}

Expand All @@ -44,7 +55,17 @@ func (f *InstancesClient) DeleteInstance(param1 string, param2 string) error {
}
return f.DeleteInstanceCall.Returns.Error
}
func (f *InstancesClient) ListInstances(param1 string) ([]*compute.Instance, error) {
func (f *InstancesClient) 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 *InstancesClient) ListInstances(param1 string) ([]*gcpcompute.Instance, error) {
f.ListInstancesCall.Lock()
defer f.ListInstancesCall.Unlock()
f.ListInstancesCall.CallCount++
Expand Down
21 changes: 21 additions & 0 deletions gcp/compute/fakes/routes_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ type RoutesClient struct {
}
Stub func(string) error
}
GetNetworkNameCall struct {
sync.Mutex
CallCount int
Receives struct {
Url string
}
Returns struct {
Name string
}
Stub func(string) string
}
ListRoutesCall struct {
sync.Mutex
CallCount int
Expand All @@ -39,6 +50,16 @@ func (f *RoutesClient) DeleteRoute(param1 string) error {
}
return f.DeleteRouteCall.Returns.Error
}
func (f *RoutesClient) 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 *RoutesClient) ListRoutes() ([]*gcpcompute.Route, error) {
f.ListRoutesCall.Lock()
defer f.ListRoutesCall.Unlock()
Expand Down
15 changes: 4 additions & 11 deletions gcp/compute/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,14 @@ type Instance struct {
zone string
}

func NewInstance(
client instancesClient,
name, zone string,
tags *gcpcompute.Tags,
networkInterfaces []*gcpcompute.NetworkInterface,
) Instance {

func NewInstance(client instancesClient, name, zone string, tags *gcpcompute.Tags, networkInterfaces []*gcpcompute.NetworkInterface) Instance {
clearerName := name

extra := []string{}
for _, ni := range networkInterfaces {
link := strings.Split(ni.Network, "/networks/")
if len(link) > 1 {
networkName := link[1]
extra = append(extra, networkName)
network := client.GetNetworkName(ni.Network)
if len(network) > 0 {
extra = append(extra, network)
}
}

Expand Down
2 changes: 2 additions & 0 deletions gcp/compute/instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ var _ = Describe("Instance", func() {
networkInterfaces = []*gcpcompute.NetworkInterface{{Network: "https://www.googleapis.com/compute/v1/projects/id/global/networks/kiwi-network"}}
tags = &gcpcompute.Tags{Items: []string{"tag-1"}}

client.GetNetworkNameCall.Returns.Name = "kiwi-network"

instance = compute.NewInstance(client, name, zone, tags, networkInterfaces)
})

Expand Down
1 change: 1 addition & 0 deletions gcp/compute/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

//go:generate faux --interface instancesClient --output fakes/instances_client.go
type instancesClient interface {
GetNetworkName(url string) (name string)
ListInstances(zone string) ([]*gcpcompute.Instance, error)
DeleteInstance(zone, instance string) error
}
Expand Down
2 changes: 2 additions & 0 deletions gcp/compute/instances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ var _ = Describe("Instances", func() {
Zone: "https://zone-1",
NetworkInterfaces: []*gcpcompute.NetworkInterface{{Network: "global/networks/kiwi"}},
}}
client.GetNetworkNameCall.Returns.Name = "kiwi-network"
filter = "kiwi"
})

It("will add it to the list to delete", func() {
list, err := instances.List(filter)
Expect(err).NotTo(HaveOccurred())
Expand Down
26 changes: 18 additions & 8 deletions gcp/compute/route.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
package compute

import "fmt"
import (
"fmt"
)

type Route struct {
client routesClient
name string
client routesClient
name string
clearerName string
}

func NewRoute(client routesClient, name string) Route {
func NewRoute(client routesClient, name, network string) Route {
clearerName := name

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

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

func (r Route) Delete() error {
err := r.client.DeleteRoute(r.name)

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

func (r Route) Name() string {
return r.name
return r.clearerName
}

func (r Route) Type() string {
Expand Down
13 changes: 9 additions & 4 deletions gcp/compute/route_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("Route", func() {
var (
client *fakes.RoutesClient
name string
client *fakes.RoutesClient
name string
network string

route compute.Route
)

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

route = compute.NewRoute(client, name)
client.GetNetworkNameCall.Returns.Name = "kiwi-network"

route = compute.NewRoute(client, name, network)
})

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

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

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

//go:generate faux --interface routesClient --output fakes/routes_client.go
type routesClient interface {
GetNetworkName(url string) (name string)
ListRoutes() ([]*gcpcompute.Route, error)
DeleteRoute(route string) error
}
Expand All @@ -35,9 +36,9 @@ func (r Routes) List(filter string) ([]common.Deletable, error) {

var resources []common.Deletable
for _, route := range routes {
resource := NewRoute(r.client, route.Name)
resource := NewRoute(r.client, route.Name, route.Network)

if !strings.Contains(route.Name, filter) || strings.Contains(route.Name, "default") {
if !strings.Contains(resource.Name(), filter) || strings.Contains(route.Name, "default") {
continue
}

Expand Down
16 changes: 16 additions & 0 deletions gcp/compute/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,22 @@ var _ = Describe("Routes", func() {
Expect(list).To(HaveLen(1))
})

Context("when the route name does not contain the filter, but the network does", func() {
BeforeEach(func() {
client.ListRoutesCall.Returns.RouteSlice = []*gcpcompute.Route{{
Name: "banana-route",
Network: ".com/networks/kiwi-network",
}}
client.GetNetworkNameCall.Returns.Name = "kiwi-network"
})
It("returns the route in the list to delete", func() {
list, err := routes.List("kiwi")
Expect(err).NotTo(HaveOccurred())

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

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

0 comments on commit 90065fd

Please sign in to comment.