Skip to content

Commit

Permalink
Use elb/elbv2 lb tags to filter.
Browse files Browse the repository at this point in the history
Fixes #100
  • Loading branch information
Genevieve L'Esperance committed Feb 13, 2020
1 parent a14c14a commit 3ad303a
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 7 deletions.
22 changes: 22 additions & 0 deletions aws/elb/fakes/load_balancers_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ type LoadBalancersClient struct {
}
Stub func(*awselb.DescribeLoadBalancersInput) (*awselb.DescribeLoadBalancersOutput, error)
}
DescribeTagsCall struct {
sync.Mutex
CallCount int
Receives struct {
DescribeTagsInput *awselb.DescribeTagsInput
}
Returns struct {
DescribeTagsOutput *awselb.DescribeTagsOutput
Error error
}
Stub func(*awselb.DescribeTagsInput) (*awselb.DescribeTagsOutput, error)
}
}

func (f *LoadBalancersClient) DeleteLoadBalancer(param1 *awselb.DeleteLoadBalancerInput) (*awselb.DeleteLoadBalancerOutput, error) {
Expand All @@ -53,3 +65,13 @@ func (f *LoadBalancersClient) DescribeLoadBalancers(param1 *awselb.DescribeLoadB
}
return f.DescribeLoadBalancersCall.Returns.DescribeLoadBalancersOutput, f.DescribeLoadBalancersCall.Returns.Error
}
func (f *LoadBalancersClient) DescribeTags(param1 *awselb.DescribeTagsInput) (*awselb.DescribeTagsOutput, error) {
f.DescribeTagsCall.Lock()
defer f.DescribeTagsCall.Unlock()
f.DescribeTagsCall.CallCount++
f.DescribeTagsCall.Receives.DescribeTagsInput = param1
if f.DescribeTagsCall.Stub != nil {
return f.DescribeTagsCall.Stub(param1)
}
return f.DescribeTagsCall.Returns.DescribeTagsOutput, f.DescribeTagsCall.Returns.Error
}
19 changes: 18 additions & 1 deletion aws/elb/load_balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package elb

import (
"fmt"
"strings"

awselb "github.com/aws/aws-sdk-go/service/elb"
)
Expand All @@ -14,10 +15,26 @@ type LoadBalancer struct {
}

func NewLoadBalancer(client loadBalancersClient, name *string) LoadBalancer {
identifier := *name

tagsOutput, err := client.DescribeTags(&awselb.DescribeTagsInput{LoadBalancerNames: []*string{name}})
if err == nil && tagsOutput != nil && len(tagsOutput.TagDescriptions) == 1 {
tags := tagsOutput.TagDescriptions[0].Tags

var extra []string
for _, t := range tags {
extra = append(extra, fmt.Sprintf("%s:%s", *t.Key, *t.Value))
}

if len(extra) > 0 {
identifier = fmt.Sprintf("%s (%s)", *name, strings.Join(extra, ", "))
}
}

return LoadBalancer{
client: client,
name: name,
identifier: *name,
identifier: identifier,
rtype: "ELB Load Balancer",
}
}
Expand Down
43 changes: 40 additions & 3 deletions aws/elb/load_balancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"

"github.com/aws/aws-sdk-go/aws"
awselb "github.com/aws/aws-sdk-go/service/elb"
"github.com/genevieve/leftovers/aws/elb"
"github.com/genevieve/leftovers/aws/elb/fakes"

