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/storagegateway_nfs_file_share - add support for notification_policy + validations #16340

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
30 changes: 30 additions & 0 deletions aws/internal/service/storagegateway/waiter/status.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package waiter

import (
"fmt"
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/storagegateway"
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
Expand All @@ -10,6 +13,8 @@ import (
const (
StoredIscsiVolumeStatusNotFound = "NotFound"
StoredIscsiVolumeStatusUnknown = "Unknown"
NfsFileShareStatusNotFound = "NotFound"
NfsFileShareStatusUnknown = "Unknown"
)

// StoredIscsiVolumeStatus fetches the Volume and its Status
Expand Down Expand Up @@ -37,3 +42,28 @@ func StoredIscsiVolumeStatus(conn *storagegateway.StorageGateway, volumeARN stri
return output, aws.StringValue(output.StorediSCSIVolumes[0].VolumeStatus), nil
}
}

func NfsFileShareStatus(conn *storagegateway.StorageGateway, fileShareArn string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
input := &storagegateway.DescribeNFSFileSharesInput{
FileShareARNList: []*string{aws.String(fileShareArn)},
}

log.Printf("[DEBUG] Reading Storage Gateway NFS File Share: %s", input)
output, err := conn.DescribeNFSFileShares(input)
if err != nil {
if tfawserr.ErrMessageContains(err, storagegateway.ErrCodeInvalidGatewayRequestException, "The specified file share was not found.") {
return nil, NfsFileShareStatusNotFound, nil
}
return nil, NfsFileShareStatusUnknown, fmt.Errorf("error reading Storage Gateway NFS File Share: %w", err)
}

if output == nil || len(output.NFSFileShareInfoList) == 0 || output.NFSFileShareInfoList[0] == nil {
return nil, NfsFileShareStatusNotFound, nil
}

fileshare := output.NFSFileShareInfoList[0]

return fileshare, aws.StringValue(fileshare.FileShareStatus), nil
}
}
40 changes: 40 additions & 0 deletions aws/internal/service/storagegateway/waiter/waiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (

const (
StoredIscsiVolumeAvailableTimeout = 5 * time.Minute
NfsFileShareAvailableDelay = 5 * time.Second
NfsFileShareNotFoundDelay = 5 * time.Second
)

// StoredIscsiVolumeAvailable waits for a StoredIscsiVolume to return Available
Expand All @@ -28,3 +30,41 @@ func StoredIscsiVolumeAvailable(conn *storagegateway.StorageGateway, volumeARN s

return nil, err
}

// NfsFileShareAvailable waits for a NFS File Share to return Available
func NfsFileShareAvailable(conn *storagegateway.StorageGateway, fileShareArn string, timeout time.Duration) (*storagegateway.NFSFileShareInfo, error) {
stateConf := &resource.StateChangeConf{
Pending: []string{"BOOTSTRAPPING", "CREATING", "RESTORING", "UPDATING"},
Target: []string{"AVAILABLE"},
Comment on lines +37 to +38
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Define these as constants?

Refresh: NfsFileShareStatus(conn, fileShareArn),
Timeout: timeout,
Delay: NfsFileShareAvailableDelay,
}

outputRaw, err := stateConf.WaitForState()

if output, ok := outputRaw.(*storagegateway.NFSFileShareInfo); ok {
return output, err
}

return nil, err
}

func NfsFileShareNotFound(conn *storagegateway.StorageGateway, fileShareArn string, timeout time.Duration) (*storagegateway.NFSFileShareInfo, error) {
DrFaust92 marked this conversation as resolved.
Show resolved Hide resolved
stateConf := &resource.StateChangeConf{
Pending: []string{"AVAILABLE", "DELETING", "FORCE_DELETING"},
Target: []string{"NotFound"},
DrFaust92 marked this conversation as resolved.
Show resolved Hide resolved
Refresh: NfsFileShareStatus(conn, fileShareArn),
Timeout: timeout,
Delay: NfsFileShareNotFoundDelay,
NotFoundChecks: 1,
}

outputRaw, err := stateConf.WaitForState()

if output, ok := outputRaw.(*storagegateway.NFSFileShareInfo); ok {
return output, err
}

return nil, err
}
113 changes: 44 additions & 69 deletions aws/resource_aws_storagegateway_nfs_file_share.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package aws
import (
"fmt"
"log"
"regexp"
"time"

"github.com/aws/aws-sdk-go/aws"
Expand All @@ -11,6 +12,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/storagegateway/waiter"
)

func resourceAwsStorageGatewayNfsFileShare() *schema.Resource {
Expand Down Expand Up @@ -38,7 +40,13 @@ func resourceAwsStorageGatewayNfsFileShare() *schema.Resource {
Required: true,
MinItems: 1,
MaxItems: 100,
Elem: &schema.Schema{Type: schema.TypeString},
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.Any(
validateIpv4CIDRNetworkAddress,
validation.IsIPv4Address,
),
},
},
"default_storage_class": {
Type: schema.TypeString,
Expand Down Expand Up @@ -89,26 +97,28 @@ func resourceAwsStorageGatewayNfsFileShare() *schema.Resource {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"directory_mode": {
Type: schema.TypeString,
Optional: true,
Default: "0777",
Type: schema.TypeString,
Optional: true,
Default: "0777",
ValidateFunc: validation.StringMatch(regexp.MustCompile(`^[0-7]{4}$`), ""),
DrFaust92 marked this conversation as resolved.
Show resolved Hide resolved
},
"file_mode": {
Type: schema.TypeString,
Optional: true,
Default: "0666",
Type: schema.TypeString,
Optional: true,
Default: "0666",
ValidateFunc: validation.StringMatch(regexp.MustCompile(`^[0-7]{4}$`), ""),
},
"group_id": {
Type: schema.TypeInt,
Optional: true,
Default: 65534,
ValidateFunc: validation.IntAtLeast(0),
ValidateFunc: validation.IntBetween(0, 4294967294),
DrFaust92 marked this conversation as resolved.
Show resolved Hide resolved
},
"owner_id": {
Type: schema.TypeInt,
Optional: true,
Default: 65534,
ValidateFunc: validation.IntAtLeast(0),
ValidateFunc: validation.IntBetween(0, 4294967294),
},
},
},
Expand Down Expand Up @@ -169,6 +179,15 @@ func resourceAwsStorageGatewayNfsFileShare() *schema.Resource {
Computed: true,
ValidateFunc: validation.StringLenBetween(1, 255),
},
"notification_policy": {
Type: schema.TypeString,
Optional: true,
Default: "{}",
ValidateFunc: validation.All(
validation.StringMatch(regexp.MustCompile(`^\{[\w\s:\{\}\[\]"]*}$`), ""),
validation.StringLenBetween(2, 100),
),
},
"tags": tagsSchema(),
},
}
Expand Down Expand Up @@ -198,6 +217,10 @@ func resourceAwsStorageGatewayNfsFileShareCreate(d *schema.ResourceData, meta in
input.KMSKey = aws.String(v.(string))
}

