Skip to content

Commit

Permalink
r/aws_elb: Fix sweeper.
Browse files Browse the repository at this point in the history
  • Loading branch information
ewbankkit committed Feb 9, 2023
1 parent 4da4cb9 commit aa9c28a
Showing 1 changed file with 28 additions and 24 deletions.
52 changes: 28 additions & 24 deletions internal/service/elb/sweep.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}

0 comments on commit aa9c28a

Please sign in to comment.