From d46cb8a87f6f563fcab4cfbe67b2d601f9aebeab Mon Sep 17 00:00:00 2001 From: Ilia Lazebnik Date: Fri, 24 Jan 2020 18:02:19 +0200 Subject: [PATCH] resource/aws_efs_file_system: Refactor tagging to standardized TagResource/UntagResource calls, SDK constants, and tests (#11654) Output from acceptance testing in AWS Commercial: ``` --- PASS: TestAccAWSEFSFileSystem_kmsConfigurationWithoutEncryption (25.32s) --- PASS: TestAccAWSEFSFileSystem_pagedTags (45.14s) --- PASS: TestAccAWSEFSFileSystem_ThroughputMode (53.13s) --- PASS: TestAccAWSEFSFileSystem_lifecyclePolicy (67.97s) --- PASS: TestAccAWSEFSFileSystem_basic (70.60s) --- PASS: TestAccAWSEFSFileSystem_kmsKey (75.09s) --- PASS: TestAccAWSEFSFileSystem_lifecyclePolicy_removal (80.93s) --- PASS: TestAccAWSEFSFileSystem_disappears (85.33s) --- PASS: TestAccAWSEFSFileSystem_ProvisionedThroughputInMibps (106.19s) --- PASS: TestAccAWSEFSFileSystem_lifecyclePolicy_update (127.86s) --- PASS: TestAccAWSEFSFileSystem_tags (128.90s) ``` Output from acceptance testing in AWS GovCloud (US) (unrelated test failure fix noted in the review): ``` --- PASS: TestAccAWSEFSFileSystem_kmsConfigurationWithoutEncryption (24.79s) --- FAIL: TestAccAWSEFSFileSystem_basic (27.99s) --- PASS: TestAccAWSEFSFileSystem_disappears (30.58s) --- PASS: TestAccAWSEFSFileSystem_kmsKey (49.76s) --- PASS: TestAccAWSEFSFileSystem_lifecyclePolicy_removal (53.12s) --- PASS: TestAccAWSEFSFileSystem_tags (62.95s) --- PASS: TestAccAWSEFSFileSystem_ProvisionedThroughputInMibps (81.86s) --- PASS: TestAccAWSEFSFileSystem_ThroughputMode (87.62s) --- PASS: TestAccAWSEFSFileSystem_pagedTags (90.78s) --- PASS: TestAccAWSEFSFileSystem_lifecyclePolicy (117.45s) --- PASS: TestAccAWSEFSFileSystem_lifecyclePolicy_update (123.65s) ``` --- .../generators/updatetags/main.go | 6 +- aws/internal/keyvaluetags/update_tags_gen.go | 16 +- aws/resource_aws_efs_file_system.go | 20 +- aws/resource_aws_efs_file_system_test.go | 279 +++++++++++------- 4 files changed, 180 insertions(+), 141 deletions(-) diff --git a/aws/internal/keyvaluetags/generators/updatetags/main.go b/aws/internal/keyvaluetags/generators/updatetags/main.go index 59ed89713df..1aac566b7af 100644 --- a/aws/internal/keyvaluetags/generators/updatetags/main.go +++ b/aws/internal/keyvaluetags/generators/updatetags/main.go @@ -333,8 +333,6 @@ func ServiceTagFunction(serviceName string) string { return "AddTagsToResource" case "ec2": return "CreateTags" - case "efs": - return "CreateTags" case "elasticache": return "AddTagsToResource" case "elasticbeanstalk": @@ -424,7 +422,7 @@ func ServiceTagInputIdentifierField(serviceName string) string { case "ec2": return "Resources" case "efs": - return "FileSystemId" + return "ResourceId" case "elasticache": return "ResourceName" case "elasticsearchservice": @@ -577,8 +575,6 @@ func ServiceUntagFunction(serviceName string) string { return "RemoveTagsFromResource" case "ec2": return "DeleteTags" - case "efs": - return "DeleteTags" case "elasticache": return "RemoveTagsFromResource" case "elasticbeanstalk": diff --git a/aws/internal/keyvaluetags/update_tags_gen.go b/aws/internal/keyvaluetags/update_tags_gen.go index 7088dd08a63..7fb24e63292 100644 --- a/aws/internal/keyvaluetags/update_tags_gen.go +++ b/aws/internal/keyvaluetags/update_tags_gen.go @@ -1442,12 +1442,12 @@ func EfsUpdateTags(conn *efs.EFS, identifier string, oldTagsMap interface{}, new newTags := New(newTagsMap) if removedTags := oldTags.Removed(newTags); len(removedTags) > 0 { - input := &efs.DeleteTagsInput{ - FileSystemId: aws.String(identifier), - TagKeys: aws.StringSlice(removedTags.IgnoreAws().Keys()), + input := &efs.UntagResourceInput{ + ResourceId: aws.String(identifier), + TagKeys: aws.StringSlice(removedTags.IgnoreAws().Keys()), } - _, err := conn.DeleteTags(input) + _, err := conn.UntagResource(input) if err != nil { return fmt.Errorf("error untagging resource (%s): %w", identifier, err) @@ -1455,12 +1455,12 @@ func EfsUpdateTags(conn *efs.EFS, identifier string, oldTagsMap interface{}, new } if updatedTags := oldTags.Updated(newTags); len(updatedTags) > 0 { - input := &efs.CreateTagsInput{ - FileSystemId: aws.String(identifier), - Tags: updatedTags.IgnoreAws().EfsTags(), + input := &efs.TagResourceInput{ + ResourceId: aws.String(identifier), + Tags: updatedTags.IgnoreAws().EfsTags(), } - _, err := conn.CreateTags(input) + _, err := conn.TagResource(input) if err != nil { return fmt.Errorf("error tagging resource (%s): %w", identifier, err) diff --git a/aws/resource_aws_efs_file_system.go b/aws/resource_aws_efs_file_system.go index 5d2e49493f4..1a310d6ced1 100644 --- a/aws/resource_aws_efs_file_system.go +++ b/aws/resource_aws_efs_file_system.go @@ -8,7 +8,6 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/arn" - "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/service/efs" "github.com/hashicorp/terraform-plugin-sdk/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" @@ -263,7 +262,7 @@ func resourceAwsEfsFileSystemRead(d *schema.ResourceData, meta interface{}) erro FileSystemId: aws.String(d.Id()), }) if err != nil { - if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "FileSystemNotFound" { + if isAWSErr(err, efs.ErrCodeFileSystemNotFound, "") { log.Printf("[WARN] EFS file system (%s) could not be found.", d.Id()) d.SetId("") return nil @@ -275,16 +274,6 @@ func resourceAwsEfsFileSystemRead(d *schema.ResourceData, meta interface{}) erro return fmt.Errorf("EFS file system %q could not be found.", d.Id()) } - tags, err := keyvaluetags.EfsListTags(conn, d.Id()) - - if err != nil { - return fmt.Errorf("error listing tags for EFS file system (%s): %s", d.Id(), err) - } - - if err := d.Set("tags", tags.IgnoreAws().Map()); err != nil { - return fmt.Errorf("error settings tags: %s", err) - } - var fs *efs.FileSystemDescription for _, f := range resp.FileSystems { if d.Id() == *f.FileSystemId { @@ -314,6 +303,10 @@ func resourceAwsEfsFileSystemRead(d *schema.ResourceData, meta interface{}) erro d.Set("provisioned_throughput_in_mibps", fs.ProvisionedThroughputInMibps) d.Set("throughput_mode", fs.ThroughputMode) + if err := d.Set("tags", keyvaluetags.EfsKeyValueTags(fs.Tags).IgnoreAws().Map()); err != nil { + return fmt.Errorf("error setting tags: %s", err) + } + region := meta.(*AWSClient).region if err := d.Set("dns_name", resourceAwsEfsDnsName(aws.StringValue(fs.FileSystemId), region)); err != nil { return fmt.Errorf("error setting dns_name: %s", err) @@ -363,8 +356,7 @@ func waitForDeleteEfsFileSystem(conn *efs.EFS, id string, timeout time.Duration) FileSystemId: aws.String(id), }) if err != nil { - efsErr, ok := err.(awserr.Error) - if ok && efsErr.Code() == "FileSystemNotFound" { + if isAWSErr(err, efs.ErrCodeFileSystemNotFound, "") { return nil, "", nil } return nil, "error", err diff --git a/aws/resource_aws_efs_file_system_test.go b/aws/resource_aws_efs_file_system_test.go index e470d35ae1f..82d02845ac2 100644 --- a/aws/resource_aws_efs_file_system_test.go +++ b/aws/resource_aws_efs_file_system_test.go @@ -8,7 +8,6 @@ import ( "time" "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/service/efs" multierror "github.com/hashicorp/go-multierror" @@ -86,8 +85,9 @@ func TestResourceAWSEFSFileSystem_hasEmptyFileSystems(t *testing.T) { } func TestAccAWSEFSFileSystem_basic(t *testing.T) { - rInt := acctest.RandInt() + var desc efs.FileSystemDescription resourceName := "aws_efs_file_system.test" + rName := acctest.RandomWithPrefix("tf-acc") resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -95,29 +95,14 @@ func TestAccAWSEFSFileSystem_basic(t *testing.T) { CheckDestroy: testAccCheckEfsFileSystemDestroy, Steps: []resource.TestStep{ { - Config: testAccAWSEFSFileSystemConfig, + Config: testAccAWSEFSFileSystemConfig(rName), Check: resource.ComposeTestCheckFunc( - testAccMatchResourceAttrRegionalARN("aws_efs_file_system.test", "arn", "elasticfilesystem", regexp.MustCompile(`file-system/fs-.+`)), - resource.TestCheckResourceAttr( - "aws_efs_file_system.test", - "performance_mode", - "generalPurpose"), - resource.TestCheckResourceAttr( - "aws_efs_file_system.test", - "throughput_mode", - efs.ThroughputModeBursting), - testAccCheckEfsFileSystem( - "aws_efs_file_system.test", - ), - testAccCheckEfsFileSystemPerformanceMode( - "aws_efs_file_system.test", - "generalPurpose", - ), - resource.TestMatchResourceAttr( - "aws_efs_file_system.test", - "dns_name", - regexp.MustCompile("^[^.]+.efs.us-west-2.amazonaws.com$"), - ), + testAccMatchResourceAttrRegionalARN(resourceName, "arn", "elasticfilesystem", regexp.MustCompile(`file-system/fs-.+`)), + resource.TestCheckResourceAttr(resourceName, "performance_mode", "generalPurpose"), + resource.TestCheckResourceAttr(resourceName, "throughput_mode", efs.ThroughputModeBursting), + testAccCheckEfsFileSystem(resourceName, &desc), + testAccCheckEfsFileSystemPerformanceMode(resourceName, "generalPurpose"), + resource.TestMatchResourceAttr(resourceName, "dns_name", regexp.MustCompile("^[^.]+.efs.us-west-2.amazonaws.com$")), ), }, { @@ -127,50 +112,66 @@ func TestAccAWSEFSFileSystem_basic(t *testing.T) { ImportStateVerifyIgnore: []string{"creation_token"}, }, { - Config: testAccAWSEFSFileSystemConfigWithTags(rInt), + Config: testAccAWSEFSFileSystemConfigWithPerformanceMode, Check: resource.ComposeTestCheckFunc( - testAccCheckEfsFileSystem( - "aws_efs_file_system.test", - ), - testAccCheckEfsFileSystemPerformanceMode( - "aws_efs_file_system.test", - "generalPurpose", - ), - resource.TestCheckResourceAttr("aws_efs_file_system.test", "tags.%", "2"), - resource.TestCheckResourceAttr("aws_efs_file_system.test", "tags.Name", fmt.Sprintf("test-efs-%d", rInt)), - resource.TestCheckResourceAttr("aws_efs_file_system.test", "tags.Another", "tag"), + testAccCheckEfsFileSystem("aws_efs_file_system.test2", &desc), + testAccCheckEfsCreationToken("aws_efs_file_system.test2", "supercalifragilisticexpialidocious"), + testAccCheckEfsFileSystemPerformanceMode("aws_efs_file_system.test2", "maxIO"), ), }, + }, + }) +} + +func TestAccAWSEFSFileSystem_tags(t *testing.T) { + var desc efs.FileSystemDescription + rName := acctest.RandomWithPrefix("tf-acc-tags") + resourceName := "aws_efs_file_system.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckEfsFileSystemDestroy, + Steps: []resource.TestStep{ { - Config: testAccAWSEFSFileSystemConfigWithMaxTags(rInt), + Config: testAccAWSEFSFileSystemConfigTags1(rName, "key1", "value1"), Check: resource.ComposeTestCheckFunc( - testAccCheckEfsFileSystem( - "aws_efs_file_system.test1", - ), - testAccCheckEfsFileSystemPerformanceMode( - "aws_efs_file_system.test1", - "generalPurpose", - ), - resource.TestCheckResourceAttr("aws_efs_file_system.test1", "tags.%", "50"), - resource.TestCheckResourceAttr("aws_efs_file_system.test1", "tags.Name", fmt.Sprintf("test-efs-%d", rInt)), - resource.TestCheckResourceAttr("aws_efs_file_system.test1", "tags.Another", "tag"), - resource.TestCheckResourceAttr("aws_efs_file_system.test1", "tags.Tag45", "TestTagValue"), + testAccCheckEfsFileSystem(resourceName, &desc), + resource.TestCheckResourceAttr(resourceName, "tags.%", "1"), + resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1"), ), }, { - Config: testAccAWSEFSFileSystemConfigWithPerformanceMode, + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"creation_token"}, + }, + { + Config: testAccAWSEFSFileSystemConfigTags2(rName, "key1", "value1updated", "key2", "value2"), Check: resource.ComposeTestCheckFunc( - testAccCheckEfsFileSystem( - "aws_efs_file_system.test2", - ), - testAccCheckEfsCreationToken( - "aws_efs_file_system.test2", - "supercalifragilisticexpialidocious", - ), - testAccCheckEfsFileSystemPerformanceMode( - "aws_efs_file_system.test2", - "maxIO", - ), + testAccCheckEfsFileSystem(resourceName, &desc), + resource.TestCheckResourceAttr(resourceName, "tags.%", "2"), + resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1updated"), + resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"), + ), + }, + { + Config: testAccAWSEFSFileSystemConfigTags1(rName, "key2", "value2"), + Check: resource.ComposeTestCheckFunc( + testAccCheckEfsFileSystem(resourceName, &desc), + resource.TestCheckResourceAttr(resourceName, "tags.%", "1"), + resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"), + ), + }, + { + Config: testAccAWSEFSFileSystemConfigWithMaxTags(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckEfsFileSystem(resourceName, &desc), + resource.TestCheckResourceAttr(resourceName, "tags.%", "50"), + resource.TestCheckResourceAttr(resourceName, "tags.Name", rName), + resource.TestCheckResourceAttr(resourceName, "tags.Another", "tag"), + resource.TestCheckResourceAttr(resourceName, "tags.Tag45", "TestTagValue"), ), }, }, @@ -178,6 +179,7 @@ func TestAccAWSEFSFileSystem_basic(t *testing.T) { } func TestAccAWSEFSFileSystem_pagedTags(t *testing.T) { + var desc efs.FileSystemDescription rInt := acctest.RandInt() resourceName := "aws_efs_file_system.test" @@ -189,17 +191,8 @@ func TestAccAWSEFSFileSystem_pagedTags(t *testing.T) { { Config: testAccAWSEFSFileSystemConfigPagedTags(rInt), Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr( - "aws_efs_file_system.test", - "tags.%", - "11"), - //testAccCheckEfsFileSystem( - // "aws_efs_file_system.test", - //), - //testAccCheckEfsFileSystemPerformanceMode( - // "aws_efs_file_system.test", - // "generalPurpose", - //), + testAccCheckEfsFileSystem(resourceName, &desc), + resource.TestCheckResourceAttr(resourceName, "tags.%", "11"), ), }, { @@ -213,6 +206,7 @@ func TestAccAWSEFSFileSystem_pagedTags(t *testing.T) { } func TestAccAWSEFSFileSystem_kmsKey(t *testing.T) { + var desc efs.FileSystemDescription rInt := acctest.RandInt() kmsKeyResourceName := "aws_kms_key.test" resourceName := "aws_efs_file_system.test" @@ -225,8 +219,9 @@ func TestAccAWSEFSFileSystem_kmsKey(t *testing.T) { { Config: testAccAWSEFSFileSystemConfigWithKmsKey(rInt), Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrPair("aws_efs_file_system.test", "kms_key_id", kmsKeyResourceName, "arn"), - resource.TestCheckResourceAttr("aws_efs_file_system.test", "encrypted", "true"), + testAccCheckEfsFileSystem(resourceName, &desc), + resource.TestCheckResourceAttrPair(resourceName, "kms_key_id", kmsKeyResourceName, "arn"), + resource.TestCheckResourceAttr(resourceName, "encrypted", "true"), ), }, { @@ -256,6 +251,7 @@ func TestAccAWSEFSFileSystem_kmsConfigurationWithoutEncryption(t *testing.T) { } func TestAccAWSEFSFileSystem_ProvisionedThroughputInMibps(t *testing.T) { + var desc efs.FileSystemDescription resourceName := "aws_efs_file_system.test" resource.ParallelTest(t, resource.TestCase{ @@ -266,7 +262,7 @@ func TestAccAWSEFSFileSystem_ProvisionedThroughputInMibps(t *testing.T) { { Config: testAccAWSEFSFileSystemConfig_ProvisionedThroughputInMibps(1.0), Check: resource.ComposeTestCheckFunc( - testAccCheckEfsFileSystem(resourceName), + testAccCheckEfsFileSystem(resourceName, &desc), resource.TestCheckResourceAttr(resourceName, "provisioned_throughput_in_mibps", "1"), resource.TestCheckResourceAttr(resourceName, "throughput_mode", efs.ThroughputModeProvisioned), ), @@ -274,7 +270,7 @@ func TestAccAWSEFSFileSystem_ProvisionedThroughputInMibps(t *testing.T) { { Config: testAccAWSEFSFileSystemConfig_ProvisionedThroughputInMibps(2.0), Check: resource.ComposeTestCheckFunc( - testAccCheckEfsFileSystem(resourceName), + testAccCheckEfsFileSystem(resourceName, &desc), resource.TestCheckResourceAttr(resourceName, "provisioned_throughput_in_mibps", "2"), resource.TestCheckResourceAttr(resourceName, "throughput_mode", efs.ThroughputModeProvisioned), ), @@ -290,6 +286,7 @@ func TestAccAWSEFSFileSystem_ProvisionedThroughputInMibps(t *testing.T) { } func TestAccAWSEFSFileSystem_ThroughputMode(t *testing.T) { + var desc efs.FileSystemDescription resourceName := "aws_efs_file_system.test" resource.ParallelTest(t, resource.TestCase{ @@ -300,7 +297,7 @@ func TestAccAWSEFSFileSystem_ThroughputMode(t *testing.T) { { Config: testAccAWSEFSFileSystemConfig_ProvisionedThroughputInMibps(1.0), Check: resource.ComposeTestCheckFunc( - testAccCheckEfsFileSystem(resourceName), + testAccCheckEfsFileSystem(resourceName, &desc), resource.TestCheckResourceAttr(resourceName, "provisioned_throughput_in_mibps", "1"), resource.TestCheckResourceAttr(resourceName, "throughput_mode", efs.ThroughputModeProvisioned), ), @@ -308,7 +305,7 @@ func TestAccAWSEFSFileSystem_ThroughputMode(t *testing.T) { { Config: testAccAWSEFSFileSystemConfig_ThroughputMode(efs.ThroughputModeBursting), Check: resource.ComposeTestCheckFunc( - testAccCheckEfsFileSystem(resourceName), + testAccCheckEfsFileSystem(resourceName, &desc), resource.TestCheckResourceAttr(resourceName, "provisioned_throughput_in_mibps", "0"), resource.TestCheckResourceAttr(resourceName, "throughput_mode", efs.ThroughputModeBursting), ), @@ -324,6 +321,7 @@ func TestAccAWSEFSFileSystem_ThroughputMode(t *testing.T) { } func TestAccAWSEFSFileSystem_lifecyclePolicy(t *testing.T) { + var desc efs.FileSystemDescription resourceName := "aws_efs_file_system.test" resource.ParallelTest(t, resource.TestCase{ @@ -344,7 +342,7 @@ func TestAccAWSEFSFileSystem_lifecyclePolicy(t *testing.T) { efs.TransitionToIARulesAfter30Days, ), Check: resource.ComposeTestCheckFunc( - testAccCheckEfsFileSystem(resourceName), + testAccCheckEfsFileSystem(resourceName, &desc), testAccCheckEfsFileSystemLifecyclePolicy(resourceName, "badExpectation"), ), ExpectError: regexp.MustCompile(`Expected: badExpectation`), @@ -355,7 +353,7 @@ func TestAccAWSEFSFileSystem_lifecyclePolicy(t *testing.T) { efs.TransitionToIARulesAfter30Days, ), Check: resource.ComposeTestCheckFunc( - testAccCheckEfsFileSystem(resourceName), + testAccCheckEfsFileSystem(resourceName, &desc), testAccCheckEfsFileSystemLifecyclePolicy(resourceName, efs.TransitionToIARulesAfter30Days), ), }, @@ -369,6 +367,7 @@ func TestAccAWSEFSFileSystem_lifecyclePolicy(t *testing.T) { } func TestAccAWSEFSFileSystem_lifecyclePolicy_update(t *testing.T) { + var desc efs.FileSystemDescription resourceName := "aws_efs_file_system.test" resource.ParallelTest(t, resource.TestCase{ @@ -377,22 +376,16 @@ func TestAccAWSEFSFileSystem_lifecyclePolicy_update(t *testing.T) { CheckDestroy: testAccCheckEfsFileSystemDestroy, Steps: []resource.TestStep{ { - Config: testAccAWSEFSFileSystemConfigWithLifecyclePolicy( - "transition_to_ia", - efs.TransitionToIARulesAfter30Days, - ), + Config: testAccAWSEFSFileSystemConfigWithLifecyclePolicy("transition_to_ia", efs.TransitionToIARulesAfter30Days), Check: resource.ComposeTestCheckFunc( - testAccCheckEfsFileSystem(resourceName), + testAccCheckEfsFileSystem(resourceName, &desc), testAccCheckEfsFileSystemLifecyclePolicy(resourceName, efs.TransitionToIARulesAfter30Days), ), }, { - Config: testAccAWSEFSFileSystemConfigWithLifecyclePolicy( - "transition_to_ia", - efs.TransitionToIARulesAfter90Days, - ), + Config: testAccAWSEFSFileSystemConfigWithLifecyclePolicy("transition_to_ia", efs.TransitionToIARulesAfter90Days), Check: resource.ComposeTestCheckFunc( - testAccCheckEfsFileSystem(resourceName), + testAccCheckEfsFileSystem(resourceName, &desc), testAccCheckEfsFileSystemLifecyclePolicy(resourceName, efs.TransitionToIARulesAfter90Days), ), }, @@ -406,6 +399,7 @@ func TestAccAWSEFSFileSystem_lifecyclePolicy_update(t *testing.T) { } func TestAccAWSEFSFileSystem_lifecyclePolicy_removal(t *testing.T) { + var desc efs.FileSystemDescription resourceName := "aws_efs_file_system.test" resource.ParallelTest(t, resource.TestCase{ @@ -414,19 +408,16 @@ func TestAccAWSEFSFileSystem_lifecyclePolicy_removal(t *testing.T) { CheckDestroy: testAccCheckEfsFileSystemDestroy, Steps: []resource.TestStep{ { - Config: testAccAWSEFSFileSystemConfigWithLifecyclePolicy( - "transition_to_ia", - efs.TransitionToIARulesAfter14Days, - ), + Config: testAccAWSEFSFileSystemConfigWithLifecyclePolicy("transition_to_ia", efs.TransitionToIARulesAfter14Days), Check: resource.ComposeTestCheckFunc( - testAccCheckEfsFileSystem(resourceName), + testAccCheckEfsFileSystem(resourceName, &desc), testAccCheckEfsFileSystemLifecyclePolicy(resourceName, efs.TransitionToIARulesAfter14Days), ), }, { Config: testAccAWSEFSFileSystemConfigRemovedLifecyclePolicy, Check: resource.ComposeTestCheckFunc( - testAccCheckEfsFileSystem(resourceName), + testAccCheckEfsFileSystem(resourceName, &desc), testAccCheckEfsFileSystemLifecyclePolicy(resourceName, efs.TransitionToIARulesAfter14Days), ), ExpectError: regexp.MustCompile(fmt.Sprintf(`Expected: %s`, efs.TransitionToIARulesAfter14Days)), @@ -440,6 +431,28 @@ func TestAccAWSEFSFileSystem_lifecyclePolicy_removal(t *testing.T) { }) } +func TestAccAWSEFSFileSystem_disappears(t *testing.T) { + var desc efs.FileSystemDescription + resourceName := "aws_efs_file_system.test" + rName := acctest.RandomWithPrefix("tf-acc-disappears") + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckEfsFileSystemDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSEFSFileSystemConfig(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckEfsFileSystem(resourceName, &desc), + testAccCheckEfsFileSystemDisappears(&desc), + ), + ExpectNonEmptyPlan: true, + }, + }, + }) +} + func testAccCheckEfsFileSystemDestroy(s *terraform.State) error { conn := testAccProvider.Meta().(*AWSClient).efsconn for _, rs := range s.RootModule().Resources { @@ -451,7 +464,7 @@ func testAccCheckEfsFileSystemDestroy(s *terraform.State) error { FileSystemId: aws.String(rs.Primary.ID), }) if err != nil { - if efsErr, ok := err.(awserr.Error); ok && efsErr.Code() == "FileSystemNotFound" { + if isAWSErr(err, efs.ErrCodeFileSystemNotFound, "") { // gone return nil } @@ -465,7 +478,7 @@ func testAccCheckEfsFileSystemDestroy(s *terraform.State) error { return nil } -func testAccCheckEfsFileSystem(resourceID string) resource.TestCheckFunc { +func testAccCheckEfsFileSystem(resourceID string, fDesc *efs.FileSystemDescription) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[resourceID] if !ok { @@ -477,10 +490,34 @@ func testAccCheckEfsFileSystem(resourceID string) resource.TestCheckFunc { } conn := testAccProvider.Meta().(*AWSClient).efsconn - _, err := conn.DescribeFileSystems(&efs.DescribeFileSystemsInput{ + fs, err := conn.DescribeFileSystems(&efs.DescribeFileSystemsInput{ FileSystemId: aws.String(rs.Primary.ID), }) + if err != nil { + return err + } + + if len(fs.FileSystems) == 0 { + return fmt.Errorf("EFS File System not found") + } + + *fDesc = *fs.FileSystems[0] + + return nil + } +} + +func testAccCheckEfsFileSystemDisappears(fDesc *efs.FileSystemDescription) resource.TestCheckFunc { + return func(s *terraform.State) error { + conn := testAccProvider.Meta().(*AWSClient).efsconn + + input := &efs.DeleteFileSystemInput{ + FileSystemId: fDesc.FileSystemId, + } + + _, err := conn.DeleteFileSystem(input) + return err } } @@ -585,11 +622,36 @@ func testAccCheckEfsFileSystemLifecyclePolicy(resourceID string, expectedVal str } } -const testAccAWSEFSFileSystemConfig = ` +func testAccAWSEFSFileSystemConfig(rName string) string { + return fmt.Sprintf(` resource "aws_efs_file_system" "test" { - creation_token = "radeksimko" + creation_token = %q +} +`, rName) +} + +func testAccAWSEFSFileSystemConfigTags1(rName, tagKey1, tagValue1 string) string { + return fmt.Sprintf(` +resource "aws_efs_file_system" "test" { + creation_token = %[1]q + tags = { + %[2]q = %[3]q + } +} +`, rName, tagKey1, tagValue1) +} + +func testAccAWSEFSFileSystemConfigTags2(rName, tagKey1, tagValue1, tagKey2, tagValue2 string) string { + return fmt.Sprintf(` +resource "aws_efs_file_system" "test" { + creation_token = %[1]q + tags = { + %[2]q = %[3]q + %[4]q = %[5]q + } +} +`, rName, tagKey1, tagValue1, tagKey2, tagValue2) } -` func testAccAWSEFSFileSystemConfigPagedTags(rInt int) string { return fmt.Sprintf(` @@ -611,22 +673,11 @@ resource "aws_efs_file_system" "test" { `, rInt) } -func testAccAWSEFSFileSystemConfigWithTags(rInt int) string { +func testAccAWSEFSFileSystemConfigWithMaxTags(rName string) string { return fmt.Sprintf(` resource "aws_efs_file_system" "test" { tags = { - Name = "test-efs-%d" - Another = "tag" - } -} -`, rInt) -} - -func testAccAWSEFSFileSystemConfigWithMaxTags(rInt int) string { - return fmt.Sprintf(` -resource "aws_efs_file_system" "test1" { - tags = { - Name = "test-efs-%d" + Name = %q Another = "tag" Tag00 = "TestTagValue" @@ -679,7 +730,7 @@ resource "aws_efs_file_system" "test1" { Tag47 = "TestTagValue" } } -`, rInt) +`, rName) } const testAccAWSEFSFileSystemConfigWithPerformanceMode = `