Skip to content

Commit

Permalink
fsx: Migrate sweepers to AWS SDK v2
Browse files Browse the repository at this point in the history
  • Loading branch information
mattburgess committed Jul 15, 2024
1 parent 8d4fcf6 commit 335a845
Showing 1 changed file with 103 additions and 120 deletions.
223 changes: 103 additions & 120 deletions internal/service/fsx/sweep.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ import (
awstypes "github.com/aws/aws-sdk-go-v2/service/fsx/types"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-provider-aws/internal/errs"
"github.com/hashicorp/terraform-provider-aws/internal/sweep"
"github.com/hashicorp/terraform-provider-aws/internal/sweep/awsv1"
"github.com/hashicorp/terraform-provider-aws/internal/sweep/awsv2"
)

func RegisterSweepers() {
Expand Down Expand Up @@ -94,9 +93,17 @@ func sweepBackups(region string) error {
var errs *multierror.Error
input := &fsx.DescribeBackupsInput{}

err = conn.DescribeBackupsPagesWithContext(ctx, input, func(page *fsx.DescribeBackupsOutput, lastPage bool) bool {
if page == nil {
return !lastPage
pages := fsx.NewDescribeBackupsPaginator(conn, input)

for pages.HasMorePages() {
page, err := pages.NextPage(ctx)

if err != nil {
errs = multierror.Append(errs, fmt.Errorf("error listing FSx Backups for %s: %w", region, err))
}

if err = sweep.SweepOrchestrator(ctx, sweepResources); err != nil {
errs = multierror.Append(errs, fmt.Errorf("error sweeping FSx Backups for %s: %w", region, err))
}

for _, fs := range page.Backups {
Expand All @@ -106,19 +113,9 @@ func sweepBackups(region string) error {

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

return !lastPage
})

if err != nil {
errs = multierror.Append(errs, fmt.Errorf("error listing FSx Backups for %s: %w", region, err))
}

if err = sweep.SweepOrchestrator(ctx, sweepResources); err != nil {
errs = multierror.Append(errs, fmt.Errorf("error sweeping FSx Backups for %s: %w", region, err))
}

if awsv1.SkipSweepError(errs.ErrorOrNil()) {
if awsv2.SkipSweepError(errs.ErrorOrNil()) {
log.Printf("[WARN] Skipping FSx Backups sweep for %s: %s", region, errs)
return nil
}
Expand All @@ -139,13 +136,21 @@ func sweepLustreFileSystems(region string) error {
var errs *multierror.Error
input := &fsx.DescribeFileSystemsInput{}

err = conn.DescribeFileSystemsPagesWithContext(ctx, input, func(page *fsx.DescribeFileSystemsOutput, lastPage bool) bool {
if page == nil {
return !lastPage
pages := fsx.NewDescribeFileSystemsPaginator(conn, input)

for pages.HasMorePages() {
page, err := pages.NextPage(ctx)

if err != nil {
errs = multierror.Append(errs, fmt.Errorf("error listing FSx Lustre File Systems for %s: %w", region, err))
}

if err = sweep.SweepOrchestrator(ctx, sweepResources); err != nil {
errs = multierror.Append(errs, fmt.Errorf("error sweeping FSx Lustre File Systems for %s: %w", region, err))
}

for _, fs := range page.FileSystems {
if string(fs.FileSystemType) != awstypes.FileSystemTypeLustre {
if fs.FileSystemType != awstypes.FileSystemTypeLustre {
continue
}

Expand All @@ -155,19 +160,9 @@ func sweepLustreFileSystems(region string) error {

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

return !lastPage
})

if err != nil {
errs = multierror.Append(errs, fmt.Errorf("error listing FSx Lustre File Systems for %s: %w", region, err))
}

if err = sweep.SweepOrchestrator(ctx, sweepResources); err != nil {
errs = multierror.Append(errs, fmt.Errorf("error sweeping FSx Lustre File Systems for %s: %w", region, err))
}

if awsv1.SkipSweepError(errs.ErrorOrNil()) {
if awsv2.SkipSweepError(errs.ErrorOrNil()) {
log.Printf("[WARN] Skipping FSx Lustre File System sweep for %s: %s", region, errs)
return nil
}
Expand All @@ -188,13 +183,21 @@ func sweepONTAPFileSystems(region string) error {
var errs *multierror.Error
input := &fsx.DescribeFileSystemsInput{}

err = conn.DescribeFileSystemsPagesWithContext(ctx, input, func(page *fsx.DescribeFileSystemsOutput, lastPage bool) bool {
if page == nil {
return !lastPage
pages := fsx.NewDescribeFileSystemsPaginator(conn, input)

for pages.HasMorePages() {
page, err := pages.NextPage(ctx)

if err != nil {
errs = multierror.Append(errs, fmt.Errorf("error listing FSx ONTAP File Systems for %s: %w", region, err))
}

if err = sweep.SweepOrchestrator(ctx, sweepResources); err != nil {
errs = multierror.Append(errs, fmt.Errorf("error sweeping FSx ONTAP File Systems for %s: %w", region, err))
}

for _, fs := range page.FileSystems {
if string(fs.FileSystemType) != awstypes.FileSystemTypeOntap {
if fs.FileSystemType != awstypes.FileSystemTypeOntap {
continue
}

Expand All @@ -204,19 +207,9 @@ func sweepONTAPFileSystems(region string) error {

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

return !lastPage
})

if err != nil {
errs = multierror.Append(errs, fmt.Errorf("error listing FSx ONTAP File Systems for %s: %w", region, err))
}

if err = sweep.SweepOrchestrator(ctx, sweepResources); err != nil {
errs = multierror.Append(errs, fmt.Errorf("error sweeping FSx ONTAP File Systems for %s: %w", region, err))
}

if awsv1.SkipSweepError(errs.ErrorOrNil()) {
if awsv2.SkipSweepError(errs.ErrorOrNil()) {
log.Printf("[WARN] Skipping FSx ONTAP File System sweep for %s: %s", region, errs)
return nil
}
Expand All @@ -237,9 +230,17 @@ func sweepONTAPStorageVirtualMachine(region string) error {
var errs *multierror.Error
input := &fsx.DescribeStorageVirtualMachinesInput{}

err = conn.DescribeStorageVirtualMachinesPagesWithContext(ctx, input, func(page *fsx.DescribeStorageVirtualMachinesOutput, lastPage bool) bool {
if page == nil {
return !lastPage
pages := fsx.NewDescribeStorageVirtualMachinesPaginator(conn, input)

for pages.HasMorePages() {
page, err := pages.NextPage(ctx)

if err != nil {
errs = multierror.Append(errs, fmt.Errorf("error listing FSx ONTAP Storage Virtual Machine for %s: %w", region, err))
}

if err = sweep.SweepOrchestrator(ctx, sweepResources); err != nil {
errs = multierror.Append(errs, fmt.Errorf("error sweeping FSx ONTAP Storage Virtual Machine for %s: %w", region, err))
}

for _, vm := range page.StorageVirtualMachines {
Expand All @@ -249,19 +250,9 @@ func sweepONTAPStorageVirtualMachine(region string) error {

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

return !lastPage
})

if err != nil {
errs = multierror.Append(errs, fmt.Errorf("error listing FSx ONTAP Storage Virtual Machine for %s: %w", region, err))
}

if err = sweep.SweepOrchestrator(ctx, sweepResources); err != nil {
errs = multierror.Append(errs, fmt.Errorf("error sweeping FSx ONTAP Storage Virtual Machine for %s: %w", region, err))
}

if awsv1.SkipSweepError(errs.ErrorOrNil()) {
if awsv2.SkipSweepError(errs.ErrorOrNil()) {
log.Printf("[WARN] Skipping FSx ONTAP Storage Virtual Machine sweep for %s: %s", region, errs)
return nil
}
Expand All @@ -282,13 +273,21 @@ func sweepONTAPVolumes(region string) error {
var errs *multierror.Error
input := &fsx.DescribeVolumesInput{}

err = conn.DescribeVolumesPagesWithContext(ctx, input, func(page *fsx.DescribeVolumesOutput, lastPage bool) bool {
if page == nil {
return !lastPage
pages := fsx.NewDescribeVolumesPaginator(conn, input)

for pages.HasMorePages() {
page, err := pages.NextPage(ctx)

if err != nil {
errs = multierror.Append(errs, fmt.Errorf("error listing FSx ONTAP Volume for %s: %w", region, err))
}

if err = sweep.SweepOrchestrator(ctx, sweepResources); err != nil {
errs = multierror.Append(errs, fmt.Errorf("error sweeping FSx ONTAP Volume for %s: %w", region, err))
}

for _, v := range page.Volumes {
if string(v.VolumeType) != awstypes.VolumeTypeOntap {
if v.VolumeType != awstypes.VolumeTypeOntap {
continue
}
if v.OntapConfiguration != nil && aws.ToBool(v.OntapConfiguration.StorageVirtualMachineRoot) {
Expand All @@ -303,19 +302,9 @@ func sweepONTAPVolumes(region string) error {

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

return !lastPage
})

if err != nil {
errs = multierror.Append(errs, fmt.Errorf("error listing FSx ONTAP Volume for %s: %w", region, err))
}

if err = sweep.SweepOrchestrator(ctx, sweepResources); err != nil {
errs = multierror.Append(errs, fmt.Errorf("error sweeping FSx ONTAP Volume for %s: %w", region, err))
}

if awsv1.SkipSweepError(errs.ErrorOrNil()) {
if awsv2.SkipSweepError(errs.ErrorOrNil()) {
log.Printf("[WARN] Skipping FSx ONTAP Volume sweep for %s: %s", region, errs)
return nil
}
Expand All @@ -336,13 +325,21 @@ func sweepOpenZFSFileSystems(region string) error {
var errs *multierror.Error
input := &fsx.DescribeFileSystemsInput{}

err = conn.DescribeFileSystemsPagesWithContext(ctx, input, func(page *fsx.DescribeFileSystemsOutput, lastPage bool) bool {
if page == nil {
return !lastPage
pages := fsx.NewDescribeFileSystemsPaginator(conn, input)

for pages.HasMorePages() {
page, err := pages.NextPage(ctx)

if err != nil {
errs = multierror.Append(errs, fmt.Errorf("error listing FSx OpenZFS File Systems for %s: %w", region, err))
}

if err = sweep.SweepOrchestrator(ctx, sweepResources); err != nil {
errs = multierror.Append(errs, fmt.Errorf("error sweeping FSx OpenZFS File Systems for %s: %w", region, err))
}

for _, fs := range page.FileSystems {
if string(fs.FileSystemType) != awstypes.FileSystemTypeOpenzfs {
if fs.FileSystemType != awstypes.FileSystemTypeOpenzfs {
continue
}

Expand All @@ -352,19 +349,9 @@ func sweepOpenZFSFileSystems(region string) error {

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

return !lastPage
})

if err != nil {
errs = multierror.Append(errs, fmt.Errorf("error listing FSx OpenZFS File Systems for %s: %w", region, err))
}

if err = sweep.SweepOrchestrator(ctx, sweepResources); err != nil {
errs = multierror.Append(errs, fmt.Errorf("error sweeping FSx OpenZFS File Systems for %s: %w", region, err))
}

if awsv1.SkipSweepError(errs.ErrorOrNil()) {
if awsv2.SkipSweepError(errs.ErrorOrNil()) {
log.Printf("[WARN] Skipping FSx OpenZFS File System sweep for %s: %s", region, errs)
return nil
}
Expand All @@ -385,13 +372,21 @@ func sweepOpenZFSVolume(region string) error {
var errs *multierror.Error
input := &fsx.DescribeVolumesInput{}

err = conn.DescribeVolumesPagesWithContext(ctx, input, func(page *fsx.DescribeVolumesOutput, lastPage bool) bool {
if page == nil {
return !lastPage
pages := fsx.NewDescribeVolumesPaginator(conn, input)

for pages.HasMorePages() {
page, err := pages.NextPage(ctx)

if err != nil {
errs = multierror.Append(errs, fmt.Errorf("error listing FSx OpenZFS Volume for %s: %w", region, err))
}

if err = sweep.SweepOrchestrator(ctx, sweepResources); err != nil {
errs = multierror.Append(errs, fmt.Errorf("error sweeping FSx OpenZFS Volume for %s: %w", region, err))
}

for _, v := range page.Volumes {
if string(v.VolumeType) != awstypes.VolumeTypeOpenzfs {
if v.VolumeType != awstypes.VolumeTypeOpenzfs {
continue
}
if v.OpenZFSConfiguration != nil && aws.ToString(v.OpenZFSConfiguration.ParentVolumeId) == "" {
Expand All @@ -404,19 +399,9 @@ func sweepOpenZFSVolume(region string) error {

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

return !lastPage
})

if err != nil {
errs = multierror.Append(errs, fmt.Errorf("error listing FSx OpenZFS Volume for %s: %w", region, err))
}

if err = sweep.SweepOrchestrator(ctx, sweepResources); err != nil {
errs = multierror.Append(errs, fmt.Errorf("error sweeping FSx OpenZFS Volume for %s: %w", region, err))
}

if awsv1.SkipSweepError(errs.ErrorOrNil()) {
if awsv2.SkipSweepError(errs.ErrorOrNil()) {
log.Printf("[WARN] Skipping FSx OpenZFS Volume sweep for %s: %s", region, errs)
return nil
}
Expand All @@ -437,13 +422,21 @@ func sweepWindowsFileSystems(region string) error {
var errs *multierror.Error
input := &fsx.DescribeFileSystemsInput{}

err = conn.DescribeFileSystemsPagesWithContext(ctx, input, func(page *fsx.DescribeFileSystemsOutput, lastPage bool) bool {
if page == nil {
return !lastPage
pages := fsx.NewDescribeFileSystemsPaginator(conn, input)

for pages.HasMorePages() {
page, err := pages.NextPage(ctx)

if err != nil {
errs = multierror.Append(errs, fmt.Errorf("error listing FSx Windows File Systems for %s: %w", region, err))
}

if err = sweep.SweepOrchestrator(ctx, sweepResources); err != nil {
errs = multierror.Append(errs, fmt.Errorf("error sweeping FSx Windows File Systems for %s: %w", region, err))
}

for _, fs := range page.FileSystems {
if string(fs.FileSystemType) != awstypes.FileSystemTypeWindows {
if fs.FileSystemType != awstypes.FileSystemTypeWindows {
continue
}

Expand All @@ -454,19 +447,9 @@ func sweepWindowsFileSystems(region string) error {

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

return !lastPage
})

if err != nil {
errs = multierror.Append(errs, fmt.Errorf("error listing FSx Windows File Systems for %s: %w", region, err))
}

if err = sweep.SweepOrchestrator(ctx, sweepResources); err != nil {
errs = multierror.Append(errs, fmt.Errorf("error sweeping FSx Windows File Systems for %s: %w", region, err))
}

if awsv1.SkipSweepError(errs.ErrorOrNil()) {
if awsv2.SkipSweepError(errs.ErrorOrNil()) {
log.Printf("[WARN] Skipping FSx Windows File System sweep for %s: %s", region, errs)
return nil
}
Expand Down

0 comments on commit 335a845

Please sign in to comment.