Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r/fsx_windows_fs - create from backup + move fsx common functions to internal package #20643

Merged
merged 6 commits into from
Aug 23, 2021
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions .changelog/20643.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_fsx_windows_filesystem: Allow creating filesystem from backup using `backup_id`.
```
155 changes: 0 additions & 155 deletions aws/fsx.go

This file was deleted.

40 changes: 40 additions & 0 deletions aws/internal/service/fsx/finder/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,43 @@ func BackupByID(conn *fsx.FSx, id string) (*fsx.Backup, error) {

return output.Backups[0], nil
}

func FileSystemByID(conn *fsx.FSx, id string) (*fsx.FileSystem, error) {
input := &fsx.DescribeFileSystemsInput{
FileSystemIds: []*string{aws.String(id)},
}

var filesystems []*fsx.FileSystem

err := conn.DescribeFileSystemsPages(input, func(page *fsx.DescribeFileSystemsOutput, lastPage bool) bool {
for _, fs := range page.FileSystems {
if fs == nil {
continue
}

filesystems = append(filesystems, fs)
}

return !lastPage
})

if tfawserr.ErrCodeEquals(err, fsx.ErrCodeFileSystemNotFound) {
return nil, &resource.NotFoundError{
LastError: err,
LastRequest: input,
}
}

if err != nil {
return nil, err
}

if filesystems == nil || filesystems[0] == nil {
return nil, &resource.NotFoundError{
Message: "Empty result",
LastRequest: input,
}
}

return filesystems[0], nil
}
42 changes: 42 additions & 0 deletions aws/internal/service/fsx/waiter/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,45 @@ func BackupStatus(conn *fsx.FSx, id string) resource.StateRefreshFunc {
return output, aws.StringValue(output.Lifecycle), nil
}
}

func FileSystemStatus(conn *fsx.FSx, id string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
output, err := finder.FileSystemByID(conn, id)

if tfresource.NotFound(err) {
return nil, "", nil
}

if err != nil {
return nil, "", err
}

return output, aws.StringValue(output.Lifecycle), nil
}
}

func FileSystemAdministrativeActionsStatus(conn *fsx.FSx, id, action string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
output, err := finder.FileSystemByID(conn, id)

if tfresource.NotFound(err) {
return nil, "", nil
}

if err != nil {
return nil, "", err
}

for _, administrativeAction := range output.AdministrativeActions {
if administrativeAction == nil {
continue
}

if aws.StringValue(administrativeAction.AdministrativeActionType) == action {
return output, aws.StringValue(administrativeAction.Status), nil
}
}

return output, fsx.StatusCompleted, nil
}
}
69 changes: 69 additions & 0 deletions aws/internal/service/fsx/waiter/waiter.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package waiter

import (
"errors"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/fsx"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/tfresource"
)

const (
Expand Down Expand Up @@ -45,3 +48,69 @@ func BackupDeleted(conn *fsx.FSx, id string) (*fsx.Backup, error) {

return nil, err
}

func FileSystemAvailable(conn *fsx.FSx, id string, timeout time.Duration) (*fsx.FileSystem, error) {
stateConf := &resource.StateChangeConf{
Pending: []string{fsx.FileSystemLifecycleCreating, fsx.FileSystemLifecycleUpdating},
Target: []string{fsx.FileSystemLifecycleAvailable},
Refresh: FileSystemStatus(conn, id),
Timeout: timeout,
Delay: 30 * time.Second,
}

outputRaw, err := stateConf.WaitForState()

if output, ok := outputRaw.(*fsx.FileSystem); ok {
if output.FailureDetails != nil {
tfresource.SetLastError(err, errors.New(aws.StringValue(output.FailureDetails.Message)))
}
}

return nil, err
}

func FileSystemDeleted(conn *fsx.FSx, id string, timeout time.Duration) (*fsx.FileSystem, error) {
stateConf := &resource.StateChangeConf{
Pending: []string{fsx.FileSystemLifecycleAvailable, fsx.FileSystemLifecycleDeleting},
Target: []string{},
Refresh: FileSystemStatus(conn, id),
Timeout: timeout,
Delay: 30 * time.Second,
}

outputRaw, err := stateConf.WaitForState()

if output, ok := outputRaw.(*fsx.FileSystem); ok {
if output.FailureDetails != nil {
tfresource.SetLastError(err, errors.New(aws.StringValue(output.FailureDetails.Message)))
}
}

return nil, err
}

func FileSystemAdministrativeActionsCompleted(conn *fsx.FSx, id, action string, timeout time.Duration) (*fsx.FileSystem, error) {
stateConf := &resource.StateChangeConf{
Pending: []string{
fsx.StatusInProgress,
fsx.StatusPending,
},
Target: []string{
fsx.StatusCompleted,
fsx.StatusUpdatedOptimizing,
},
Refresh: FileSystemAdministrativeActionsStatus(conn, id, action),
Timeout: timeout,
Delay: 30 * time.Second,
}

outputRaw, err := stateConf.WaitForState()

if output, ok := outputRaw.(*fsx.FileSystem); ok {
if output.FailureDetails != nil {
tfresource.SetLastError(err, errors.New(aws.StringValue(output.FailureDetails.Message)))
}
}

return nil, err
}
2 changes: 1 addition & 1 deletion aws/resource_aws_fsx_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func resourceAwsFsxBackupDelete(d *schema.ResourceData, meta interface{}) error
return fmt.Errorf("error deleting FSx Backup (%s): %w", d.Id(), err)
}

log.Println("[DEBUG] Waiting for filesystem to delete")
log.Println("[DEBUG] Waiting for backup to delete")
if _, err := waiter.BackupDeleted(conn, d.Id()); err != nil {
return fmt.Errorf("error waiting for FSx Backup (%s) to deleted: %w", d.Id(), err)
}
Expand Down
Loading