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_ontap_filesystem - new resource #20951

Merged
merged 25 commits into from
Sep 22, 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/20951.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
aws_fsx_ontap_filesystem
```
48 changes: 33 additions & 15 deletions aws/internal/service/fsx/finder/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,30 @@ import (
"github.com/aws/aws-sdk-go/service/fsx"
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/tfresource"
)

func AdministrativeActionByFileSystemIDAndActionType(conn *fsx.FSx, fsID, actionType string) (*fsx.AdministrativeAction, error) {
fileSystem, err := FileSystemByID(conn, fsID)

if err != nil {
return nil, err
}

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

if aws.StringValue(administrativeAction.AdministrativeActionType) == actionType {
return administrativeAction, nil
}
}

// If the administrative action isn't found, assume it's complete.
return &fsx.AdministrativeAction{Status: aws.String(fsx.StatusCompleted)}, nil
}

func BackupByID(conn *fsx.FSx, id string) (*fsx.Backup, error) {
input := &fsx.DescribeBackupsInput{
BackupIds: aws.StringSlice([]string{id}),
Expand All @@ -26,10 +48,7 @@ func BackupByID(conn *fsx.FSx, id string) (*fsx.Backup, error) {
}

if output == nil || len(output.Backups) == 0 || output.Backups[0] == nil {
return nil, &resource.NotFoundError{
Message: "Empty result",
LastRequest: input,
}
return nil, tfresource.NewEmptyResultError(input)
}

return output.Backups[0], nil
Expand All @@ -43,14 +62,12 @@ func FileSystemByID(conn *fsx.FSx, id string) (*fsx.FileSystem, error) {
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)
if page == nil {
return !lastPage
}

filesystems = append(filesystems, page.FileSystems...)

return !lastPage
})

Expand All @@ -65,11 +82,12 @@ func FileSystemByID(conn *fsx.FSx, id string) (*fsx.FileSystem, error) {
return nil, err
}

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

if count := len(filesystems); count > 1 {
return nil, tfresource.NewTooManyResultsError(count, input)
}

return filesystems[0], nil
Expand Down
24 changes: 7 additions & 17 deletions aws/internal/service/fsx/waiter/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"github.com/terraform-providers/terraform-provider-aws/aws/internal/tfresource"
)

func BackupStatus(conn *fsx.FSx, id string) resource.StateRefreshFunc {
func AdministrativeActionStatus(conn *fsx.FSx, fsID, actionType string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
output, err := finder.BackupByID(conn, id)
output, err := finder.AdministrativeActionByFileSystemIDAndActionType(conn, fsID, actionType)

if tfresource.NotFound(err) {
return nil, "", nil
Expand All @@ -20,13 +20,13 @@ func BackupStatus(conn *fsx.FSx, id string) resource.StateRefreshFunc {
return nil, "", err
}

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

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

if tfresource.NotFound(err) {
return nil, "", nil
Expand All @@ -40,7 +40,7 @@ func FileSystemStatus(conn *fsx.FSx, id string) resource.StateRefreshFunc {
}
}

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

Expand All @@ -52,16 +52,6 @@ func FileSystemAdministrativeActionsStatus(conn *fsx.FSx, id, action string) res
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
return output, aws.StringValue(output.Lifecycle), nil
}
}
58 changes: 40 additions & 18 deletions aws/internal/service/fsx/waiter/waiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,28 @@ const (
BackupDeletedTimeout = 10 * time.Minute
)

func AdministrativeActionCompleted(conn *fsx.FSx, fsID, actionType string, timeout time.Duration) (*fsx.AdministrativeAction, error) {
stateConf := &resource.StateChangeConf{
Pending: []string{fsx.StatusInProgress, fsx.StatusPending},
Target: []string{fsx.StatusCompleted, fsx.StatusUpdatedOptimizing},
Refresh: AdministrativeActionStatus(conn, fsID, actionType),
Timeout: timeout,
Delay: 30 * time.Second,
}

outputRaw, err := stateConf.WaitForState()

if output, ok := outputRaw.(*fsx.AdministrativeAction); ok {
if status, details := aws.StringValue(output.Status), output.FailureDetails; status == fsx.StatusFailed && details != nil {
tfresource.SetLastError(err, errors.New(aws.StringValue(output.FailureDetails.Message)))
}

return output, err
}

return nil, err
}

func BackupAvailable(conn *fsx.FSx, id string) (*fsx.Backup, error) {
stateConf := &resource.StateChangeConf{
Pending: []string{fsx.BackupLifecycleCreating, fsx.BackupLifecyclePending, fsx.BackupLifecycleTransferring},
Expand Down Expand Up @@ -49,9 +71,9 @@ 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) {
func FileSystemCreated(conn *fsx.FSx, id string, timeout time.Duration) (*fsx.FileSystem, error) {
stateConf := &resource.StateChangeConf{
Pending: []string{fsx.FileSystemLifecycleCreating, fsx.FileSystemLifecycleUpdating},
Pending: []string{fsx.FileSystemLifecycleCreating},
Target: []string{fsx.FileSystemLifecycleAvailable},
Refresh: FileSystemStatus(conn, id),
Timeout: timeout,
Expand All @@ -61,18 +83,20 @@ func FileSystemAvailable(conn *fsx.FSx, id string, timeout time.Duration) (*fsx.
outputRaw, err := stateConf.WaitForState()

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

return output, err
}

return nil, err
}

func FileSystemDeleted(conn *fsx.FSx, id string, timeout time.Duration) (*fsx.FileSystem, error) {
func FileSystemUpdated(conn *fsx.FSx, id string, timeout time.Duration) (*fsx.FileSystem, error) {
stateConf := &resource.StateChangeConf{
Pending: []string{fsx.FileSystemLifecycleAvailable, fsx.FileSystemLifecycleDeleting},
Target: []string{},
Pending: []string{fsx.FileSystemLifecycleUpdating},
Target: []string{fsx.FileSystemLifecycleAvailable},
Refresh: FileSystemStatus(conn, id),
Timeout: timeout,
Delay: 30 * time.Second,
Expand All @@ -81,35 +105,33 @@ func FileSystemDeleted(conn *fsx.FSx, id string, timeout time.Duration) (*fsx.Fi
outputRaw, err := stateConf.WaitForState()

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

return output, err
}

return nil, err
}

func FileSystemAdministrativeActionsCompleted(conn *fsx.FSx, id, action string, timeout time.Duration) (*fsx.FileSystem, error) {
func FileSystemDeleted(conn *fsx.FSx, id 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),
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 {
if status, details := aws.StringValue(output.Lifecycle), output.FailureDetails; status == fsx.FileSystemLifecycleFailed && details != nil {
tfresource.SetLastError(err, errors.New(aws.StringValue(output.FailureDetails.Message)))
}

return output, err
}

return nil, err
Expand Down
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,7 @@ func Provider() *schema.Provider {
"aws_flow_log": resourceAwsFlowLog(),
"aws_fsx_backup": resourceAwsFsxBackup(),
"aws_fsx_lustre_file_system": resourceAwsFsxLustreFileSystem(),
"aws_fsx_ontap_file_system": resourceAwsFsxOntapFileSystem(),
"aws_fsx_windows_file_system": resourceAwsFsxWindowsFileSystem(),
"aws_fms_admin_account": resourceAwsFmsAdminAccount(),
"aws_fms_policy": resourceAwsFmsPolicy(),
Expand Down
9 changes: 5 additions & 4 deletions aws/resource_aws_fsx_lustre_file_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func resourceAwsFsxLustreFileSystem() *schema.Resource {

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(30 * time.Minute),
Update: schema.DefaultTimeout(30 * time.Minute),
Delete: schema.DefaultTimeout(30 * time.Minute),
},

Expand Down Expand Up @@ -338,8 +339,8 @@ func resourceAwsFsxLustreFileSystemCreate(d *schema.ResourceData, meta interface
d.SetId(aws.StringValue(result.FileSystem.FileSystemId))
}

if _, err := waiter.FileSystemAvailable(conn, d.Id(), d.Timeout(schema.TimeoutCreate)); err != nil {
return fmt.Errorf("error waiting for FSx Lustre File System (%s) to be available: %w", d.Id(), err)
if _, err := waiter.FileSystemCreated(conn, d.Id(), d.Timeout(schema.TimeoutCreate)); err != nil {
return fmt.Errorf("error waiting for FSx Lustre File System (%s) create: %w", d.Id(), err)
}

return resourceAwsFsxLustreFileSystemRead(d, meta)
Expand Down Expand Up @@ -392,8 +393,8 @@ func resourceAwsFsxLustreFileSystemUpdate(d *schema.ResourceData, meta interface
return fmt.Errorf("error updating FSX Lustre File System (%s): %w", d.Id(), err)
}

if _, err := waiter.FileSystemAvailable(conn, d.Id(), d.Timeout(schema.TimeoutCreate)); err != nil {
return fmt.Errorf("error waiting for FSx Lustre File System (%s) to be available: %w", d.Id(), err)
if _, err := waiter.FileSystemUpdated(conn, d.Id(), d.Timeout(schema.TimeoutUpdate)); err != nil {
return fmt.Errorf("error waiting for FSx Lustre File System (%s) update: %w", d.Id(), err)
}
}

Expand Down
Loading