Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Refactor fsx-filesystems.go to enhance error handling and robustness #1249

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 93 additions & 4 deletions resources/fsx-filesystems.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package resources

import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/fsx"
"github.com/rebuy-de/aws-nuke/v2/pkg/types"
"strings"
"time"
)

type FSxFileSystem struct {
Expand Down Expand Up @@ -45,12 +48,98 @@ func ListFSxFileSystems(sess *session.Session) ([]Resource, error) {
return resources, nil
}

func isRootVolume(volume *fsx.Volume) bool {
return strings.HasSuffix(*volume.Name, "_root")
}

func deleteFileSystemWithRetry(svc *fsx.FSx, filesystemId *string) error {
const maxRetries = 5
const retryDelay = 10 * time.Second

for attempt := 0; attempt < maxRetries; attempt++ {
describeSVMsInput := &fsx.DescribeStorageVirtualMachinesInput{
Filters: []*fsx.StorageVirtualMachineFilter{
{
Name: aws.String("file-system-id"),
Values: []*string{filesystemId},
},
},
}
svmResp, err := svc.DescribeStorageVirtualMachines(describeSVMsInput)
if err != nil {
return err
}

if len(svmResp.StorageVirtualMachines) == 0 {
_, err := svc.DeleteFileSystem(&fsx.DeleteFileSystemInput{
FileSystemId: filesystemId,
})
if err != nil {
return err
}
return nil
} else {
time.Sleep(retryDelay)
}
}

return awserr.New("MaxRetriesExceeded", "unable to delete filesystem after max retries", nil)
}

func (f *FSxFileSystem) Remove() error {
_, err := f.svc.DeleteFileSystem(&fsx.DeleteFileSystemInput{
FileSystemId: f.filesystem.FileSystemId,
})
describeSVMsInput := &fsx.DescribeStorageVirtualMachinesInput{
StorageVirtualMachineIds: []*string{ /* Populate with SVM IDs as needed */ },
}

svmResp, err := f.svc.DescribeStorageVirtualMachines(describeSVMsInput)
if err != nil {
return err
}

for _, svm := range svmResp.StorageVirtualMachines {
listVolumesInput := &fsx.DescribeVolumesInput{
Filters: []*fsx.VolumeFilter{
{
Name: aws.String("storage-virtual-machine-id"),
Values: []*string{svm.StorageVirtualMachineId},
},
},
}

volumeResp, err := f.svc.DescribeVolumes(listVolumesInput)
if err != nil {
return err
}

for _, volume := range volumeResp.Volumes {
if !isRootVolume(volume) {
_, err := f.svc.DeleteVolume(&fsx.DeleteVolumeInput{
VolumeId: volume.VolumeId,
OntapConfiguration: &fsx.DeleteVolumeOntapConfiguration{
SkipFinalBackup: aws.Bool(true),
},
})
if err != nil {
return err
}
}
}

_, err = f.svc.DeleteStorageVirtualMachine(&fsx.DeleteStorageVirtualMachineInput{
StorageVirtualMachineId: svm.StorageVirtualMachineId,
})

if err != nil {
return err
}
}

fsDeletionErr := deleteFileSystemWithRetry(f.svc, f.filesystem.FileSystemId)
if fsDeletionErr != nil {
return fsDeletionErr
}

return err
return nil
}

func (f *FSxFileSystem) Properties() types.Properties {
Expand Down