Skip to content

Commit

Permalink
Merge pull request #28354 from hashicorp/s-aws_vpc
Browse files Browse the repository at this point in the history
Sweepers: Various fixes
  • Loading branch information
ewbankkit authored Dec 14, 2022
2 parents 0160b5d + b085efa commit 6b50b56
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 38 deletions.
2 changes: 1 addition & 1 deletion internal/service/ec2/ec2_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -2794,7 +2794,7 @@ func StopInstance(conn *ec2.EC2, id string, timeout time.Duration) error {

// terminateInstance shuts down an EC2 instance and waits for the instance to be deleted.
func terminateInstance(conn *ec2.EC2, id string, timeout time.Duration) error {
log.Printf("[INFO] Terminating EC2 Instance: %s", id)
log.Printf("[DEBUG] Terminating EC2 Instance: %s", id)
_, err := conn.TerminateInstances(&ec2.TerminateInstancesInput{
InstanceIds: aws.StringSlice([]string{id}),
})
Expand Down
3 changes: 1 addition & 2 deletions internal/service/ec2/ec2_spot_instance_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,9 +413,8 @@ func resourceSpotInstanceRequestDelete(d *schema.ResourceData, meta interface{})
}

if instanceId := d.Get("spot_instance_id").(string); instanceId != "" {
log.Printf("[INFO] Terminating instance: %s", instanceId)
if err := terminateInstance(conn, instanceId, d.Timeout(schema.TimeoutDelete)); err != nil {
return fmt.Errorf("Error terminating spot instance: %s", err)
return err
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/service/ec2/sweep.go
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,7 @@ func sweepInstances(region string) error {
r := ResourceInstance()
d := r.Data(nil)
d.SetId(id)
d.Set("disable_api_termination", false)
d.Set("disable_api_stop", false)

sweepResources = append(sweepResources, sweep.NewSweepResource(r, d, client))
}
Expand Down
40 changes: 33 additions & 7 deletions internal/service/eks/sweep.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/eks"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
multierror "github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/sweep"
Expand All @@ -19,7 +20,7 @@ import (
func init() {
resource.AddTestSweepers("aws_eks_addon", &resource.Sweeper{
Name: "aws_eks_addon",
F: sweepAddon,
F: sweepAddons,
})

resource.AddTestSweepers("aws_eks_cluster", &resource.Sweeper{
Expand Down Expand Up @@ -49,7 +50,7 @@ func init() {
})
}

func sweepAddon(region string) error {
func sweepAddons(region string) error {
client, err := sweep.SharedRegionalSweepClient(region)
if err != nil {
return fmt.Errorf("error getting client: %w", err)
Expand All @@ -65,20 +66,21 @@ func sweepAddon(region string) error {
return !lastPage
}

for _, cluster := range page.Clusters {
for _, v := range page.Clusters {
clusterName := aws.StringValue(v)
input := &eks.ListAddonsInput{
ClusterName: cluster,
ClusterName: aws.String(clusterName),
}

err := conn.ListAddonsPagesWithContext(ctx, input, func(page *eks.ListAddonsOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

for _, addon := range page.Addons {
for _, v := range page.Addons {
r := ResourceAddon()
d := r.Data(nil)
d.SetId(AddonCreateResourceID(aws.StringValue(cluster), aws.StringValue(addon)))
d.SetId(AddonCreateResourceID(clusterName, aws.StringValue(v)))

sweepResources = append(sweepResources, sweep.NewSweepResource(r, d, client))
}
Expand All @@ -90,6 +92,12 @@ func sweepAddon(region string) error {
continue
}

// There are EKS clusters that are listed (and are in the AWS Console) but can't be found.
// ¯\_(ツ)_/¯
if tfawserr.ErrCodeEquals(err, eks.ErrCodeResourceNotFoundException) {
continue
}

if err != nil {
sweeperErrs = multierror.Append(sweeperErrs, fmt.Errorf("error listing EKS Add-Ons (%s): %w", region, err))
}
Expand Down Expand Up @@ -199,6 +207,12 @@ func sweepFargateProfiles(region string) error {
continue
}

// There are EKS clusters that are listed (and are in the AWS Console) but can't be found.
// ¯\_(ツ)_/¯
if tfawserr.ErrCodeEquals(err, eks.ErrCodeResourceNotFoundException) {
continue
}

if err != nil {
sweeperErrs = multierror.Append(sweeperErrs, fmt.Errorf("error listing EKS Fargate Profiles (%s): %w", region, err))
}
Expand Down Expand Up @@ -266,6 +280,12 @@ func sweepIdentityProvidersConfig(region string) error {
continue
}

// There are EKS clusters that are listed (and are in the AWS Console) but can't be found.
// ¯\_(ツ)_/¯
if tfawserr.ErrCodeEquals(err, eks.ErrCodeResourceNotFoundException) {
continue
}

if err != nil {
sweeperErrs = multierror.Append(sweeperErrs, fmt.Errorf("error listing EKS Identity Provider Configs (%s): %w", region, err))
}
Expand Down Expand Up @@ -332,6 +352,12 @@ func sweepNodeGroups(region string) error {
continue
}

// There are EKS clusters that are listed (and are in the AWS Console) but can't be found.
// ¯\_(ツ)_/¯
if tfawserr.ErrCodeEquals(err, eks.ErrCodeResourceNotFoundException) {
continue
}

if err != nil {
sweeperErrs = multierror.Append(sweeperErrs, fmt.Errorf("error listing EKS Node Groups (%s): %w", region, err))
}
Expand Down
5 changes: 5 additions & 0 deletions internal/service/elasticache/sweep.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,11 @@ func sweepSubnetGroups(region string) error {
for _, subnetGroup := range page.CacheSubnetGroups {
name := aws.StringValue(subnetGroup.CacheSubnetGroupName)

if name == "default" {
log.Printf("[INFO] Skipping ElastiCache Subnet Group: %s", name)
continue
}

log.Printf("[INFO] Deleting ElastiCache Subnet Group: %s", name)
_, err := conn.DeleteCacheSubnetGroup(&elasticache.DeleteCacheSubnetGroupInput{
CacheSubnetGroupName: aws.String(name),
Expand Down
2 changes: 1 addition & 1 deletion internal/service/emr/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -1402,7 +1402,7 @@ func resourceClusterUpdate(d *schema.ResourceData, meta interface{}) error {
func resourceClusterDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).EMRConn

log.Printf("[DEBUG] Deleting EMR Cluster: (%s)", d.Id())
log.Printf("[DEBUG] Deleting EMR Cluster: %s", d.Id())
_, err := conn.TerminateJobFlows(&emr.TerminateJobFlowsInput{
JobFlowIds: []*string{
aws.String(d.Id()),
Expand Down
54 changes: 28 additions & 26 deletions internal/service/emr/sweep.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,49 +33,51 @@ func sweepClusters(region string) error {
return fmt.Errorf("error getting client: %w", err)
}
conn := client.(*conns.AWSClient).EMRConn

input := &emr.ListClustersInput{
ClusterStates: []*string{
aws.String(emr.ClusterStateBootstrapping),
aws.String(emr.ClusterStateRunning),
aws.String(emr.ClusterStateStarting),
aws.String(emr.ClusterStateWaiting),
},
ClusterStates: aws.StringSlice([]string{emr.ClusterStateBootstrapping, emr.ClusterStateRunning, emr.ClusterStateStarting, emr.ClusterStateWaiting}),
}
sweepResources := make([]sweep.Sweepable, 0)

err = conn.ListClustersPages(input, func(page *emr.ListClustersOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

for _, cluster := range page.Clusters {
describeClusterInput := &emr.DescribeClusterInput{
ClusterId: cluster.Id,
}
terminateJobFlowsInput := &emr.TerminateJobFlowsInput{
JobFlowIds: []*string{cluster.Id},
}
id := aws.StringValue(cluster.Id)
for _, v := range page.Clusters {
id := aws.StringValue(v.Id)

log.Printf("[INFO] Deleting EMR Cluster: %s", id)
_, err = conn.TerminateJobFlows(terminateJobFlowsInput)
_, err := conn.SetTerminationProtection(&emr.SetTerminationProtectionInput{
JobFlowIds: aws.StringSlice([]string{id}),
TerminationProtected: aws.Bool(false),
})

if err != nil {
log.Printf("[ERROR] Error terminating EMR Cluster (%s): %s", id, err)
log.Printf("[ERROR] unsetting EMR Cluster (%s) termination protection: %s", id, err)
}

if err := conn.WaitUntilClusterTerminated(describeClusterInput); err != nil {
log.Printf("[ERROR] Error waiting for EMR Cluster (%s) termination: %s", id, err)
}
r := ResourceCluster()
d := r.Data(nil)
d.SetId(id)

sweepResources = append(sweepResources, sweep.NewSweepResource(r, d, client))
}

return !lastPage
})

if sweep.SkipSweepError(err) {
log.Printf("[WARN] Skipping EMR Clusters sweep for %s: %s", region, err)
return nil
}

if err != nil {
if sweep.SkipSweepError(err) {
log.Printf("[WARN] Skipping EMR Cluster sweep for %s: %s", region, err)
return nil
}
return fmt.Errorf("error retrieving EMR Clusters: %w", err)
return fmt.Errorf("error listing EMR Clusters (%s): %w", region, err)
}

err = sweep.SweepOrchestrator(sweepResources)

if err != nil {
return fmt.Errorf("error sweeping EMR Clusters (%s): %w", region, err)
}

return nil
Expand Down

0 comments on commit 6b50b56

Please sign in to comment.