From aa9c28a7b9cd36699e9f3d74e7cb01437437640e Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 9 Feb 2023 11:01:22 -0500 Subject: [PATCH] r/aws_elb: Fix sweeper. --- internal/service/elb/sweep.go | 52 +++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/internal/service/elb/sweep.go b/internal/service/elb/sweep.go index 1997c0a98f9..137829cc761 100644 --- a/internal/service/elb/sweep.go +++ b/internal/service/elb/sweep.go @@ -7,6 +7,7 @@ import ( "fmt" "log" + "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/elb" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-provider-aws/internal/conns" @@ -24,39 +25,42 @@ func sweepLoadBalancers(region string) error { ctx := sweep.Context(region) client, err := sweep.SharedRegionalSweepClient(region) if err != nil { - return fmt.Errorf("getting client: %s", err) + return fmt.Errorf("error getting client: %s", err) } conn := client.(*conns.AWSClient).ELBConn() + input := &elb.DescribeLoadBalancersInput{} + sweepResources := make([]sweep.Sweepable, 0) - err = conn.DescribeLoadBalancersPagesWithContext(ctx, &elb.DescribeLoadBalancersInput{}, func(out *elb.DescribeLoadBalancersOutput, lastPage bool) bool { - if len(out.LoadBalancerDescriptions) == 0 { - log.Println("[INFO] No ELBs found for sweeping") - return false + err = conn.DescribeLoadBalancersPagesWithContext(ctx, input, func(page *elb.DescribeLoadBalancersOutput, lastPage bool) bool { + if page == nil { + return !lastPage } - for _, lb := range out.LoadBalancerDescriptions { - log.Printf("[INFO] Deleting ELB: %s", *lb.LoadBalancerName) - - _, err := conn.DeleteLoadBalancerWithContext(ctx, &elb.DeleteLoadBalancerInput{ - LoadBalancerName: lb.LoadBalancerName, - }) - if err != nil { - log.Printf("[ERROR] Failed to delete ELB %s: %s", *lb.LoadBalancerName, err) - continue - } - err = CleanupNetworkInterfaces(ctx, client.(*conns.AWSClient).EC2Conn(), *lb.LoadBalancerName) - if err != nil { - log.Printf("[WARN] Failed to cleanup ENIs for ELB %q: %s", *lb.LoadBalancerName, err) - } + for _, v := range page.LoadBalancerDescriptions { + r := ResourceLoadBalancer() + d := r.Data(nil) + d.SetId(aws.StringValue(v.LoadBalancerName)) + + sweepResources = append(sweepResources, sweep.NewSweepResource(r, d, client)) } + return !lastPage }) + + if sweep.SkipSweepError(err) { + log.Printf("[WARN] Skipping ELB Classic Load Balancer sweep for %s: %s", region, err) + return nil + } + if err != nil { - if sweep.SkipSweepError(err) { - log.Printf("[WARN] Skipping ELB sweep for %s: %s", region, err) - return nil - } - return fmt.Errorf("Error retrieving ELBs: %s", err) + return fmt.Errorf("error listing ELB Classic Load Balancers (%s): %w", region, err) } + + err = sweep.SweepOrchestratorWithContext(ctx, sweepResources) + + if err != nil { + return fmt.Errorf("error sweeping ELB Classic Load Balancers (%s): %w", region, err) + } + return nil }