Skip to content

Commit

Permalink
Merge pull request #38144 from hashicorp/td-m2-sweepers
Browse files Browse the repository at this point in the history
Adds sweepers for M2
  • Loading branch information
gdavison authored Jun 27, 2024
2 parents de37754 + bc7c539 commit 6d62972
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 4 deletions.
1 change: 1 addition & 0 deletions internal/service/efs/sweep.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func RegisterSweepers() {
Dependencies: []string{
"aws_efs_mount_target",
"aws_efs_access_point",
"aws_m2_environment",
},
})

Expand Down
4 changes: 4 additions & 0 deletions internal/service/fsx/sweep.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func RegisterSweepers() {
F: sweepLustreFileSystems,
Dependencies: []string{
"aws_datasync_location",
"aws_m2_environment",
},
})

Expand All @@ -35,6 +36,7 @@ func RegisterSweepers() {
Dependencies: []string{
"aws_datasync_location",
"aws_fsx_ontap_storage_virtual_machine",
"aws_m2_environment",
},
})

Expand All @@ -57,6 +59,7 @@ func RegisterSweepers() {
Dependencies: []string{
"aws_datasync_location",
"aws_fsx_openzfs_volume",
"aws_m2_environment",
},
})

Expand All @@ -70,6 +73,7 @@ func RegisterSweepers() {
F: sweepWindowsFileSystems,
Dependencies: []string{
"aws_datasync_location",
"aws_m2_environment",
"aws_storagegateway_file_system_association",
},
})
Expand Down
10 changes: 6 additions & 4 deletions internal/service/m2/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,10 +568,12 @@ func waitEnvironmentUpdated(ctx context.Context, conn *m2.Client, id string, tim

func waitEnvironmentDeleted(ctx context.Context, conn *m2.Client, id string, timeout time.Duration) (*m2.GetEnvironmentOutput, error) {
stateConf := &retry.StateChangeConf{
Pending: enum.Slice(awstypes.EnvironmentLifecycleAvailable, awstypes.EnvironmentLifecycleCreating, awstypes.EnvironmentLifecycleDeleting),
Target: []string{},
Refresh: statusEnvironment(ctx, conn, id),
Timeout: timeout,
Pending: enum.Slice(awstypes.EnvironmentLifecycleAvailable, awstypes.EnvironmentLifecycleCreating, awstypes.EnvironmentLifecycleDeleting),
Target: []string{},
Refresh: statusEnvironment(ctx, conn, id),
Timeout: timeout,
Delay: 4 * time.Minute,
MinTimeout: 10 * time.Second,
}

outputRaw, err := stateConf.WaitForStateContext(ctx)
Expand Down
77 changes: 77 additions & 0 deletions internal/service/m2/sweep.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package m2

import (
"context"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/m2"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/sweep"
"github.com/hashicorp/terraform-provider-aws/internal/sweep/awsv2"
"github.com/hashicorp/terraform-provider-aws/internal/sweep/framework"
"github.com/hashicorp/terraform-provider-aws/names"
)

func RegisterSweepers() {
sweep.Register("aws_m2_application", sweepApplications)

sweep.Register("aws_m2_environment", sweepEnvironments)
}

func sweepApplications(ctx context.Context, client *conns.AWSClient) ([]sweep.Sweepable, error) {
conn := client.M2Client(ctx)

var sweepResources []sweep.Sweepable

pages := m2.NewListApplicationsPaginator(conn, &m2.ListApplicationsInput{})
for pages.HasMorePages() {
page, err := pages.NextPage(ctx)
if awsv2.SkipSweepError(err) {
tflog.Warn(ctx, "Skipping sweeper", map[string]any{
"error": err.Error(),
})
return nil, nil
}
if err != nil {
return nil, err
}

for _, application := range page.Applications {
sweepResources = append(sweepResources, framework.NewSweepResource(newApplicationResource, client,
framework.NewAttribute(names.AttrID, aws.ToString(application.ApplicationId))))
}
}

return sweepResources, nil
}

func sweepEnvironments(ctx context.Context, client *conns.AWSClient) ([]sweep.Sweepable, error) {
conn := client.M2Client(ctx)

var sweepResources []sweep.Sweepable

pages := m2.NewListEnvironmentsPaginator(conn, &m2.ListEnvironmentsInput{})
for pages.HasMorePages() {
page, err := pages.NextPage(ctx)
if awsv2.SkipSweepError(err) {
tflog.Warn(ctx, "Skipping sweeper", map[string]any{
"error": err.Error(),
})
return nil, nil
}
if err != nil {
return nil, err
}

for _, environment := range page.Environments {
sweepResources = append(sweepResources, framework.NewSweepResource(newEnvironmentResource, client,
framework.NewAttribute(names.AttrID, aws.ToString(environment.EnvironmentId))))
}
}

return sweepResources, nil
}
3 changes: 3 additions & 0 deletions internal/service/s3/sweep.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ func RegisterSweepers() {
resource.AddTestSweepers("aws_s3_object", &resource.Sweeper{
Name: "aws_s3_object",
F: sweepObjects,
Dependencies: []string{
"aws_m2_application",
},
})

resource.AddTestSweepers("aws_s3_bucket", &resource.Sweeper{
Expand Down
2 changes: 2 additions & 0 deletions internal/sweep/register_gen_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6d62972

Please sign in to comment.