Skip to content

Commit

Permalink
cloudmock: Implement WithContext methods for ELBv2
Browse files Browse the repository at this point in the history
Also switch methods that were not passing a context.
  • Loading branch information
justinsb committed Feb 4, 2024
1 parent c35c754 commit c9b9a47
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
22 changes: 12 additions & 10 deletions cloudmock/aws/mockelbv2/listeners.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,39 +21,41 @@ import (
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/service/elbv2"
"k8s.io/klog/v2"
)

func (m *MockELBV2) DescribeListeners(request *elbv2.DescribeListenersInput) (*elbv2.DescribeListenersOutput, error) {
func (m *MockELBV2) DescribeListenersPagesWithContext(ctx aws.Context, request *elbv2.DescribeListenersInput, callback func(*elbv2.DescribeListenersOutput, bool) bool, options ...request.Option) error {
m.mutex.Lock()
defer m.mutex.Unlock()

klog.Infof("DescribeListeners v2 %v", request)
klog.Infof("DescribeListenersPagesWithContext v2 %v", request)

resp := &elbv2.DescribeListenersOutput{
page := &elbv2.DescribeListenersOutput{
Listeners: make([]*elbv2.Listener, 0),
}
for _, l := range m.Listeners {
listener := l.description
if aws.StringValue(request.LoadBalancerArn) == aws.StringValue(listener.LoadBalancerArn) {
resp.Listeners = append(resp.Listeners, &listener)
page.Listeners = append(page.Listeners, &listener)
} else {
for _, reqARN := range request.ListenerArns {
if aws.StringValue(reqARN) == aws.StringValue(listener.ListenerArn) {
resp.Listeners = append(resp.Listeners, &listener)
page.Listeners = append(page.Listeners, &listener)
}
}
}
}
return resp, nil
callback(page, true)
return nil
}

func (m *MockELBV2) CreateListener(request *elbv2.CreateListenerInput) (*elbv2.CreateListenerOutput, error) {
func (m *MockELBV2) CreateListenerWithContext(ctx aws.Context, request *elbv2.CreateListenerInput, opts ...request.Option) (*elbv2.CreateListenerOutput, error) {
m.mutex.Lock()
defer m.mutex.Unlock()

klog.Infof("CreateListener v2 %v", request)
klog.Infof("CreateListenerWithContext v2 %v", request)

l := elbv2.Listener{
DefaultActions: request.DefaultActions,
Expand Down Expand Up @@ -96,11 +98,11 @@ func (m *MockELBV2) CreateListener(request *elbv2.CreateListenerInput) (*elbv2.C
return &elbv2.CreateListenerOutput{Listeners: []*elbv2.Listener{&l}}, nil
}

func (m *MockELBV2) DeleteListener(request *elbv2.DeleteListenerInput) (*elbv2.DeleteListenerOutput, error) {
func (m *MockELBV2) DeleteListenerWithContext(ctx aws.Context, request *elbv2.DeleteListenerInput, opts ...request.Option) (*elbv2.DeleteListenerOutput, error) {
m.mutex.Lock()
defer m.mutex.Unlock()

klog.Infof("DeleteListener v2 %v", request)
klog.Infof("DeleteListenerWithContext v2 %v", request)

lARN := aws.StringValue(request.ListenerArn)
if _, ok := m.Listeners[lARN]; !ok {
Expand Down
12 changes: 8 additions & 4 deletions upup/pkg/fi/cloudup/awstasks/network_load_balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ func (e *NetworkLoadBalancer) getHostedZoneId() *string {
}

func (e *NetworkLoadBalancer) Find(c *fi.CloudupContext) (*NetworkLoadBalancer, error) {
ctx := c.Context()
cloud := c.T.Cloud.(awsup.AWSCloud)

lb, err := cloud.FindELBV2ByNameTag(e.Tags["Name"])
Expand Down Expand Up @@ -260,13 +261,16 @@ func (e *NetworkLoadBalancer) Find(c *fi.CloudupContext) (*NetworkLoadBalancer,
request := &elbv2.DescribeListenersInput{
LoadBalancerArn: loadBalancerArn,
}
response, err := cloud.ELBV2().DescribeListeners(request)
if err != nil {
return nil, fmt.Errorf("error querying for NLB listeners :%v", err)
var listeners []*elbv2.Listener
if err := cloud.ELBV2().DescribeListenersPagesWithContext(ctx, request, func(page *elbv2.DescribeListenersOutput, lastPage bool) bool {
listeners = append(listeners, page.Listeners...)
return true
}); err != nil {
return nil, fmt.Errorf("listing NLB listeners: %w", err)
}

actual.TargetGroups = []*TargetGroup{}
for _, l := range response.Listeners {
for _, l := range listeners {
// This will need to be rearranged when we recognized multiple listeners and target groups per NLB
if len(l.DefaultActions) > 0 {
targetGroupARN := l.DefaultActions[0].TargetGroupArn
Expand Down

0 comments on commit c9b9a47

Please sign in to comment.