Skip to content
This repository has been archived by the owner on May 6, 2024. It is now read-only.

Commit

Permalink
Add coverage to elbv2 methods exposed in interface
Browse files Browse the repository at this point in the history
- Ensure mockgen is installed in mocks step
- Rename CreateLoadBalancerInput to CreateLoadBalancerParameters to avoid
  confusion between our API and the AWS SDK's API
- Rename CreateListenerInput to CreateListenerParameters for the same reason
- Remove unused and largely useless StateReason attribute on LoadBalancer
- Add godoc
  • Loading branch information
jpignata committed Feb 27, 2018
1 parent a390913 commit 8628ed5
Show file tree
Hide file tree
Showing 18 changed files with 513 additions and 105 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
PACKAGES := $(shell go list ./... | grep -v /mock)

mocks:
go get github.com/golang/mock/mockgen
go generate $(PACKAGES)

test:
Expand Down
3 changes: 1 addition & 2 deletions cmd/lb_alias_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ var (
VPCID: "vpc-3ac0fb5f",
Name: "web",
SecurityGroupIDs: []string{"sg-5943793c"},
State: "active",
StateReason: "",
Status: "active",
SubnetIDs: []string{"subnet-8360a9e7"},
Type: "application",
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/lb_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (o lbCreateOperation) execute() {
defaultTargetGroupName := fmt.Sprintf(defaultTargetGroupFormat, o.lbName)

loadBalancerARN, err := o.elbv2.CreateLoadBalancer(
elbv2.CreateLoadBalancerInput{
elbv2.CreateLoadBalancerParameters{
Name: o.lbName,
SecurityGroupIDs: o.securityGroupIDs,
SubnetIDs: o.subnetIDs,
Expand All @@ -140,7 +140,7 @@ func (o lbCreateOperation) execute() {

o.output.Debug("Creating target group [Name=%s]", defaultTargetGroupName)
defaultTargetGroupARN, err := o.elbv2.CreateTargetGroup(
elbv2.CreateTargetGroupInput{
elbv2.CreateTargetGroupParameters{
Name: defaultTargetGroupName,
Port: o.ports[0].Number,
Protocol: o.ports[0].Protocol,
Expand All @@ -158,7 +158,7 @@ func (o lbCreateOperation) execute() {
for _, port := range o.ports {
o.output.Debug("Creating listener [Port=%d Protocol=%s]", port.Number, port.Protocol)
listenerARN, err := o.elbv2.CreateListener(
elbv2.CreateListenerInput{
elbv2.CreateListenerParameters{
CertificateARNs: o.certificateARNs,
DefaultTargetGroupARN: defaultTargetGroupARN,
LoadBalancerARN: loadBalancerARN,
Expand Down
16 changes: 8 additions & 8 deletions cmd/lb_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,19 @@ func TestLBCreateOperation(t *testing.T) {
mockEC2Client := ec2client.NewMockClient(mockCtrl)
mockOutput := &mock.Output{}

createLoadBalancerInput := elbv2.CreateLoadBalancerInput{
createLoadBalancerInput := elbv2.CreateLoadBalancerParameters{
Name: lbName,
SecurityGroupIDs: securityGroupIDs,
SubnetIDs: subnetIDs,
Type: lbType,
}
createTargetGroupInput := elbv2.CreateTargetGroupInput{
createTargetGroupInput := elbv2.CreateTargetGroupParameters{
Name: "lb-default",
Port: 80,
Protocol: "HTTP",
VPCID: vpcID,
}
createListenerInput := elbv2.CreateListenerInput{
createListenerInput := elbv2.CreateListenerParameters{
DefaultTargetGroupARN: tgARN,
LoadBalancerARN: lbARN,
Port: 80,
Expand Down Expand Up @@ -144,13 +144,13 @@ func TestLBCreateOperationTargetGroupError(t *testing.T) {
mockEC2Client := ec2client.NewMockClient(mockCtrl)
mockOutput := &mock.Output{}

createLoadBalancerInput := elbv2.CreateLoadBalancerInput{
createLoadBalancerInput := elbv2.CreateLoadBalancerParameters{
Name: lbName,
SecurityGroupIDs: securityGroupIDs,
SubnetIDs: subnetIDs,
Type: lbType,
}
createTargetGroupInput := elbv2.CreateTargetGroupInput{
createTargetGroupInput := elbv2.CreateTargetGroupParameters{
Name: "lb-default",
Port: 80,
Protocol: "HTTP",
Expand Down Expand Up @@ -205,19 +205,19 @@ func TestLBCreateOperationListenerError(t *testing.T) {
mockEC2Client := ec2client.NewMockClient(mockCtrl)
mockOutput := &mock.Output{}

createLoadBalancerInput := elbv2.CreateLoadBalancerInput{
createLoadBalancerInput := elbv2.CreateLoadBalancerParameters{
Name: lbName,
SecurityGroupIDs: securityGroupIDs,
SubnetIDs: subnetIDs,
Type: lbType,
}
createTargetGroupInput := elbv2.CreateTargetGroupInput{
createTargetGroupInput := elbv2.CreateTargetGroupParameters{
Name: "lb-default",
Port: 80,
Protocol: "HTTP",
VPCID: vpcID,
}
createListenerInput := elbv2.CreateListenerInput{
createListenerInput := elbv2.CreateListenerParameters{
DefaultTargetGroupARN: tgARN,
LoadBalancerARN: lbARN,
Port: 80,
Expand Down
2 changes: 1 addition & 1 deletion cmd/lb_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func getLoadBalancerInfo(operation *LbInfoOperation) {
services := ecs.ListServices()

console.KeyValue("Load Balancer Name", "%s\n", loadBalancer.Name)
console.KeyValue("Status", "%s\n", Humanize(loadBalancer.State))
console.KeyValue("Status", "%s\n", Humanize(loadBalancer.Status))
console.KeyValue("Type", "%s\n", Humanize(loadBalancer.Type))
console.KeyValue("DNS Name", "%s\n", loadBalancer.DNSName)
console.KeyValue("Subnets", "%s\n", strings.Join(loadBalancer.SubnetIDs, ", "))
Expand Down
2 changes: 1 addition & 1 deletion cmd/lb_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (o lbListOperation) execute() {
[]string{
loadBalancer.Name,
Titleize(loadBalancer.Type),
Titleize(loadBalancer.State),
Titleize(loadBalancer.Status),
loadBalancer.DNSName,
fmt.Sprintf("%s", loadBalancer.Listeners),
},
Expand Down
8 changes: 4 additions & 4 deletions cmd/lb_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ func TestLBListOperation(t *testing.T) {
DNSName: "test-12345678.us-east-1.elb.amazonaws.com",
Name: "test",
Type: "application",
State: "active",
Status: "active",
}
loadBalancer2 := elbv2.LoadBalancer{
ARN: "arn:aws:elasticloadbalancing:us-east-1:123456789012:loadbalancer/app/lb/93fa3d386bec918a",
DNSName: "test-abcdef.us-east-1.elb.amazonaws.com",
Name: "test2",
Type: "application",
State: "active",
Status: "active",
}
listener1 := elbv2.Listener{
ARN: "arn:aws:elasticloadbalancing:us-east-1:123456789012:listener/app/my-load-balancer/50dc6c495c0c9188/f2f7dc8efc522ab2",
Expand Down Expand Up @@ -77,7 +77,7 @@ func TestLBListOperation(t *testing.T) {
t.Errorf("expected type: %s, got: %s", expected, row1[1])
}

if expected := Titleize(loadBalancer1.State); row1[2] != expected {
if expected := Titleize(loadBalancer1.Status); row1[2] != expected {
t.Errorf("expected status: %s, got: %s", expected, row1[2])
}

Expand All @@ -99,7 +99,7 @@ func TestLBListOperation(t *testing.T) {
t.Errorf("expected type: %s, got: %s", expected, row2[1])
}

if expected := Titleize(loadBalancer2.State); row2[2] != expected {
if expected := Titleize(loadBalancer2.Status); row2[2] != expected {
t.Errorf("expected status: %s, got: %s", expected, row2[2])
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/service_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ func createService(operation *ServiceCreateOperation) {
if operation.LoadBalancerArn != "" {
vpcId, _ := ec2.GetSubnetVPCID(operation.SubnetIds[0])
targetGroupArn, _ = elbv2.CreateTargetGroup(
ELBV2.CreateTargetGroupInput{
ELBV2.CreateTargetGroupParameters{
Name: fmt.Sprintf("%s-%s", clusterName, operation.ServiceName),
Port: operation.Port.Number,
Protocol: operation.Port.Protocol,
Expand Down
2 changes: 1 addition & 1 deletion cmd/service_destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func destroyService(operation *ServiceDestroyOperation) {

if defaultTargetGroupArn == "" {
defaultTargetGroupArn, _ = elbv2.CreateTargetGroup(
ELBV2.CreateTargetGroupInput{
ELBV2.CreateTargetGroupParameters{
Name: defaultTargetGroupName,
Port: listeners[0].Port,
Protocol: listeners[0].Protocol,
Expand Down
88 changes: 49 additions & 39 deletions elbv2/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/jpignata/fargate/console"
)

// Listener accepts incoming traffic on a load balancer based upon the provided routing rules.
type Listener struct {
ARN string
CertificateARNs []string
Expand All @@ -19,12 +20,15 @@ type Listener struct {
Rules []Rule
}

// String returns a friendly representation of the listener.
func (l Listener) String() string {
return fmt.Sprintf("%s:%d", l.Protocol, l.Port)
}

// Listeners is a collection of listeners.
type Listeners []Listener

// Listeners returns a comma-separated friendly representation of the listeners.
func (l Listeners) String() string {
var listenerStrings []string

Expand All @@ -35,6 +39,7 @@ func (l Listeners) String() string {
return strings.Join(listenerStrings, ", ")
}

// Rule defines a routing rule defining how traffic should be routed to a listener.
type Rule struct {
ARN string
IsDefault bool
Expand All @@ -44,22 +49,63 @@ type Rule struct {
Value string
}

func (r *Rule) String() string {
// String returns a friendly representation of a rule.
func (r Rule) String() string {
return strings.Join([]string{r.Type, r.Value}, "=")
}

type CreateListenerInput struct {
// CreateListenerParameters are the parameters required to create a new listener.
type CreateListenerParameters struct {
CertificateARNs []string
DefaultTargetGroupARN string
LoadBalancerARN string
Port int64
Protocol string
}

func (input *CreateListenerInput) SetCertificateArns(arns []string) {
// SetCertificateARNs sets the certificate ARNs with the given ARNs.
func (input *CreateListenerParameters) SetCertificateARNs(arns []string) {
input.CertificateARNs = arns
}

// CreateListener creates a new listener and returns the listener ARN if successfully created.
func (elbv2 SDKClient) CreateListener(p CreateListenerParameters) (string, error) {
action := &awselbv2.Action{
TargetGroupArn: aws.String(p.DefaultTargetGroupARN),
Type: aws.String(awselbv2.ActionTypeEnumForward),
}

i := &awselbv2.CreateListenerInput{
Port: aws.Int64(p.Port),
Protocol: aws.String(p.Protocol),
LoadBalancerArn: aws.String(p.LoadBalancerARN),
DefaultActions: []*awselbv2.Action{action},
}

if len(p.CertificateARNs) > 0 {
certificates := []*awselbv2.Certificate{}

for _, certificateARN := range p.CertificateARNs {
certificates = append(certificates,
&awselbv2.Certificate{
CertificateArn: aws.String(certificateARN),
},
)
}

i.SetCertificates(certificates)
}

resp, err := elbv2.client.CreateListener(i)

if err != nil {
return "", err
}

return aws.StringValue(resp.Listeners[0].ListenerArn), nil
}

// DescribeListeners returns all of the listeners for a given load balancer ARN.
func (elbv2 SDKClient) DescribeListeners(lbARN string) (Listeners, error) {
var listeners []Listener

Expand Down Expand Up @@ -91,42 +137,6 @@ func (elbv2 SDKClient) DescribeListeners(lbARN string) (Listeners, error) {
return listeners, err
}

func (elbv2 SDKClient) CreateListener(i CreateListenerInput) (string, error) {
action := &awselbv2.Action{
TargetGroupArn: aws.String(i.DefaultTargetGroupARN),
Type: aws.String(awselbv2.ActionTypeEnumForward),
}

sdki := &awselbv2.CreateListenerInput{
Port: aws.Int64(i.Port),
Protocol: aws.String(i.Protocol),
LoadBalancerArn: aws.String(i.LoadBalancerARN),
DefaultActions: []*awselbv2.Action{action},
}

if len(i.CertificateARNs) > 0 {
certificates := []*awselbv2.Certificate{}

for _, certificateARN := range i.CertificateARNs {
certificates = append(certificates,
&awselbv2.Certificate{
CertificateArn: aws.String(certificateARN),
},
)
}

sdki.SetCertificates(certificates)
}

resp, err := elbv2.client.CreateListener(sdki)

if err != nil {
return "", err
}

return aws.StringValue(resp.Listeners[0].ListenerArn), nil
}

func (elbv2 SDKClient) ModifyLoadBalancerDefaultAction(lbARN, targetGroupARN string) {
for _, listener := range elbv2.GetListeners(lbARN) {
elbv2.ModifyListenerDefaultAction(listener.ARN, targetGroupARN)
Expand Down
Loading

0 comments on commit 8628ed5

Please sign in to comment.