Expand All @@ -13,19 +14,53 @@ import (

var _ = Describe("LoadBalancer", func() {
var (
client *fakes.LoadBalancersClient
name *string

loadBalancer elb.LoadBalancer
client *fakes.LoadBalancersClient
name *string
)

BeforeEach(func() {
client = &fakes.LoadBalancersClient{}
name = aws.String("the-name")
})

Describe("NewLoadBalancer", func() {
BeforeEach(func() {
tags := []*awselb.Tag{{Key: aws.String("the-key"), Value: aws.String("the-value")}}
client.DescribeTagsCall.Returns.DescribeTagsOutput = &awselb.DescribeTagsOutput{
TagDescriptions: []*awselb.TagDescription{{
LoadBalancerName: name,
Tags: tags,
}},
}
})

It("returns the identifier", func() {
loadBalancer = elb.NewLoadBalancer(client, name)
Expect(client.DescribeTagsCall.CallCount).To(Equal(1))
Expect(client.DescribeTagsCall.Receives.DescribeTagsInput.LoadBalancerNames[0]).To(Equal(name))

Expect(loadBalancer.Name()).To(Equal("the-name (the-key:the-value)"))
})

loadBalancer = elb.NewLoadBalancer(client, name)
Context("when the describe tags call fails", func() {
BeforeEach(func() {
client.DescribeTagsCall.Returns.Error = errors.New("banana")
})

It("ignores it", func() {
loadBalancer = elb.NewLoadBalancer(client, name)
Expect(loadBalancer.Name()).To(Equal("the-name"))
})
})
})

Describe("Delete", func() {
BeforeEach(func() {
loadBalancer = elb.NewLoadBalancer(client, name)
})

It("deletes the load balancer", func() {
err := loadBalancer.Delete()
Expect(err).NotTo(HaveOccurred())
Expand All @@ -48,12 +83,14 @@ var _ = Describe("LoadBalancer", func() {

Describe("Name", func() {
It("returns the identifier", func() {
loadBalancer = elb.NewLoadBalancer(client, name)
Expect(loadBalancer.Name()).To(Equal("the-name"))
})
})

Describe("Type", func() {
It("returns the type", func() {
loadBalancer = elb.NewLoadBalancer(client, name)
Expect(loadBalancer.Type()).To(Equal("ELB Load Balancer"))
})
})
Expand Down
2 changes: 2 additions & 0 deletions aws/elb/load_balancers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
type loadBalancersClient interface {
DescribeLoadBalancers(*awselb.DescribeLoadBalancersInput) (*awselb.DescribeLoadBalancersOutput, error)
DeleteLoadBalancer(*awselb.DeleteLoadBalancerInput) (*awselb.DeleteLoadBalancerOutput, error)

DescribeTags(*awselb.DescribeTagsInput) (*awselb.DescribeTagsOutput, error)
}

type LoadBalancers struct {
Expand Down
22 changes: 22 additions & 0 deletions aws/elbv2/fakes/load_balancers_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ type LoadBalancersClient struct {
}
Stub func(*awselbv2.DescribeLoadBalancersInput) (*awselbv2.DescribeLoadBalancersOutput, error)
}
DescribeTagsCall struct {
sync.Mutex
CallCount int
Receives struct {
DescribeTagsInput *awselbv2.DescribeTagsInput
}
Returns struct {
DescribeTagsOutput *awselbv2.DescribeTagsOutput
Error error
}
Stub func(*awselbv2.DescribeTagsInput) (*awselbv2.DescribeTagsOutput, error)
}
}

func (f *LoadBalancersClient) DeleteLoadBalancer(param1 *awselbv2.DeleteLoadBalancerInput) (*awselbv2.DeleteLoadBalancerOutput, error) {
Expand All @@ -53,3 +65,13 @@ func (f *LoadBalancersClient) DescribeLoadBalancers(param1 *awselbv2.DescribeLoa
}
return f.DescribeLoadBalancersCall.Returns.DescribeLoadBalancersOutput, f.DescribeLoadBalancersCall.Returns.Error
}
func (f *LoadBalancersClient) DescribeTags(param1 *awselbv2.DescribeTagsInput) (*awselbv2.DescribeTagsOutput, error) {
f.DescribeTagsCall.Lock()
defer f.DescribeTagsCall.Unlock()
f.DescribeTagsCall.CallCount++
f.DescribeTagsCall.Receives.DescribeTagsInput = param1
if f.DescribeTagsCall.Stub != nil {
return f.DescribeTagsCall.Stub(param1)
}
return f.DescribeTagsCall.Returns.DescribeTagsOutput, f.DescribeTagsCall.Returns.Error
}
19 changes: 18 additions & 1 deletion aws/elbv2/load_balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package elbv2

import (
"fmt"
"strings"

awselbv2 "github.com/aws/aws-sdk-go/service/elbv2"
)
Expand All @@ -15,11 +16,27 @@ type LoadBalancer struct {
}

func NewLoadBalancer(client loadBalancersClient, name, arn *string) LoadBalancer {
identifier := *name

tagsOutput, err := client.DescribeTags(&awselbv2.DescribeTagsInput{ResourceArns: []*string{arn}})
if err == nil && tagsOutput != nil && len(tagsOutput.TagDescriptions) == 1 {
tags := tagsOutput.TagDescriptions[0].Tags

var extra []string
for _, t := range tags {
extra = append(extra, fmt.Sprintf("%s:%s", *t.Key, *t.Value))
}

if len(extra) > 0 {
identifier = fmt.Sprintf("%s (%s)", *name, strings.Join(extra, ", "))
}
}

return LoadBalancer{
client: client,
name: name,
arn: arn,
identifier: *name,
identifier: identifier,
rtype: "ELBV2 Load Balancer",
}
}
Expand Down
39 changes: 37 additions & 2 deletions aws/elbv2/load_balancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"

"github.com/aws/aws-sdk-go/aws"
awselbv2 "github.com/aws/aws-sdk-go/service/elbv2"
"github.com/genevieve/leftovers/aws/elbv2"
"github.com/genevieve/leftovers/aws/elbv2/fakes"

Expand All @@ -23,11 +24,43 @@ var _ = Describe("LoadBalancer", func() {
client = &fakes.LoadBalancersClient{}
name = aws.String("the-name")
arn = aws.String("the-arn")
})

Describe("NewLoadBalancer", func() {
BeforeEach(func() {
tags := []*awselbv2.Tag{{Key: aws.String("the-key"), Value: aws.String("the-value")}}
client.DescribeTagsCall.Returns.DescribeTagsOutput = &awselbv2.DescribeTagsOutput{
TagDescriptions: []*awselbv2.TagDescription{{
ResourceArn: arn,
Tags: tags,
}},
}
})

It("returns the identifier", func() {
loadBalancer = elbv2.NewLoadBalancer(client, name, arn)
Expect(client.DescribeTagsCall.CallCount).To(Equal(1))
Expect(client.DescribeTagsCall.Receives.DescribeTagsInput.ResourceArns[0]).To(Equal(arn))

Expect(loadBalancer.Name()).To(Equal("the-name (the-key:the-value)"))
})

loadBalancer = elbv2.NewLoadBalancer(client, name, arn)
Context("when the describe tags call fails", func() {
BeforeEach(func() {
client.DescribeTagsCall.Returns.Error = errors.New("banana")
})

It("ignores it", func() {
loadBalancer = elbv2.NewLoadBalancer(client, name, arn)
Expect(loadBalancer.Name()).To(Equal("the-name"))
})
})
})

Describe("Delete", func() {
BeforeEach(func() {
loadBalancer = elbv2.NewLoadBalancer(client, name, arn)
})
It("deletes the load balancer", func() {
err := loadBalancer.Delete()
Expect(err).NotTo(HaveOccurred())
Expand All @@ -50,12 +83,14 @@ var _ = Describe("LoadBalancer", func() {

Describe("Name", func() {
It("returns the identifier", func() {
loadBalancer = elbv2.NewLoadBalancer(client, name, arn)
Expect(loadBalancer.Name()).To(Equal("the-name"))
})
})

Describe("Type", func() {
It("returns \"load balancer\"", func() {
It("returns load balancer", func() {
loadBalancer = elbv2.NewLoadBalancer(client, name, arn)
Expect(loadBalancer.Type()).To(Equal("ELBV2 Load Balancer"))
})
})
Expand Down
2 changes: 2 additions & 0 deletions aws/elbv2/load_balancers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
type loadBalancersClient interface {
DescribeLoadBalancers(*awselbv2.DescribeLoadBalancersInput) (*awselbv2.DescribeLoadBalancersOutput, error)
DeleteLoadBalancer(*awselbv2.DeleteLoadBalancerInput) (*awselbv2.DeleteLoadBalancerOutput, error)

DescribeTags(*awselbv2.DescribeTagsInput) (*awselbv2.DescribeTagsOutput, error)
}

type LoadBalancers struct {
Expand Down

0 comments on commit 3ad303a

Please sign in to comment.