From 0af4c7357308ca0fa61e0922f6beadbb2abfa314 Mon Sep 17 00:00:00 2001 From: drfaust92 Date: Fri, 20 Nov 2020 11:47:04 +0200 Subject: [PATCH 1/5] add notifiaction policy + validations --- .../service/storagegateway/waiter/status.go | 30 +++++ .../service/storagegateway/waiter/waiter.go | 40 ++++++ ...ource_aws_storagegateway_nfs_file_share.go | 120 +++++++----------- ..._aws_storagegateway_nfs_file_share_test.go | 60 ++++++++- ...toragegateway_nfs_file_share.html.markdown | 1 + 5 files changed, 178 insertions(+), 73 deletions(-) diff --git a/aws/internal/service/storagegateway/waiter/status.go b/aws/internal/service/storagegateway/waiter/status.go index dc5083642dc..b5aa8e857ff 100644 --- a/aws/internal/service/storagegateway/waiter/status.go +++ b/aws/internal/service/storagegateway/waiter/status.go @@ -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" @@ -10,6 +13,8 @@ import ( const ( StoredIscsiVolumeStatusNotFound = "NotFound" StoredIscsiVolumeStatusUnknown = "Unknown" + NfsFileShareStatusNotFound = "NotFound" + NfsFileShareStatusUnknown = "Unknown" ) // StoredIscsiVolumeStatus fetches the Volume and its Status @@ -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 + } +} diff --git a/aws/internal/service/storagegateway/waiter/waiter.go b/aws/internal/service/storagegateway/waiter/waiter.go index 67173ec57f7..be644bcf226 100644 --- a/aws/internal/service/storagegateway/waiter/waiter.go +++ b/aws/internal/service/storagegateway/waiter/waiter.go @@ -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 @@ -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"}, + 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) { + stateConf := &resource.StateChangeConf{ + Pending: []string{"AVAILABLE", "DELETING", "FORCE_DELETING"}, + Target: []string{"NotFound"}, + 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 +} diff --git a/aws/resource_aws_storagegateway_nfs_file_share.go b/aws/resource_aws_storagegateway_nfs_file_share.go index c115eba4047..1011f2f68b1 100644 --- a/aws/resource_aws_storagegateway_nfs_file_share.go +++ b/aws/resource_aws_storagegateway_nfs_file_share.go @@ -3,6 +3,7 @@ package aws import ( "fmt" "log" + "regexp" "time" "github.com/aws/aws-sdk-go/aws" @@ -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 { @@ -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, @@ -67,9 +75,10 @@ func resourceAwsStorageGatewayNfsFileShare() *schema.Resource { Default: true, }, "kms_encrypted": { - Type: schema.TypeBool, - Optional: true, - Default: false, + Type: schema.TypeBool, + Optional: true, + Default: false, + RequiredWith: []string{"kms_key_arn"}, }, "kms_key_arn": { Type: schema.TypeString, @@ -89,26 +98,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}$`), ""), }, "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), }, "owner_id": { Type: schema.TypeInt, Optional: true, Default: 65534, - ValidateFunc: validation.IntAtLeast(0), + ValidateFunc: validation.IntBetween(0, 4294967294), }, }, }, @@ -169,6 +180,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(), }, } @@ -198,6 +218,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)) } @@ -214,17 +238,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) @@ -287,6 +302,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) @@ -307,7 +323,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)), @@ -326,6 +342,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)) } @@ -340,17 +360,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) } } @@ -373,51 +384,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 diff --git a/aws/resource_aws_storagegateway_nfs_file_share_test.go b/aws/resource_aws_storagegateway_nfs_file_share_test.go index f79534d923c..0fd9f70deaa 100644 --- a/aws/resource_aws_storagegateway_nfs_file_share_test.go +++ b/aws/resource_aws_storagegateway_nfs_file_share_test.go @@ -49,6 +49,8 @@ func TestAccAWSStorageGatewayNfsFileShare_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "squash", "RootSquash"), resource.TestCheckResourceAttr(resourceName, "cache_attributes.#", "0"), resource.TestCheckResourceAttr(resourceName, "file_share_name", rName), + resource.TestCheckResourceAttr(resourceName, "notification_policy", "{}"), + resource.TestCheckResourceAttr(resourceName, "tags.%", "0"), ), }, { @@ -155,6 +157,7 @@ func TestAccAWSStorageGatewayNfsFileShare_ClientList(t *testing.T) { Check: resource.ComposeTestCheckFunc( testAccCheckAWSStorageGatewayNfsFileShareExists(resourceName, &nfsFileShare), resource.TestCheckResourceAttr(resourceName, "client_list.#", "1"), + tfawsresource.TestCheckTypeSetElemAttr(resourceName, "client_list.*", "1.1.1.1/32"), ), }, { @@ -162,13 +165,16 @@ func TestAccAWSStorageGatewayNfsFileShare_ClientList(t *testing.T) { Check: resource.ComposeTestCheckFunc( testAccCheckAWSStorageGatewayNfsFileShareExists(resourceName, &nfsFileShare), resource.TestCheckResourceAttr(resourceName, "client_list.#", "2"), + tfawsresource.TestCheckTypeSetElemAttr(resourceName, "client_list.*", "2.2.2.2/32"), + tfawsresource.TestCheckTypeSetElemAttr(resourceName, "client_list.*", "3.3.3.3/32"), ), }, { - Config: testAccAWSStorageGatewayNfsFileShareConfig_ClientList_Single(rName, "4.4.4.4/32"), + Config: testAccAWSStorageGatewayNfsFileShareConfig_ClientList_Single(rName, "4.4.4.4"), Check: resource.ComposeTestCheckFunc( testAccCheckAWSStorageGatewayNfsFileShareExists(resourceName, &nfsFileShare), resource.TestCheckResourceAttr(resourceName, "client_list.#", "1"), + tfawsresource.TestCheckTypeSetElemAttr(resourceName, "client_list.*", "4.4.4.4"), ), }, { @@ -493,6 +499,46 @@ func TestAccAWSStorageGatewayNfsFileShare_Squash(t *testing.T) { }) } +func TestAccAWSStorageGatewayNfsFileShare_notificationPolicy(t *testing.T) { + var nfsFileShare storagegateway.NFSFileShareInfo + rName := acctest.RandomWithPrefix("tf-acc-test") + resourceName := "aws_storagegateway_nfs_file_share.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSStorageGatewayNfsFileShareDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSStorageGatewayNfsFileShareConfigNotificationPolicy(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSStorageGatewayNfsFileShareExists(resourceName, &nfsFileShare), + resource.TestCheckResourceAttr(resourceName, "notification_policy", "{\"Upload\": {\"SettlingTimeInSeconds\": 60}}"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccAWSStorageGatewayNfsFileShareConfig_Required(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSStorageGatewayNfsFileShareExists(resourceName, &nfsFileShare), + resource.TestCheckResourceAttr(resourceName, "notification_policy", "{}"), + ), + }, + { + Config: testAccAWSStorageGatewayNfsFileShareConfigNotificationPolicy(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSStorageGatewayNfsFileShareExists(resourceName, &nfsFileShare), + resource.TestCheckResourceAttr(resourceName, "notification_policy", "{\"Upload\": {\"SettlingTimeInSeconds\": 60}}"), + ), + }, + }, + }) +} + func TestAccAWSStorageGatewayNfsFileShare_cacheAttributes(t *testing.T) { var nfsFileShare storagegateway.NFSFileShareInfo rName := acctest.RandomWithPrefix("tf-acc-test") @@ -910,3 +956,15 @@ resource "aws_storagegateway_nfs_file_share" "test" { } `, timeout) } + +func testAccAWSStorageGatewayNfsFileShareConfigNotificationPolicy(rName string) string { + return testAccAWSStorageGateway_S3FileShareBase(rName) + ` +resource "aws_storagegateway_nfs_file_share" "test" { + client_list = ["0.0.0.0/0"] + gateway_arn = aws_storagegateway_gateway.test.arn + location_arn = aws_s3_bucket.test.arn + role_arn = aws_iam_role.test.arn + notification_policy = "{\"Upload\": {\"SettlingTimeInSeconds\": 60}}" +} +` +} diff --git a/website/docs/r/storagegateway_nfs_file_share.html.markdown b/website/docs/r/storagegateway_nfs_file_share.html.markdown index c32771a145d..95e165ef5a7 100644 --- a/website/docs/r/storagegateway_nfs_file_share.html.markdown +++ b/website/docs/r/storagegateway_nfs_file_share.html.markdown @@ -40,6 +40,7 @@ The following arguments are supported: * `requester_pays` - (Optional) Boolean who pays the cost of the request and the data download from the Amazon S3 bucket. Set this value to `true` if you want the requester to pay instead of the bucket owner. Defaults to `false`. * `squash` - (Optional) Maps a user to anonymous user. Defaults to `RootSquash`. Valid values: `RootSquash` (only root is mapped to anonymous user), `NoSquash` (no one is mapped to anonymous user), `AllSquash` (everyone is mapped to anonymous user) * `file_share_name` - (Optional) The name of the file share. Must be set if an S3 prefix name is set in `location_arn`. +* `notification_policy` - (Optional) The notification policy of the file share. For more information see [AWS Documnetation](https://docs.aws.amazon.com/storagegateway/latest/APIReference/API_CreateNFSFileShare.html#StorageGateway-CreateNFSFileShare-request-NotificationPolicy). Default value is `{}`. * `tags` - (Optional) Key-value map of resource tags ### nfs_file_share_defaults From 7e6bbe254c4aef97a1c1aaef1c96f8c6346ab9d6 Mon Sep 17 00:00:00 2001 From: drfaust92 Date: Fri, 20 Nov 2020 12:39:57 +0200 Subject: [PATCH 2/5] revert kms --- aws/resource_aws_storagegateway_nfs_file_share.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/aws/resource_aws_storagegateway_nfs_file_share.go b/aws/resource_aws_storagegateway_nfs_file_share.go index 1011f2f68b1..bd431f0efaa 100644 --- a/aws/resource_aws_storagegateway_nfs_file_share.go +++ b/aws/resource_aws_storagegateway_nfs_file_share.go @@ -75,10 +75,9 @@ func resourceAwsStorageGatewayNfsFileShare() *schema.Resource { Default: true, }, "kms_encrypted": { - Type: schema.TypeBool, - Optional: true, - Default: false, - RequiredWith: []string{"kms_key_arn"}, + Type: schema.TypeBool, + Optional: true, + Default: false, }, "kms_key_arn": { Type: schema.TypeString, From 984ee1aee0cfb3daf7ff70d012d196525aa2b0c9 Mon Sep 17 00:00:00 2001 From: Ilia Lazebnik Date: Fri, 20 Nov 2020 18:23:37 +0200 Subject: [PATCH 3/5] Update website/docs/r/storagegateway_nfs_file_share.html.markdown Co-authored-by: Kit Ewbank --- website/docs/r/storagegateway_nfs_file_share.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/r/storagegateway_nfs_file_share.html.markdown b/website/docs/r/storagegateway_nfs_file_share.html.markdown index 95e165ef5a7..e439e04dd6d 100644 --- a/website/docs/r/storagegateway_nfs_file_share.html.markdown +++ b/website/docs/r/storagegateway_nfs_file_share.html.markdown @@ -40,7 +40,7 @@ The following arguments are supported: * `requester_pays` - (Optional) Boolean who pays the cost of the request and the data download from the Amazon S3 bucket. Set this value to `true` if you want the requester to pay instead of the bucket owner. Defaults to `false`. * `squash` - (Optional) Maps a user to anonymous user. Defaults to `RootSquash`. Valid values: `RootSquash` (only root is mapped to anonymous user), `NoSquash` (no one is mapped to anonymous user), `AllSquash` (everyone is mapped to anonymous user) * `file_share_name` - (Optional) The name of the file share. Must be set if an S3 prefix name is set in `location_arn`. -* `notification_policy` - (Optional) The notification policy of the file share. For more information see [AWS Documnetation](https://docs.aws.amazon.com/storagegateway/latest/APIReference/API_CreateNFSFileShare.html#StorageGateway-CreateNFSFileShare-request-NotificationPolicy). Default value is `{}`. +* `notification_policy` - (Optional) The notification policy of the file share. For more information see the [AWS Documentation](https://docs.aws.amazon.com/storagegateway/latest/APIReference/API_CreateNFSFileShare.html#StorageGateway-CreateNFSFileShare-request-NotificationPolicy). Default value is `{}`. * `tags` - (Optional) Key-value map of resource tags ### nfs_file_share_defaults From 8eacb831f1defa5bf858b900877a8c6cf0d501f7 Mon Sep 17 00:00:00 2001 From: drfaust92 Date: Sat, 21 Nov 2020 13:43:39 +0200 Subject: [PATCH 4/5] rename to deleted --- aws/internal/service/storagegateway/waiter/waiter.go | 8 ++++---- aws/resource_aws_storagegateway_nfs_file_share.go | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/aws/internal/service/storagegateway/waiter/waiter.go b/aws/internal/service/storagegateway/waiter/waiter.go index be644bcf226..ca90995b307 100644 --- a/aws/internal/service/storagegateway/waiter/waiter.go +++ b/aws/internal/service/storagegateway/waiter/waiter.go @@ -10,7 +10,7 @@ import ( const ( StoredIscsiVolumeAvailableTimeout = 5 * time.Minute NfsFileShareAvailableDelay = 5 * time.Second - NfsFileShareNotFoundDelay = 5 * time.Second + NfsFileShareDeletedDelay = 5 * time.Second ) // StoredIscsiVolumeAvailable waits for a StoredIscsiVolume to return Available @@ -50,13 +50,13 @@ func NfsFileShareAvailable(conn *storagegateway.StorageGateway, fileShareArn str return nil, err } -func NfsFileShareNotFound(conn *storagegateway.StorageGateway, fileShareArn string, timeout time.Duration) (*storagegateway.NFSFileShareInfo, error) { +func NfsFileShareDeleted(conn *storagegateway.StorageGateway, fileShareArn string, timeout time.Duration) (*storagegateway.NFSFileShareInfo, error) { stateConf := &resource.StateChangeConf{ Pending: []string{"AVAILABLE", "DELETING", "FORCE_DELETING"}, - Target: []string{"NotFound"}, + Target: []string{}, Refresh: NfsFileShareStatus(conn, fileShareArn), Timeout: timeout, - Delay: NfsFileShareNotFoundDelay, + Delay: NfsFileShareDeletedDelay, NotFoundChecks: 1, } diff --git a/aws/resource_aws_storagegateway_nfs_file_share.go b/aws/resource_aws_storagegateway_nfs_file_share.go index bd431f0efaa..435a3d2e945 100644 --- a/aws/resource_aws_storagegateway_nfs_file_share.go +++ b/aws/resource_aws_storagegateway_nfs_file_share.go @@ -383,7 +383,7 @@ func resourceAwsStorageGatewayNfsFileShareDelete(d *schema.ResourceData, meta in return fmt.Errorf("error deleting Storage Gateway NFS File Share: %w", err) } - if _, err = waiter.NfsFileShareNotFound(conn, d.Id(), d.Timeout(schema.TimeoutDelete)); err != nil { + if _, err = waiter.NfsFileShareDeleted(conn, d.Id(), d.Timeout(schema.TimeoutDelete)); err != nil { if isResourceNotFoundError(err) { return nil } From de31a131f4b6c99178d8132fba660bd56f216c85 Mon Sep 17 00:00:00 2001 From: drfaust92 Date: Sun, 22 Nov 2020 11:36:29 +0200 Subject: [PATCH 5/5] change group_id and owner_id to string --- ...ource_aws_storagegateway_nfs_file_share.go | 57 +++++++++++++------ ..._aws_storagegateway_nfs_file_share_test.go | 15 +++-- aws/validators.go | 9 +++ 3 files changed, 59 insertions(+), 22 deletions(-) diff --git a/aws/resource_aws_storagegateway_nfs_file_share.go b/aws/resource_aws_storagegateway_nfs_file_share.go index 435a3d2e945..36ef82b6990 100644 --- a/aws/resource_aws_storagegateway_nfs_file_share.go +++ b/aws/resource_aws_storagegateway_nfs_file_share.go @@ -4,6 +4,7 @@ import ( "fmt" "log" "regexp" + "strconv" "time" "github.com/aws/aws-sdk-go/aws" @@ -100,25 +101,25 @@ func resourceAwsStorageGatewayNfsFileShare() *schema.Resource { Type: schema.TypeString, Optional: true, Default: "0777", - ValidateFunc: validation.StringMatch(regexp.MustCompile(`^[0-7]{4}$`), ""), + ValidateFunc: validateLinuxFileMode, }, "file_mode": { Type: schema.TypeString, Optional: true, Default: "0666", - ValidateFunc: validation.StringMatch(regexp.MustCompile(`^[0-7]{4}$`), ""), + ValidateFunc: validateLinuxFileMode, }, "group_id": { - Type: schema.TypeInt, + Type: schema.TypeString, Optional: true, - Default: 65534, - ValidateFunc: validation.IntBetween(0, 4294967294), + Default: "65534", + ValidateFunc: validate4ByteAsn, }, "owner_id": { - Type: schema.TypeInt, + Type: schema.TypeString, Optional: true, - Default: 65534, - ValidateFunc: validation.IntBetween(0, 4294967294), + Default: "65534", + ValidateFunc: validate4ByteAsn, }, }, }, @@ -196,6 +197,11 @@ func resourceAwsStorageGatewayNfsFileShare() *schema.Resource { func resourceAwsStorageGatewayNfsFileShareCreate(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).storagegatewayconn + fileShareDefaults, err := expandStorageGatewayNfsFileShareDefaults(d.Get("nfs_file_share_defaults").([]interface{})) + if err != nil { + return err + } + input := &storagegateway.CreateNFSFileShareInput{ ClientList: expandStringSet(d.Get("client_list").(*schema.Set)), ClientToken: aws.String(resource.UniqueId()), @@ -204,7 +210,7 @@ func resourceAwsStorageGatewayNfsFileShareCreate(d *schema.ResourceData, meta in GuessMIMETypeEnabled: aws.Bool(d.Get("guess_mime_type_enabled").(bool)), KMSEncrypted: aws.Bool(d.Get("kms_encrypted").(bool)), LocationARN: aws.String(d.Get("location_arn").(string)), - NFSFileShareDefaults: expandStorageGatewayNfsFileShareDefaults(d.Get("nfs_file_share_defaults").([]interface{})), + NFSFileShareDefaults: fileShareDefaults, ObjectACL: aws.String(d.Get("object_acl").(string)), ReadOnly: aws.Bool(d.Get("read_only").(bool)), RequesterPays: aws.Bool(d.Get("requester_pays").(bool)), @@ -324,13 +330,18 @@ func resourceAwsStorageGatewayNfsFileShareUpdate(d *schema.ResourceData, meta in "nfs_file_share_defaults", "object_acl", "read_only", "requester_pays", "squash", "kms_key_arn", "cache_attributes", "file_share_name", "notification_policy") { + fileShareDefaults, err := expandStorageGatewayNfsFileShareDefaults(d.Get("nfs_file_share_defaults").([]interface{})) + if err != nil { + return err + } + input := &storagegateway.UpdateNFSFileShareInput{ ClientList: expandStringSet(d.Get("client_list").(*schema.Set)), DefaultStorageClass: aws.String(d.Get("default_storage_class").(string)), FileShareARN: aws.String(d.Id()), GuessMIMETypeEnabled: aws.Bool(d.Get("guess_mime_type_enabled").(bool)), KMSEncrypted: aws.Bool(d.Get("kms_encrypted").(bool)), - NFSFileShareDefaults: expandStorageGatewayNfsFileShareDefaults(d.Get("nfs_file_share_defaults").([]interface{})), + NFSFileShareDefaults: fileShareDefaults, ObjectACL: aws.String(d.Get("object_acl").(string)), ReadOnly: aws.Bool(d.Get("read_only").(bool)), RequesterPays: aws.Bool(d.Get("requester_pays").(bool)), @@ -354,7 +365,7 @@ func resourceAwsStorageGatewayNfsFileShareUpdate(d *schema.ResourceData, meta in } log.Printf("[DEBUG] Updating Storage Gateway NFS File Share: %s", input) - _, err := conn.UpdateNFSFileShare(input) + _, err = conn.UpdateNFSFileShare(input) if err != nil { return fmt.Errorf("error updating Storage Gateway NFS File Share: %w", err) } @@ -393,21 +404,31 @@ func resourceAwsStorageGatewayNfsFileShareDelete(d *schema.ResourceData, meta in return nil } -func expandStorageGatewayNfsFileShareDefaults(l []interface{}) *storagegateway.NFSFileShareDefaults { +func expandStorageGatewayNfsFileShareDefaults(l []interface{}) (*storagegateway.NFSFileShareDefaults, error) { if len(l) == 0 || l[0] == nil { - return nil + return nil, nil } m := l[0].(map[string]interface{}) + groupID, err := strconv.ParseInt(m["group_id"].(string), 10, 64) + if err != nil { + return nil, err + } + + ownerID, err := strconv.ParseInt(m["owner_id"].(string), 10, 64) + if err != nil { + return nil, err + } + nfsFileShareDefaults := &storagegateway.NFSFileShareDefaults{ DirectoryMode: aws.String(m["directory_mode"].(string)), FileMode: aws.String(m["file_mode"].(string)), - GroupId: aws.Int64(int64(m["group_id"].(int))), - OwnerId: aws.Int64(int64(m["owner_id"].(int))), + GroupId: aws.Int64(groupID), + OwnerId: aws.Int64(ownerID), } - return nfsFileShareDefaults + return nfsFileShareDefaults, nil } func flattenStorageGatewayNfsFileShareDefaults(nfsFileShareDefaults *storagegateway.NFSFileShareDefaults) []interface{} { @@ -418,8 +439,8 @@ func flattenStorageGatewayNfsFileShareDefaults(nfsFileShareDefaults *storagegate m := map[string]interface{}{ "directory_mode": aws.StringValue(nfsFileShareDefaults.DirectoryMode), "file_mode": aws.StringValue(nfsFileShareDefaults.FileMode), - "group_id": int(aws.Int64Value(nfsFileShareDefaults.GroupId)), - "owner_id": int(aws.Int64Value(nfsFileShareDefaults.OwnerId)), + "group_id": strconv.Itoa(int(aws.Int64Value(nfsFileShareDefaults.GroupId))), + "owner_id": strconv.Itoa(int(aws.Int64Value(nfsFileShareDefaults.OwnerId))), } return []interface{}{m} diff --git a/aws/resource_aws_storagegateway_nfs_file_share_test.go b/aws/resource_aws_storagegateway_nfs_file_share_test.go index 0fd9f70deaa..2b625d691e0 100644 --- a/aws/resource_aws_storagegateway_nfs_file_share_test.go +++ b/aws/resource_aws_storagegateway_nfs_file_share_test.go @@ -665,7 +665,7 @@ func testAccCheckAWSStorageGatewayNfsFileShareExists(resourceName string, nfsFil func testAccAWSStorageGateway_S3FileShareBase(rName string) string { return testAccAWSStorageGateway_FileGatewayBase(rName) + fmt.Sprintf(` resource "aws_iam_role" "test" { - name = %q + name = %[1]q assume_role_policy = <