Skip to content

Commit

Permalink
Merge pull request #4 from oreillymedia/CL-550
Browse files Browse the repository at this point in the history
Cl 550 | Adjust cloudcontrol behavior in aws-nuke to properly handle ThrottlingExceptions
  • Loading branch information
corybekk authored Jun 2, 2023
2 parents 0b661ad + 1c22311 commit cfa9c9e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
21 changes: 18 additions & 3 deletions cmd/nuke.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ func (n *Nuke) Run() error {
return err
}

if n.items.Count(ItemStateFailed) > 0 && n.items.Count(ItemStateNew) == 0 {
for _, item := range n.items {
if item.State != ItemStateFailed {
continue
}
logrus.Error(fmt.Sprintf("%s. %s.", item.Type, item.Reason))
}
return fmt.Errorf("failed")
}

if n.items.Count(ItemStateNew) == 0 {
fmt.Println("No resource to delete.")
return nil
Expand Down Expand Up @@ -249,9 +259,14 @@ func (n *Nuke) HandleQueue() {
n.HandleRemove(item)
item.Print()
case ItemStateFailed:
n.HandleRemove(item)
n.HandleWait(item, listCache)
item.Print()
// item.Resource will be nil if an exception was thrown while retrieving cloudControl
// resourceType's items (I.E resourceTypes lister()), however we still pass down the
// reason and state so we aren't ignoring these exceptions.
if item.Resource != nil {
n.HandleRemove(item)
n.HandleWait(item, listCache)
item.Print()
}
case ItemStatePending:
n.HandleWait(item, listCache)
item.State = ItemStateWaiting
Expand Down
17 changes: 16 additions & 1 deletion cmd/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import (
"fmt"
"runtime/debug"

"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/rebuy-de/aws-nuke/v2/pkg/awsutil"
"github.com/rebuy-de/aws-nuke/v2/pkg/util"
"github.com/rebuy-de/aws-nuke/v2/resources"
log "github.com/sirupsen/logrus"
"golang.org/x/sync/semaphore"
)

const ScannerParallelQueries = 2
const ScannerParallelQueries = 16

func Scan(region *Region, resourceTypes []string) <-chan *Item {
s := &scanner{
Expand Down Expand Up @@ -72,6 +73,20 @@ func (s *scanner) list(region *Region, resourceType string) {
return
}

awsErr, ok := err.(awserr.Error)
if ok && awsErr.Code() == "ThrottlingException" {
s.items <- &Item{
Region: region,
Resource: nil,
State: ItemStateFailed,
Reason: err.Error(),
Type: resourceType,
}
dump := util.Indent(fmt.Sprintf("%v", err), " ")
log.Errorf("Listing %s failed:\n%s", resourceType, dump)
return
}

dump := util.Indent(fmt.Sprintf("%v", err), " ")
log.Errorf("Listing %s failed:\n%s", resourceType, dump)
return
Expand Down
4 changes: 3 additions & 1 deletion resources/cloudcontrol.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ func init() {
registerCloudControl("AWS::NetworkFirewall::RuleGroup")
}

const CloudControlAPiMaxRetries = 5

func NewListCloudControlResource(typeName string) func(*session.Session) ([]Resource, error) {
return func(sess *session.Session) ([]Resource, error) {
svc := cloudcontrolapi.New(sess)
svc := cloudcontrolapi.New(sess, &aws.Config{MaxRetries: aws.Int(CloudControlAPiMaxRetries)})

params := &cloudcontrolapi.ListResourcesInput{
TypeName: aws.String(typeName),
Expand Down

0 comments on commit cfa9c9e

Please sign in to comment.