if v, ok := d.GetOk("notification_policy"); ok {
input.NotificationPolicy = aws.String(v.(string))
}

if v, ok := d.GetOk("file_share_name"); ok {
input.FileShareName = aws.String(v.(string))
}
Expand All @@ -214,17 +237,8 @@ func resourceAwsStorageGatewayNfsFileShareCreate(d *schema.ResourceData, meta in

d.SetId(aws.StringValue(output.FileShareARN))

stateConf := &resource.StateChangeConf{
Pending: []string{"CREATING", "MISSING"},
Target: []string{"AVAILABLE"},
Refresh: storageGatewayNfsFileShareRefreshFunc(d.Id(), conn),
Timeout: d.Timeout(schema.TimeoutCreate),
Delay: 5 * time.Second,
MinTimeout: 5 * time.Second,
}
_, err = stateConf.WaitForState()
if err != nil {
return fmt.Errorf("error waiting for Storage Gateway NFS File Share creation: %w", err)
if _, err = waiter.NfsFileShareAvailable(conn, d.Id(), d.Timeout(schema.TimeoutCreate)); err != nil {
return fmt.Errorf("error waiting for Storage Gateway NFS File Share (%q) to be Available: %w", d.Id(), err)
}

return resourceAwsStorageGatewayNfsFileShareRead(d, meta)
Expand Down Expand Up @@ -287,6 +301,7 @@ func resourceAwsStorageGatewayNfsFileShareRead(d *schema.ResourceData, meta inte
d.Set("requester_pays", fileshare.RequesterPays)
d.Set("role_arn", fileshare.Role)
d.Set("squash", fileshare.Squash)
d.Set("notification_policy", fileshare.NotificationPolicy)

if err := d.Set("tags", keyvaluetags.StoragegatewayKeyValueTags(fileshare.Tags).IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %w", err)
Expand All @@ -307,7 +322,7 @@ func resourceAwsStorageGatewayNfsFileShareUpdate(d *schema.ResourceData, meta in

if d.HasChanges("client_list", "default_storage_class", "guess_mime_type_enabled", "kms_encrypted",
"nfs_file_share_defaults", "object_acl", "read_only", "requester_pays", "squash", "kms_key_arn",
"cache_attributes", "file_share_name") {
"cache_attributes", "file_share_name", "notification_policy") {

input := &storagegateway.UpdateNFSFileShareInput{
ClientList: expandStringSet(d.Get("client_list").(*schema.Set)),
Expand All @@ -326,6 +341,10 @@ func resourceAwsStorageGatewayNfsFileShareUpdate(d *schema.ResourceData, meta in
input.KMSKey = aws.String(v.(string))
}

if v, ok := d.GetOk("notification_policy"); ok {
input.NotificationPolicy = aws.String(v.(string))
}

if v, ok := d.GetOk("file_share_name"); ok {
input.FileShareName = aws.String(v.(string))
}
Expand All @@ -340,17 +359,8 @@ func resourceAwsStorageGatewayNfsFileShareUpdate(d *schema.ResourceData, meta in
return fmt.Errorf("error updating Storage Gateway NFS File Share: %w", err)
}

stateConf := &resource.StateChangeConf{
Pending: []string{"UPDATING"},
Target: []string{"AVAILABLE"},
Refresh: storageGatewayNfsFileShareRefreshFunc(d.Id(), conn),
Timeout: d.Timeout(schema.TimeoutUpdate),
Delay: 5 * time.Second,
MinTimeout: 5 * time.Second,
}
_, err = stateConf.WaitForState()
if err != nil {
return fmt.Errorf("error waiting for Storage Gateway NFS File Share update: %w", err)
if _, err = waiter.NfsFileShareAvailable(conn, d.Id(), d.Timeout(schema.TimeoutUpdate)); err != nil {
return fmt.Errorf("error waiting for Storage Gateway NFS File Share (%q) to be Available: %w", d.Id(), err)
}
}

Expand All @@ -373,51 +383,16 @@ func resourceAwsStorageGatewayNfsFileShareDelete(d *schema.ResourceData, meta in
return fmt.Errorf("error deleting Storage Gateway NFS File Share: %w", err)
}

stateConf := &resource.StateChangeConf{
Pending: []string{"AVAILABLE", "DELETING", "FORCE_DELETING"},
Target: []string{"MISSING"},
Refresh: storageGatewayNfsFileShareRefreshFunc(d.Id(), conn),
Timeout: d.Timeout(schema.TimeoutDelete),
Delay: 5 * time.Second,
MinTimeout: 5 * time.Second,
NotFoundChecks: 1,
}
_, err = stateConf.WaitForState()
if err != nil {
if _, err = waiter.NfsFileShareNotFound(conn, d.Id(), d.Timeout(schema.TimeoutDelete)); err != nil {
if isResourceNotFoundError(err) {
return nil
}
return fmt.Errorf("error waiting for Storage Gateway NFS File Share deletion: %w", err)
return fmt.Errorf("error waiting for Storage Gateway NFS File Share (%q) to be deleted: %w", d.Id(), err)
}

return nil
}

func storageGatewayNfsFileShareRefreshFunc(fileShareArn string, conn *storagegateway.StorageGateway) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
input := &storagegateway.DescribeNFSFileSharesInput{
FileShareARNList: []*string{aws.String(fileShareArn)},
}

log.Printf("[DEBUG] Reading Storage Gateway NFS File Share: %s", input)
output, err := conn.DescribeNFSFileShares(input)
if err != nil {
if isAWSErr(err, storagegateway.ErrCodeInvalidGatewayRequestException, "The specified file share was not found.") {
return nil, "MISSING", nil
}
return nil, "ERROR", fmt.Errorf("error reading Storage Gateway NFS File Share: %w", err)
}

if output == nil || len(output.NFSFileShareInfoList) == 0 || output.NFSFileShareInfoList[0] == nil {
return nil, "MISSING", nil
}

fileshare := output.NFSFileShareInfoList[0]

return fileshare, aws.StringValue(fileshare.FileShareStatus), nil
}
}

func expandStorageGatewayNfsFileShareDefaults(l []interface{}) *storagegateway.NFSFileShareDefaults {
if len(l) == 0 || l[0] == nil {
return nil
Expand Down
Loading