From 4b5763e60c2f0b510aa115a1d77d65dee21dec56 Mon Sep 17 00:00:00 2001 From: shuheiktgw Date: Wed, 10 Mar 2021 07:37:08 +0900 Subject: [PATCH 01/11] Add efs_file_system_backup_policy resource --- aws/internal/service/efs/finder/finder.go | 24 +++ aws/internal/service/efs/waiter/status.go | 18 +++ aws/internal/service/efs/waiter/waiter.go | 42 ++++- aws/provider.go | 1 + ...ource_aws_efs_file_system_backup_policy.go | 143 ++++++++++++++++++ 5 files changed, 227 insertions(+), 1 deletion(-) create mode 100644 aws/internal/service/efs/finder/finder.go create mode 100644 aws/resource_aws_efs_file_system_backup_policy.go diff --git a/aws/internal/service/efs/finder/finder.go b/aws/internal/service/efs/finder/finder.go new file mode 100644 index 00000000000..efeb33896e5 --- /dev/null +++ b/aws/internal/service/efs/finder/finder.go @@ -0,0 +1,24 @@ +package finder + +import ( + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/efs" +) + +// FileSystemBackupPolicyById returns the EFS Backup Policy corresponding to the specified Id. +// Returns nil if no policy is found. +func FileSystemBackupPolicyById(conn *efs.EFS, id string) (*efs.BackupPolicy, error) { + output, err := conn.DescribeBackupPolicy(&efs.DescribeBackupPolicyInput{ + FileSystemId: aws.String(id), + }) + + if err != nil { + return nil, err + } + + if output == nil { + return nil, nil + } + + return output.BackupPolicy, nil +} diff --git a/aws/internal/service/efs/waiter/status.go b/aws/internal/service/efs/waiter/status.go index 97d4f7d1612..27a8d2e28aa 100644 --- a/aws/internal/service/efs/waiter/status.go +++ b/aws/internal/service/efs/waiter/status.go @@ -4,6 +4,7 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/efs" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/terraform-providers/terraform-provider-aws/aws/internal/service/efs/finder" ) // AccessPointLifeCycleState fetches the Access Point and its LifecycleState @@ -29,6 +30,23 @@ func AccessPointLifeCycleState(conn *efs.EFS, accessPointId string) resource.Sta } } +// FileSystemBackupPolicyStatus fetches the EFS Backup Policy status +func FileSystemBackupPolicyStatus(conn *efs.EFS, id string) resource.StateRefreshFunc { + return func() (interface{}, string, error) { + bp, err := finder.FileSystemBackupPolicyById(conn, id) + + if err != nil { + return nil, "", err + } + + if bp == nil { + return nil, "", nil + } + + return bp, aws.StringValue(bp.Status), nil + } +} + // FileSystemLifeCycleState fetches the Access Point and its LifecycleState func FileSystemLifeCycleState(conn *efs.EFS, fileSystemID string) resource.StateRefreshFunc { return func() (interface{}, string, error) { diff --git a/aws/internal/service/efs/waiter/waiter.go b/aws/internal/service/efs/waiter/waiter.go index fd065e5c89d..47fe932d2a8 100644 --- a/aws/internal/service/efs/waiter/waiter.go +++ b/aws/internal/service/efs/waiter/waiter.go @@ -17,6 +17,10 @@ const ( FileSystemDeletedTimeout = 10 * time.Minute FileSystemDeletedDelayTimeout = 2 * time.Second FileSystemDeletedMinTimeout = 3 * time.Second + + // Maximum amount of time to wait for an EFS Backup policy operation + FileSystemBackupPolicyCreatedTimeout = 10 * time.Minute + FileSystemBackupPolicyDeletedTimeout = 10 * time.Minute ) // AccessPointCreated waits for an Operation to return Success @@ -37,7 +41,7 @@ func AccessPointCreated(conn *efs.EFS, accessPointId string) (*efs.AccessPointDe return nil, err } -// AccessPointDelete waits for an Access Point to return Deleted +// AccessPointDeleted waits for an Access Point to return Deleted func AccessPointDeleted(conn *efs.EFS, accessPointId string) (*efs.AccessPointDescription, error) { stateConf := &resource.StateChangeConf{ Pending: []string{efs.LifeCycleStateAvailable, efs.LifeCycleStateDeleting, efs.LifeCycleStateDeleted}, @@ -94,3 +98,39 @@ func FileSystemDeleted(conn *efs.EFS, fileSystemID string) (*efs.FileSystemDescr return nil, err } + +// FileSystemBackupPolicyCreated waits for a EFS Backup Policy creation +func FileSystemBackupPolicyCreated(conn *efs.EFS, id string) (*efs.BackupPolicy, error) { + stateConf := &resource.StateChangeConf{ + Pending: []string{efs.StatusEnabling}, + Target: []string{efs.StatusEnabled}, + Refresh: FileSystemBackupPolicyStatus(conn, id), + Timeout: FileSystemBackupPolicyCreatedTimeout, + } + + outputRaw, err := stateConf.WaitForState() + + if output, ok := outputRaw.(*efs.BackupPolicy); ok { + return output, err + } + + return nil, err +} + +// FileSystemBackupPolicyDeleted waits for a EFS Backup Policy deletion +func FileSystemBackupPolicyDeleted(conn *efs.EFS, id string) (*efs.BackupPolicy, error) { + stateConf := &resource.StateChangeConf{ + Pending: []string{efs.StatusDisabling}, + Target: []string{efs.StatusDisabled}, + Refresh: FileSystemBackupPolicyStatus(conn, id), + Timeout: FileSystemBackupPolicyDeletedTimeout, + } + + outputRaw, err := stateConf.WaitForState() + + if output, ok := outputRaw.(*efs.BackupPolicy); ok { + return output, err + } + + return nil, err +} diff --git a/aws/provider.go b/aws/provider.go index d0d6e9b9846..5d6981bcf75 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -714,6 +714,7 @@ func Provider() *schema.Provider { "aws_ecs_task_definition": resourceAwsEcsTaskDefinition(), "aws_efs_access_point": resourceAwsEfsAccessPoint(), "aws_efs_file_system": resourceAwsEfsFileSystem(), + "aws_efs_file_system_backup_policy": resourceAwsEfsFileSystemBackupPolicy(), "aws_efs_file_system_policy": resourceAwsEfsFileSystemPolicy(), "aws_efs_mount_target": resourceAwsEfsMountTarget(), "aws_egress_only_internet_gateway": resourceAwsEgressOnlyInternetGateway(), diff --git a/aws/resource_aws_efs_file_system_backup_policy.go b/aws/resource_aws_efs_file_system_backup_policy.go new file mode 100644 index 00000000000..f0d8bf63dd7 --- /dev/null +++ b/aws/resource_aws_efs_file_system_backup_policy.go @@ -0,0 +1,143 @@ +package aws + +import ( + "fmt" + "log" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/efs" + "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/service/efs/finder" + "github.com/terraform-providers/terraform-provider-aws/aws/internal/service/efs/waiter" +) + +func resourceAwsEfsFileSystemBackupPolicy() *schema.Resource { + return &schema.Resource{ + Create: resourceAwsEfsFileSystemBackupPolicyPut, + Read: resourceAwsEfsFileSystemBackupPolicyRead, + Delete: resourceAwsEfsFileSystemBackupPolicyDelete, + + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + + Schema: map[string]*schema.Schema{ + "file_system_id": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "backup_policy": { + Type: schema.TypeList, + Required: true, + ForceNew: true, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "status": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringInSlice([]string{ + efs.StatusEnabled, + }, false), + }, + }, + }, + }, + }, + } +} + +func resourceAwsEfsFileSystemBackupPolicyPut(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).efsconn + + fsId := d.Get("file_system_id").(string) + input := &efs.PutBackupPolicyInput{ + FileSystemId: aws.String(fsId), + } + + if v, ok := d.GetOk("backup_policy"); ok { + input.BackupPolicy = expandEfsFileSystemBackupPolicy(v.([]interface{})) + } + + log.Printf("[DEBUG] Adding EFS File System Backup Policy: %#v", input) + _, err := conn.PutBackupPolicy(input) + if err != nil { + return fmt.Errorf("error creating EFS File System Backup Policy %q: %s", fsId, err.Error()) + } + + d.SetId(fsId) + + if _, err := waiter.FileSystemBackupPolicyCreated(conn, d.Id()); err != nil { + return fmt.Errorf("error waiting for EFS File System Backup Policy (%q) creation : %s", d.Id(), err) + } + + return resourceAwsEfsFileSystemBackupPolicyRead(d, meta) +} + +func resourceAwsEfsFileSystemBackupPolicyRead(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).efsconn + + bp, err := finder.FileSystemBackupPolicyById(conn, d.Id()) + if err != nil { + if isAWSErr(err, efs.ErrCodeFileSystemNotFound, "") { + log.Printf("[WARN] EFS File System (%q) not found, removing from state", d.Id()) + d.SetId("") + return nil + } + if isAWSErr(err, efs.ErrCodePolicyNotFound, "") { + log.Printf("[WARN] EFS File System Backup Policy (%q) not found, removing from state", d.Id()) + d.SetId("") + return nil + } + return fmt.Errorf("error describing policy for EFS File System Backup Policy (%q): %s", d.Id(), err) + } + + if bp == nil || aws.StringValue(bp.Status) == efs.StatusDisabled { + log.Printf("[WARN] EFS File System Backup Policy (%q) is disabled, removing from state", d.Id()) + d.SetId("") + return nil + } + + d.Set("file_system_id", d.Id()) + if err := d.Set("backup_policy", flattenEfsFileSystemBackupPolicy(bp)); err != nil { + return fmt.Errorf("error setting backup_policy: %s", err) + } + + return nil +} + +func resourceAwsEfsFileSystemBackupPolicyDelete(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).efsconn + + log.Printf("[DEBUG] Deleting EFS File System Backup Policy: %s", d.Id()) + _, err := conn.PutBackupPolicy(&efs.PutBackupPolicyInput{ + FileSystemId: aws.String(d.Id()), + BackupPolicy: &efs.BackupPolicy{ + Status: aws.String(efs.StatusDisabled), + }, + }) + + if err != nil { + return fmt.Errorf("error deleting EFS File System Backup Policy: %s with err %s", d.Id(), err.Error()) + } + + if _, err := waiter.FileSystemBackupPolicyDeleted(conn, d.Id()); err != nil { + return fmt.Errorf("error waiting for EFS File System Backup Policy (%q) deletion : %s", d.Id(), err) + } + + log.Printf("[DEBUG] EFS File System Backup Policy %q deleted.", d.Id()) + + return nil +} + +func expandEfsFileSystemBackupPolicy(tfList []interface{}) *efs.BackupPolicy { + return &efs.BackupPolicy{ + Status: aws.String(tfList[0].(map[string]interface{})["status"].(string)), + } +} + +func flattenEfsFileSystemBackupPolicy(apiObjects *efs.BackupPolicy) []interface{} { + return []interface{}{map[string]interface{}{"status": apiObjects.Status}} +} From aa3a359790780293494ef76e22da06e5b9cc0133 Mon Sep 17 00:00:00 2001 From: shuheiktgw Date: Thu, 11 Mar 2021 07:50:40 +0900 Subject: [PATCH 02/11] Add tests for efs_file_system_backup_policy resource --- ..._aws_efs_file_system_backup_policy_test.go | 131 ++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 aws/resource_aws_efs_file_system_backup_policy_test.go diff --git a/aws/resource_aws_efs_file_system_backup_policy_test.go b/aws/resource_aws_efs_file_system_backup_policy_test.go new file mode 100644 index 00000000000..6ad5ed7b5ba --- /dev/null +++ b/aws/resource_aws_efs_file_system_backup_policy_test.go @@ -0,0 +1,131 @@ +package aws + +import ( + "fmt" + "testing" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/efs" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" +) + +func TestAccAWSEFSFileSystemBackupPolicy_basic(t *testing.T) { + var out efs.DescribeBackupPolicyOutput + resourceName := "aws_efs_file_system_backup_policy.test" + rName := acctest.RandomWithPrefix("tf-acc-test") + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckEfsFileSystemBackupPolicyDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSEFSFileSystemBackupPolicyConfig(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckEFSFileSystemBackupPolicyExists(resourceName, &out), + resource.TestCheckResourceAttr(resourceName, "backup_policy.#", "1"), + resource.TestCheckResourceAttr(resourceName, "backup_policy.0.status", "ENABLED"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func TestAccAWSEFSFileSystemBackupPolicy_disappears(t *testing.T) { + var out efs.DescribeBackupPolicyOutput + resourceName := "aws_efs_file_system_backup_policy.test" + rName := acctest.RandomWithPrefix("tf-acc-test") + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckEfsFileSystemBackupPolicyDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSEFSFileSystemBackupPolicyConfig(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckEFSFileSystemBackupPolicyExists(resourceName, &out), + testAccCheckResourceDisappears(testAccProvider, resourceAwsEfsFileSystemBackupPolicy(), resourceName), + ), + ExpectNonEmptyPlan: true, + }, + }, + }) +} + +func testAccCheckEFSFileSystemBackupPolicyExists(name string, bp *efs.DescribeBackupPolicyOutput) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[name] + if !ok { + return fmt.Errorf("not found: %s", name) + } + + if rs.Primary.ID == "" { + return fmt.Errorf("no ID is set") + } + + conn := testAccProvider.Meta().(*AWSClient).efsconn + fs, err := conn.DescribeBackupPolicy(&efs.DescribeBackupPolicyInput{ + FileSystemId: aws.String(rs.Primary.ID), + }) + + if err != nil { + return err + } + + *bp = *fs + + return nil + } +} + +func testAccCheckEfsFileSystemBackupPolicyDestroy(s *terraform.State) error { + conn := testAccProvider.Meta().(*AWSClient).efsconn + for _, rs := range s.RootModule().Resources { + if rs.Type != "aws_efs_file_system_backup_policy" { + continue + } + + resp, err := conn.DescribeBackupPolicy(&efs.DescribeBackupPolicyInput{ + FileSystemId: aws.String(rs.Primary.ID), + }) + if err != nil { + if isAWSErr(err, efs.ErrCodeFileSystemNotFound, "") || + isAWSErr(err, efs.ErrCodePolicyNotFound, "") { + return nil + } + return fmt.Errorf("error describing EFS file system backup policy in tests: %s", err) + } + + if resp == nil || resp.BackupPolicy == nil || aws.StringValue(resp.BackupPolicy.Status) == efs.StatusDisabled { + return nil + } + + return fmt.Errorf("EFS file system backup policy %q still exists", rs.Primary.ID) + } + + return nil +} + +func testAccAWSEFSFileSystemBackupPolicyConfig(rName string) string { + return fmt.Sprintf(` +resource "aws_efs_file_system" "test" { + creation_token = %q +} + +resource "aws_efs_file_system_backup_policy" "test" { + file_system_id = aws_efs_file_system.test.id + + backup_policy { + status = "ENABLED" + } +} +`, rName) +} From d577344e500558df078aac6d15f20741ce9f93ca Mon Sep 17 00:00:00 2001 From: shuheiktgw Date: Thu, 11 Mar 2021 07:57:53 +0900 Subject: [PATCH 03/11] Add document for efs_file_system_backup_policy resource --- ...fs_file_system_backup_policy.html.markdown | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 website/docs/r/efs_file_system_backup_policy.html.markdown diff --git a/website/docs/r/efs_file_system_backup_policy.html.markdown b/website/docs/r/efs_file_system_backup_policy.html.markdown new file mode 100644 index 00000000000..1925b2eb6f7 --- /dev/null +++ b/website/docs/r/efs_file_system_backup_policy.html.markdown @@ -0,0 +1,53 @@ +--- +subcategory: "EFS" +layout: "aws" +page_title: "AWS: aws_efs_file_system_backup_policy" +description: |- + Provides an Elastic File System (EFS) File System Backup Policy resource. +--- + +# Resource: aws_efs_file_system_backup_policy + +Provides an Elastic File System (EFS) File System Backup Policy resource. + +## Example Usage + +```hcl +resource "aws_efs_file_system" "fs" { + creation_token = "my-product" +} + +resource "aws_efs_file_system_backup_policy" "policy" { + file_system_id = aws_efs_file_system.fs.id + + backup_policy { + status = "ENABLED" + } +} +``` + +## Argument Reference + +The following arguments are supported: + +* `file_system_id` - (Required) The ID of the EFS file system. +* `backup_policy` - (Required) A file system backup_policy object (documented below). + +### Backup Policy Arguments +For **backup_policy** the following attributes are supported: + +* `status` - (Required) A status of the backup policy. Valid values: `Enabled`. + +## Attributes Reference + +In addition to all arguments above, the following attributes are exported: + +* `id` - The ID that identifies the file system (e.g. fs-ccfc0d65). + +## Import + +The EFS file system backup policies can be imported using the `id`, e.g. + +``` +$ terraform import aws_efs_file_system_backup_policy.foo fs-6fa144c6 +``` From 73c85478bb5ea7b01e4cc85d8396dfb67aff2702 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Tue, 22 Jun 2021 17:03:59 -0400 Subject: [PATCH 04/11] r/aws_efs_file_system_backup_policy: Fix awsproviderlint 'XAT001: missing ErrorCheck' errors. --- aws/resource_aws_efs_file_system_backup_policy_test.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/aws/resource_aws_efs_file_system_backup_policy_test.go b/aws/resource_aws_efs_file_system_backup_policy_test.go index 6ad5ed7b5ba..a266af9ef59 100644 --- a/aws/resource_aws_efs_file_system_backup_policy_test.go +++ b/aws/resource_aws_efs_file_system_backup_policy_test.go @@ -18,6 +18,7 @@ func TestAccAWSEFSFileSystemBackupPolicy_basic(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, + ErrorCheck: testAccErrorCheck(t, efs.EndpointsID), Providers: testAccProviders, CheckDestroy: testAccCheckEfsFileSystemBackupPolicyDestroy, Steps: []resource.TestStep{ @@ -45,6 +46,7 @@ func TestAccAWSEFSFileSystemBackupPolicy_disappears(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, + ErrorCheck: testAccErrorCheck(t, efs.EndpointsID), Providers: testAccProviders, CheckDestroy: testAccCheckEfsFileSystemBackupPolicyDestroy, Steps: []resource.TestStep{ @@ -117,7 +119,7 @@ func testAccCheckEfsFileSystemBackupPolicyDestroy(s *terraform.State) error { func testAccAWSEFSFileSystemBackupPolicyConfig(rName string) string { return fmt.Sprintf(` resource "aws_efs_file_system" "test" { - creation_token = %q + creation_token = %[1]q } resource "aws_efs_file_system_backup_policy" "test" { From 301f5f4ec4093845d441d7345f885d2fcf995e0a Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Tue, 22 Jun 2021 17:04:31 -0400 Subject: [PATCH 05/11] Fix tfproviderdocs linting errors. --- website/docs/r/efs_file_system_backup_policy.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/website/docs/r/efs_file_system_backup_policy.html.markdown b/website/docs/r/efs_file_system_backup_policy.html.markdown index 1925b2eb6f7..46687892db5 100644 --- a/website/docs/r/efs_file_system_backup_policy.html.markdown +++ b/website/docs/r/efs_file_system_backup_policy.html.markdown @@ -12,7 +12,7 @@ Provides an Elastic File System (EFS) File System Backup Policy resource. ## Example Usage -```hcl +```terraform resource "aws_efs_file_system" "fs" { creation_token = "my-product" } @@ -49,5 +49,5 @@ In addition to all arguments above, the following attributes are exported: The EFS file system backup policies can be imported using the `id`, e.g. ``` -$ terraform import aws_efs_file_system_backup_policy.foo fs-6fa144c6 +$ terraform import aws_efs_file_system_backup_policy.example fs-6fa144c6 ``` From 07d391cb18fdd5c0a4c23c1c3471604585399e30 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Tue, 22 Jun 2021 17:58:17 -0400 Subject: [PATCH 06/11] r/aws_efs_file_system_backup_policy: Allow status to be updated. --- aws/internal/service/efs/finder/finder.go | 26 ++- aws/internal/service/efs/waiter/status.go | 16 +- aws/internal/service/efs/waiter/waiter.go | 27 ++-- ...ource_aws_efs_file_system_backup_policy.go | 152 +++++++++++------- ..._aws_efs_file_system_backup_policy_test.go | 65 +++++--- ...fs_file_system_backup_policy.html.markdown | 2 +- 6 files changed, 177 insertions(+), 111 deletions(-) diff --git a/aws/internal/service/efs/finder/finder.go b/aws/internal/service/efs/finder/finder.go index efeb33896e5..244884f792d 100644 --- a/aws/internal/service/efs/finder/finder.go +++ b/aws/internal/service/efs/finder/finder.go @@ -3,21 +3,33 @@ package finder import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/efs" + "github.com/hashicorp/aws-sdk-go-base/tfawserr" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" ) -// FileSystemBackupPolicyById returns the EFS Backup Policy corresponding to the specified Id. -// Returns nil if no policy is found. -func FileSystemBackupPolicyById(conn *efs.EFS, id string) (*efs.BackupPolicy, error) { - output, err := conn.DescribeBackupPolicy(&efs.DescribeBackupPolicyInput{ +func BackupPolicyByID(conn *efs.EFS, id string) (*efs.BackupPolicy, error) { + input := &efs.DescribeBackupPolicyInput{ FileSystemId: aws.String(id), - }) + } + + output, err := conn.DescribeBackupPolicy(input) + + if tfawserr.ErrCodeEquals(err, efs.ErrCodeFileSystemNotFound) { + return nil, &resource.NotFoundError{ + LastError: err, + LastRequest: input, + } + } if err != nil { return nil, err } - if output == nil { - return nil, nil + if output == nil || output.BackupPolicy == nil { + return nil, &resource.NotFoundError{ + Message: "Empty result", + LastRequest: input, + } } return output.BackupPolicy, nil diff --git a/aws/internal/service/efs/waiter/status.go b/aws/internal/service/efs/waiter/status.go index 27a8d2e28aa..b539a8be0b5 100644 --- a/aws/internal/service/efs/waiter/status.go +++ b/aws/internal/service/efs/waiter/status.go @@ -5,6 +5,7 @@ import ( "github.com/aws/aws-sdk-go/service/efs" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/terraform-providers/terraform-provider-aws/aws/internal/service/efs/finder" + "github.com/terraform-providers/terraform-provider-aws/aws/internal/tfresource" ) // AccessPointLifeCycleState fetches the Access Point and its LifecycleState @@ -30,20 +31,19 @@ func AccessPointLifeCycleState(conn *efs.EFS, accessPointId string) resource.Sta } } -// FileSystemBackupPolicyStatus fetches the EFS Backup Policy status -func FileSystemBackupPolicyStatus(conn *efs.EFS, id string) resource.StateRefreshFunc { +func BackupPolicyStatus(conn *efs.EFS, id string) resource.StateRefreshFunc { return func() (interface{}, string, error) { - bp, err := finder.FileSystemBackupPolicyById(conn, id) + output, err := finder.BackupPolicyByID(conn, id) - if err != nil { - return nil, "", err + if tfresource.NotFound(err) { + return nil, "", nil } - if bp == nil { - return nil, "", nil + if err != nil { + return nil, "", err } - return bp, aws.StringValue(bp.Status), nil + return output, aws.StringValue(output.Status), nil } } diff --git a/aws/internal/service/efs/waiter/waiter.go b/aws/internal/service/efs/waiter/waiter.go index 47fe932d2a8..2e18ddccc0c 100644 --- a/aws/internal/service/efs/waiter/waiter.go +++ b/aws/internal/service/efs/waiter/waiter.go @@ -18,9 +18,8 @@ const ( FileSystemDeletedDelayTimeout = 2 * time.Second FileSystemDeletedMinTimeout = 3 * time.Second - // Maximum amount of time to wait for an EFS Backup policy operation - FileSystemBackupPolicyCreatedTimeout = 10 * time.Minute - FileSystemBackupPolicyDeletedTimeout = 10 * time.Minute + BackupPolicyDisabledTimeout = 10 * time.Minute + BackupPolicyEnabledTimeout = 10 * time.Minute ) // AccessPointCreated waits for an Operation to return Success @@ -99,13 +98,12 @@ func FileSystemDeleted(conn *efs.EFS, fileSystemID string) (*efs.FileSystemDescr return nil, err } -// FileSystemBackupPolicyCreated waits for a EFS Backup Policy creation -func FileSystemBackupPolicyCreated(conn *efs.EFS, id string) (*efs.BackupPolicy, error) { +func BackupPolicyDisabled(conn *efs.EFS, id string) (*efs.BackupPolicy, error) { stateConf := &resource.StateChangeConf{ - Pending: []string{efs.StatusEnabling}, - Target: []string{efs.StatusEnabled}, - Refresh: FileSystemBackupPolicyStatus(conn, id), - Timeout: FileSystemBackupPolicyCreatedTimeout, + Pending: []string{efs.StatusDisabling}, + Target: []string{efs.StatusDisabled}, + Refresh: BackupPolicyStatus(conn, id), + Timeout: BackupPolicyDisabledTimeout, } outputRaw, err := stateConf.WaitForState() @@ -117,13 +115,12 @@ func FileSystemBackupPolicyCreated(conn *efs.EFS, id string) (*efs.BackupPolicy, return nil, err } -// FileSystemBackupPolicyDeleted waits for a EFS Backup Policy deletion -func FileSystemBackupPolicyDeleted(conn *efs.EFS, id string) (*efs.BackupPolicy, error) { +func BackupPolicyEnabled(conn *efs.EFS, id string) (*efs.BackupPolicy, error) { stateConf := &resource.StateChangeConf{ - Pending: []string{efs.StatusDisabling}, - Target: []string{efs.StatusDisabled}, - Refresh: FileSystemBackupPolicyStatus(conn, id), - Timeout: FileSystemBackupPolicyDeletedTimeout, + Pending: []string{efs.StatusEnabling}, + Target: []string{efs.StatusEnabled}, + Refresh: BackupPolicyStatus(conn, id), + Timeout: BackupPolicyEnabledTimeout, } outputRaw, err := stateConf.WaitForState() diff --git a/aws/resource_aws_efs_file_system_backup_policy.go b/aws/resource_aws_efs_file_system_backup_policy.go index f0d8bf63dd7..3a568e6e676 100644 --- a/aws/resource_aws_efs_file_system_backup_policy.go +++ b/aws/resource_aws_efs_file_system_backup_policy.go @@ -6,32 +6,28 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/efs" + "github.com/hashicorp/aws-sdk-go-base/tfawserr" "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/service/efs/finder" "github.com/terraform-providers/terraform-provider-aws/aws/internal/service/efs/waiter" + "github.com/terraform-providers/terraform-provider-aws/aws/internal/tfresource" ) func resourceAwsEfsFileSystemBackupPolicy() *schema.Resource { return &schema.Resource{ - Create: resourceAwsEfsFileSystemBackupPolicyPut, + Create: resourceAwsEfsFileSystemBackupPolicyCreate, Read: resourceAwsEfsFileSystemBackupPolicyRead, + Update: resourceAwsEfsFileSystemBackupPolicyUpdate, Delete: resourceAwsEfsFileSystemBackupPolicyDelete, - Importer: &schema.ResourceImporter{ State: schema.ImportStatePassthrough, }, Schema: map[string]*schema.Schema{ - "file_system_id": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - }, "backup_policy": { Type: schema.TypeList, Required: true, - ForceNew: true, MaxItems: 1, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ @@ -39,39 +35,33 @@ func resourceAwsEfsFileSystemBackupPolicy() *schema.Resource { Type: schema.TypeString, Required: true, ValidateFunc: validation.StringInSlice([]string{ + efs.StatusDisabled, efs.StatusEnabled, }, false), }, }, }, }, + + "file_system_id": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, }, } } -func resourceAwsEfsFileSystemBackupPolicyPut(d *schema.ResourceData, meta interface{}) error { +func resourceAwsEfsFileSystemBackupPolicyCreate(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).efsconn - fsId := d.Get("file_system_id").(string) - input := &efs.PutBackupPolicyInput{ - FileSystemId: aws.String(fsId), - } + fsID := d.Get("file_system_id").(string) - if v, ok := d.GetOk("backup_policy"); ok { - input.BackupPolicy = expandEfsFileSystemBackupPolicy(v.([]interface{})) + if err := efsBackupPolicyPut(conn, fsID, d.Get("backup_policy").([]interface{})[0].(map[string]interface{})); err != nil { + return err } - log.Printf("[DEBUG] Adding EFS File System Backup Policy: %#v", input) - _, err := conn.PutBackupPolicy(input) - if err != nil { - return fmt.Errorf("error creating EFS File System Backup Policy %q: %s", fsId, err.Error()) - } - - d.SetId(fsId) - - if _, err := waiter.FileSystemBackupPolicyCreated(conn, d.Id()); err != nil { - return fmt.Errorf("error waiting for EFS File System Backup Policy (%q) creation : %s", d.Id(), err) - } + d.SetId(fsID) return resourceAwsEfsFileSystemBackupPolicyRead(d, meta) } @@ -79,65 +69,107 @@ func resourceAwsEfsFileSystemBackupPolicyPut(d *schema.ResourceData, meta interf func resourceAwsEfsFileSystemBackupPolicyRead(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).efsconn - bp, err := finder.FileSystemBackupPolicyById(conn, d.Id()) - if err != nil { - if isAWSErr(err, efs.ErrCodeFileSystemNotFound, "") { - log.Printf("[WARN] EFS File System (%q) not found, removing from state", d.Id()) - d.SetId("") - return nil - } - if isAWSErr(err, efs.ErrCodePolicyNotFound, "") { - log.Printf("[WARN] EFS File System Backup Policy (%q) not found, removing from state", d.Id()) - d.SetId("") - return nil - } - return fmt.Errorf("error describing policy for EFS File System Backup Policy (%q): %s", d.Id(), err) - } + output, err := finder.BackupPolicyByID(conn, d.Id()) - if bp == nil || aws.StringValue(bp.Status) == efs.StatusDisabled { - log.Printf("[WARN] EFS File System Backup Policy (%q) is disabled, removing from state", d.Id()) + if !d.IsNewResource() && tfresource.NotFound(err) { + log.Printf("[WARN] EFS Backup Policy (%s) not found, removing from state", d.Id()) d.SetId("") return nil } - d.Set("file_system_id", d.Id()) - if err := d.Set("backup_policy", flattenEfsFileSystemBackupPolicy(bp)); err != nil { - return fmt.Errorf("error setting backup_policy: %s", err) + if err != nil { + return fmt.Errorf("error reading EFS Backup Policy (%s): %w", d.Id(), err) + } + + if err := d.Set("backup_policy", []interface{}{flattenEfsBackupPolicy(output)}); err != nil { + return fmt.Errorf("error setting backup_policy: %w", err) } + d.Set("file_system_id", d.Id()) + return nil } +func resourceAwsEfsFileSystemBackupPolicyUpdate(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).efsconn + + if err := efsBackupPolicyPut(conn, d.Id(), d.Get("backup_policy").([]interface{})[0].(map[string]interface{})); err != nil { + return err + } + + return resourceAwsEfsFileSystemBackupPolicyRead(d, meta) +} + func resourceAwsEfsFileSystemBackupPolicyDelete(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).efsconn - log.Printf("[DEBUG] Deleting EFS File System Backup Policy: %s", d.Id()) - _, err := conn.PutBackupPolicy(&efs.PutBackupPolicyInput{ - FileSystemId: aws.String(d.Id()), - BackupPolicy: &efs.BackupPolicy{ - Status: aws.String(efs.StatusDisabled), - }, + err := efsBackupPolicyPut(conn, d.Id(), map[string]interface{}{ + "status": efs.StatusDisabled, }) + if tfawserr.ErrCodeEquals(err, efs.ErrCodeFileSystemNotFound) { + return nil + } + if err != nil { - return fmt.Errorf("error deleting EFS File System Backup Policy: %s with err %s", d.Id(), err.Error()) + return err + } + + return nil +} + +// efsBackupPolicyPut attempts to update the file system's backup policy. +// Any error is returned. +func efsBackupPolicyPut(conn *efs.EFS, fsID string, tfMap map[string]interface{}) error { + input := &efs.PutBackupPolicyInput{ + BackupPolicy: expandEfsBackupPolicy(tfMap), + FileSystemId: aws.String(fsID), } - if _, err := waiter.FileSystemBackupPolicyDeleted(conn, d.Id()); err != nil { - return fmt.Errorf("error waiting for EFS File System Backup Policy (%q) deletion : %s", d.Id(), err) + log.Printf("[DEBUG] Putting EFS Backup Policy: %s", input) + _, err := conn.PutBackupPolicy(input) + + if err != nil { + return fmt.Errorf("error putting EFS Backup Policy (%s): %w", fsID, err) } - log.Printf("[DEBUG] EFS File System Backup Policy %q deleted.", d.Id()) + if aws.StringValue(input.BackupPolicy.Status) == efs.StatusEnabled { + if _, err := waiter.BackupPolicyEnabled(conn, fsID); err != nil { + return fmt.Errorf("error waiting for EFS Backup Policy (%s) to enable: %w", fsID, err) + } + } else { + if _, err := waiter.BackupPolicyDisabled(conn, fsID); err != nil { + return fmt.Errorf("error waiting for EFS Backup Policy (%s) to disable: %w", fsID, err) + } + } return nil } -func expandEfsFileSystemBackupPolicy(tfList []interface{}) *efs.BackupPolicy { - return &efs.BackupPolicy{ - Status: aws.String(tfList[0].(map[string]interface{})["status"].(string)), +func expandEfsBackupPolicy(tfMap map[string]interface{}) *efs.BackupPolicy { + if tfMap == nil { + return nil + } + + apiObject := &efs.BackupPolicy{} + + if v, ok := tfMap["status"].(string); ok && v != "" { + apiObject.Status = aws.String(v) } + + return apiObject } -func flattenEfsFileSystemBackupPolicy(apiObjects *efs.BackupPolicy) []interface{} { - return []interface{}{map[string]interface{}{"status": apiObjects.Status}} +func flattenEfsBackupPolicy(apiObject *efs.BackupPolicy) map[string]interface{} { + if apiObject == nil { + return nil + } + + tfMap := map[string]interface{}{} + + if v := apiObject.Status; v != nil { + tfMap["status"] = aws.StringValue(v) + } + + return tfMap } diff --git a/aws/resource_aws_efs_file_system_backup_policy_test.go b/aws/resource_aws_efs_file_system_backup_policy_test.go index a266af9ef59..27b5842d931 100644 --- a/aws/resource_aws_efs_file_system_backup_policy_test.go +++ b/aws/resource_aws_efs_file_system_backup_policy_test.go @@ -9,10 +9,12 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" + "github.com/terraform-providers/terraform-provider-aws/aws/internal/service/efs/finder" + "github.com/terraform-providers/terraform-provider-aws/aws/internal/tfresource" ) func TestAccAWSEFSFileSystemBackupPolicy_basic(t *testing.T) { - var out efs.DescribeBackupPolicyOutput + var v efs.BackupPolicy resourceName := "aws_efs_file_system_backup_policy.test" rName := acctest.RandomWithPrefix("tf-acc-test") @@ -25,7 +27,7 @@ func TestAccAWSEFSFileSystemBackupPolicy_basic(t *testing.T) { { Config: testAccAWSEFSFileSystemBackupPolicyConfig(rName), Check: resource.ComposeTestCheckFunc( - testAccCheckEFSFileSystemBackupPolicyExists(resourceName, &out), + testAccCheckEFSFileSystemBackupPolicyExists(resourceName, &v), resource.TestCheckResourceAttr(resourceName, "backup_policy.#", "1"), resource.TestCheckResourceAttr(resourceName, "backup_policy.0.status", "ENABLED"), ), @@ -40,7 +42,7 @@ func TestAccAWSEFSFileSystemBackupPolicy_basic(t *testing.T) { } func TestAccAWSEFSFileSystemBackupPolicy_disappears(t *testing.T) { - var out efs.DescribeBackupPolicyOutput + var v efs.BackupPolicy resourceName := "aws_efs_file_system_backup_policy.test" rName := acctest.RandomWithPrefix("tf-acc-test") @@ -53,7 +55,7 @@ func TestAccAWSEFSFileSystemBackupPolicy_disappears(t *testing.T) { { Config: testAccAWSEFSFileSystemBackupPolicyConfig(rName), Check: resource.ComposeTestCheckFunc( - testAccCheckEFSFileSystemBackupPolicyExists(resourceName, &out), + testAccCheckEFSFileSystemBackupPolicyExists(resourceName, &v), testAccCheckResourceDisappears(testAccProvider, resourceAwsEfsFileSystemBackupPolicy(), resourceName), ), ExpectNonEmptyPlan: true, @@ -62,7 +64,31 @@ func TestAccAWSEFSFileSystemBackupPolicy_disappears(t *testing.T) { }) } -func testAccCheckEFSFileSystemBackupPolicyExists(name string, bp *efs.DescribeBackupPolicyOutput) resource.TestCheckFunc { +func TestAccAWSEFSFileSystemBackupPolicy_disappears_fs(t *testing.T) { + var v efs.BackupPolicy + resourceName := "aws_efs_file_system_backup_policy.test" + fsResourceName := "aws_efs_file_system.text" + rName := acctest.RandomWithPrefix("tf-acc-test") + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ErrorCheck: testAccErrorCheck(t, efs.EndpointsID), + Providers: testAccProviders, + CheckDestroy: testAccCheckEfsFileSystemBackupPolicyDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSEFSFileSystemBackupPolicyConfig(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckEFSFileSystemBackupPolicyExists(resourceName, &v), + testAccCheckResourceDisappears(testAccProvider, resourceAwsEfsFileSystem(), fsResourceName), + ), + ExpectNonEmptyPlan: true, + }, + }, + }) +} + +func testAccCheckEFSFileSystemBackupPolicyExists(name string, v *efs.BackupPolicy) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[name] if !ok { @@ -74,15 +100,14 @@ func testAccCheckEFSFileSystemBackupPolicyExists(name string, bp *efs.DescribeBa } conn := testAccProvider.Meta().(*AWSClient).efsconn - fs, err := conn.DescribeBackupPolicy(&efs.DescribeBackupPolicyInput{ - FileSystemId: aws.String(rs.Primary.ID), - }) + + output, err := finder.BackupPolicyByID(conn, rs.Primary.ID) if err != nil { return err } - *bp = *fs + *v = *output return nil } @@ -90,27 +115,27 @@ func testAccCheckEFSFileSystemBackupPolicyExists(name string, bp *efs.DescribeBa func testAccCheckEfsFileSystemBackupPolicyDestroy(s *terraform.State) error { conn := testAccProvider.Meta().(*AWSClient).efsconn + for _, rs := range s.RootModule().Resources { if rs.Type != "aws_efs_file_system_backup_policy" { continue } - resp, err := conn.DescribeBackupPolicy(&efs.DescribeBackupPolicyInput{ - FileSystemId: aws.String(rs.Primary.ID), - }) + output, err := finder.BackupPolicyByID(conn, rs.Primary.ID) + + if tfresource.NotFound(err) { + continue + } + if err != nil { - if isAWSErr(err, efs.ErrCodeFileSystemNotFound, "") || - isAWSErr(err, efs.ErrCodePolicyNotFound, "") { - return nil - } - return fmt.Errorf("error describing EFS file system backup policy in tests: %s", err) + return err } - if resp == nil || resp.BackupPolicy == nil || aws.StringValue(resp.BackupPolicy.Status) == efs.StatusDisabled { - return nil + if aws.StringValue(output.Status) == efs.StatusDisabled { + continue } - return fmt.Errorf("EFS file system backup policy %q still exists", rs.Primary.ID) + return fmt.Errorf("Transfer Server %s still exists", rs.Primary.ID) } return nil diff --git a/website/docs/r/efs_file_system_backup_policy.html.markdown b/website/docs/r/efs_file_system_backup_policy.html.markdown index 46687892db5..c98df2cb734 100644 --- a/website/docs/r/efs_file_system_backup_policy.html.markdown +++ b/website/docs/r/efs_file_system_backup_policy.html.markdown @@ -36,7 +36,7 @@ The following arguments are supported: ### Backup Policy Arguments For **backup_policy** the following attributes are supported: -* `status` - (Required) A status of the backup policy. Valid values: `Enabled`. +* `status` - (Required) A status of the backup policy. Valid values: `ENABLED`, `DISABLED`. ## Attributes Reference From 6f09221a66324c202cc879ac256157191a75df0a Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Wed, 23 Jun 2021 10:13:27 -0400 Subject: [PATCH 07/11] r/aws_efs_file_system_backup_policy: Additional acceptance tests. Acceptance test output: % make testacc TEST=./aws TESTARGS='-run=TestAccAWSEFSFileSystemBackupPolicy_' ==> Checking that code complies with gofmt requirements... TF_ACC=1 go test ./aws -v -count 1 -parallel 20 -run=TestAccAWSEFSFileSystemBackupPolicy_ -timeout 180m === RUN TestAccAWSEFSFileSystemBackupPolicy_basic === PAUSE TestAccAWSEFSFileSystemBackupPolicy_basic === RUN TestAccAWSEFSFileSystemBackupPolicy_disappears_fs === PAUSE TestAccAWSEFSFileSystemBackupPolicy_disappears_fs === RUN TestAccAWSEFSFileSystemBackupPolicy_update === PAUSE TestAccAWSEFSFileSystemBackupPolicy_update === CONT TestAccAWSEFSFileSystemBackupPolicy_basic === CONT TestAccAWSEFSFileSystemBackupPolicy_update === CONT TestAccAWSEFSFileSystemBackupPolicy_disappears_fs --- PASS: TestAccAWSEFSFileSystemBackupPolicy_disappears_fs (21.47s) --- PASS: TestAccAWSEFSFileSystemBackupPolicy_basic (27.28s) --- PASS: TestAccAWSEFSFileSystemBackupPolicy_update (48.63s) PASS ok github.com/terraform-providers/terraform-provider-aws/aws 53.197s --- ..._aws_efs_file_system_backup_policy_test.go | 45 ++++++++++++++----- 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/aws/resource_aws_efs_file_system_backup_policy_test.go b/aws/resource_aws_efs_file_system_backup_policy_test.go index 27b5842d931..4d08f015ac1 100644 --- a/aws/resource_aws_efs_file_system_backup_policy_test.go +++ b/aws/resource_aws_efs_file_system_backup_policy_test.go @@ -25,7 +25,7 @@ func TestAccAWSEFSFileSystemBackupPolicy_basic(t *testing.T) { CheckDestroy: testAccCheckEfsFileSystemBackupPolicyDestroy, Steps: []resource.TestStep{ { - Config: testAccAWSEFSFileSystemBackupPolicyConfig(rName), + Config: testAccAWSEFSFileSystemBackupPolicyConfig(rName, "ENABLED"), Check: resource.ComposeTestCheckFunc( testAccCheckEFSFileSystemBackupPolicyExists(resourceName, &v), resource.TestCheckResourceAttr(resourceName, "backup_policy.#", "1"), @@ -41,9 +41,10 @@ func TestAccAWSEFSFileSystemBackupPolicy_basic(t *testing.T) { }) } -func TestAccAWSEFSFileSystemBackupPolicy_disappears(t *testing.T) { +func TestAccAWSEFSFileSystemBackupPolicy_disappears_fs(t *testing.T) { var v efs.BackupPolicy resourceName := "aws_efs_file_system_backup_policy.test" + fsResourceName := "aws_efs_file_system.test" rName := acctest.RandomWithPrefix("tf-acc-test") resource.ParallelTest(t, resource.TestCase{ @@ -53,10 +54,10 @@ func TestAccAWSEFSFileSystemBackupPolicy_disappears(t *testing.T) { CheckDestroy: testAccCheckEfsFileSystemBackupPolicyDestroy, Steps: []resource.TestStep{ { - Config: testAccAWSEFSFileSystemBackupPolicyConfig(rName), + Config: testAccAWSEFSFileSystemBackupPolicyConfig(rName, "ENABLED"), Check: resource.ComposeTestCheckFunc( testAccCheckEFSFileSystemBackupPolicyExists(resourceName, &v), - testAccCheckResourceDisappears(testAccProvider, resourceAwsEfsFileSystemBackupPolicy(), resourceName), + testAccCheckResourceDisappears(testAccProvider, resourceAwsEfsFileSystem(), fsResourceName), ), ExpectNonEmptyPlan: true, }, @@ -64,10 +65,9 @@ func TestAccAWSEFSFileSystemBackupPolicy_disappears(t *testing.T) { }) } -func TestAccAWSEFSFileSystemBackupPolicy_disappears_fs(t *testing.T) { +func TestAccAWSEFSFileSystemBackupPolicy_update(t *testing.T) { var v efs.BackupPolicy resourceName := "aws_efs_file_system_backup_policy.test" - fsResourceName := "aws_efs_file_system.text" rName := acctest.RandomWithPrefix("tf-acc-test") resource.ParallelTest(t, resource.TestCase{ @@ -77,12 +77,33 @@ func TestAccAWSEFSFileSystemBackupPolicy_disappears_fs(t *testing.T) { CheckDestroy: testAccCheckEfsFileSystemBackupPolicyDestroy, Steps: []resource.TestStep{ { - Config: testAccAWSEFSFileSystemBackupPolicyConfig(rName), + Config: testAccAWSEFSFileSystemBackupPolicyConfig(rName, "DISABLED"), Check: resource.ComposeTestCheckFunc( testAccCheckEFSFileSystemBackupPolicyExists(resourceName, &v), - testAccCheckResourceDisappears(testAccProvider, resourceAwsEfsFileSystem(), fsResourceName), + resource.TestCheckResourceAttr(resourceName, "backup_policy.#", "1"), + resource.TestCheckResourceAttr(resourceName, "backup_policy.0.status", "DISABLED"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccAWSEFSFileSystemBackupPolicyConfig(rName, "ENABLED"), + Check: resource.ComposeTestCheckFunc( + testAccCheckEFSFileSystemBackupPolicyExists(resourceName, &v), + resource.TestCheckResourceAttr(resourceName, "backup_policy.#", "1"), + resource.TestCheckResourceAttr(resourceName, "backup_policy.0.status", "ENABLED"), + ), + }, + { + Config: testAccAWSEFSFileSystemBackupPolicyConfig(rName, "DISABLED"), + Check: resource.ComposeTestCheckFunc( + testAccCheckEFSFileSystemBackupPolicyExists(resourceName, &v), + resource.TestCheckResourceAttr(resourceName, "backup_policy.#", "1"), + resource.TestCheckResourceAttr(resourceName, "backup_policy.0.status", "DISABLED"), ), - ExpectNonEmptyPlan: true, }, }, }) @@ -141,7 +162,7 @@ func testAccCheckEfsFileSystemBackupPolicyDestroy(s *terraform.State) error { return nil } -func testAccAWSEFSFileSystemBackupPolicyConfig(rName string) string { +func testAccAWSEFSFileSystemBackupPolicyConfig(rName, status string) string { return fmt.Sprintf(` resource "aws_efs_file_system" "test" { creation_token = %[1]q @@ -151,8 +172,8 @@ resource "aws_efs_file_system_backup_policy" "test" { file_system_id = aws_efs_file_system.test.id backup_policy { - status = "ENABLED" + status = %[2]q } } -`, rName) +`, rName, status) } From ee0ef97ed7f99c1dc0b6022c00e989dc949f7a51 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Wed, 23 Jun 2021 10:33:24 -0400 Subject: [PATCH 08/11] Rename 'aws_efs_file_system_backup_policy' to 'aws_efs_backup_policy'. Acceptance test output: % make testacc TEST=./aws TESTARGS='-run=TestAccAWSEFSBackupPolicy_' ==> Checking that code complies with gofmt requirements... TF_ACC=1 go test ./aws -v -count 1 -parallel 20 -run=TestAccAWSEFSBackupPolicy_ -timeout 180m === RUN TestAccAWSEFSBackupPolicy_basic === PAUSE TestAccAWSEFSBackupPolicy_basic === RUN TestAccAWSEFSBackupPolicy_disappears_fs === PAUSE TestAccAWSEFSBackupPolicy_disappears_fs === RUN TestAccAWSEFSBackupPolicy_update === PAUSE TestAccAWSEFSBackupPolicy_update === CONT TestAccAWSEFSBackupPolicy_basic === CONT TestAccAWSEFSBackupPolicy_update === CONT TestAccAWSEFSBackupPolicy_disappears_fs --- PASS: TestAccAWSEFSBackupPolicy_disappears_fs (22.38s) --- PASS: TestAccAWSEFSBackupPolicy_basic (28.27s) --- PASS: TestAccAWSEFSBackupPolicy_update (50.06s) PASS ok github.com/terraform-providers/terraform-provider-aws/aws 53.142s --- aws/provider.go | 2 +- ...y.go => resource_aws_efs_backup_policy.go} | 22 ++++----- ...=> resource_aws_efs_backup_policy_test.go} | 48 +++++++++---------- ...rkdown => efs_backup_policy.html.markdown} | 13 ++--- 4 files changed, 43 insertions(+), 42 deletions(-) rename aws/{resource_aws_efs_file_system_backup_policy.go => resource_aws_efs_backup_policy.go} (83%) rename aws/{resource_aws_efs_file_system_backup_policy_test.go => resource_aws_efs_backup_policy_test.go} (69%) rename website/docs/r/{efs_file_system_backup_policy.html.markdown => efs_backup_policy.html.markdown} (67%) diff --git a/aws/provider.go b/aws/provider.go index 5d6981bcf75..1954a1bc0eb 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -713,8 +713,8 @@ func Provider() *schema.Provider { "aws_ecs_service": resourceAwsEcsService(), "aws_ecs_task_definition": resourceAwsEcsTaskDefinition(), "aws_efs_access_point": resourceAwsEfsAccessPoint(), + "aws_efs_backup_policy": resourceAwsEfsBackupPolicy(), "aws_efs_file_system": resourceAwsEfsFileSystem(), - "aws_efs_file_system_backup_policy": resourceAwsEfsFileSystemBackupPolicy(), "aws_efs_file_system_policy": resourceAwsEfsFileSystemPolicy(), "aws_efs_mount_target": resourceAwsEfsMountTarget(), "aws_egress_only_internet_gateway": resourceAwsEgressOnlyInternetGateway(), diff --git a/aws/resource_aws_efs_file_system_backup_policy.go b/aws/resource_aws_efs_backup_policy.go similarity index 83% rename from aws/resource_aws_efs_file_system_backup_policy.go rename to aws/resource_aws_efs_backup_policy.go index 3a568e6e676..854e74cf4cd 100644 --- a/aws/resource_aws_efs_file_system_backup_policy.go +++ b/aws/resource_aws_efs_backup_policy.go @@ -14,12 +14,12 @@ import ( "github.com/terraform-providers/terraform-provider-aws/aws/internal/tfresource" ) -func resourceAwsEfsFileSystemBackupPolicy() *schema.Resource { +func resourceAwsEfsBackupPolicy() *schema.Resource { return &schema.Resource{ - Create: resourceAwsEfsFileSystemBackupPolicyCreate, - Read: resourceAwsEfsFileSystemBackupPolicyRead, - Update: resourceAwsEfsFileSystemBackupPolicyUpdate, - Delete: resourceAwsEfsFileSystemBackupPolicyDelete, + Create: resourceAwsEfsBackupPolicyCreate, + Read: resourceAwsEfsBackupPolicyRead, + Update: resourceAwsEfsBackupPolicyUpdate, + Delete: resourceAwsEfsBackupPolicyDelete, Importer: &schema.ResourceImporter{ State: schema.ImportStatePassthrough, }, @@ -52,7 +52,7 @@ func resourceAwsEfsFileSystemBackupPolicy() *schema.Resource { } } -func resourceAwsEfsFileSystemBackupPolicyCreate(d *schema.ResourceData, meta interface{}) error { +func resourceAwsEfsBackupPolicyCreate(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).efsconn fsID := d.Get("file_system_id").(string) @@ -63,10 +63,10 @@ func resourceAwsEfsFileSystemBackupPolicyCreate(d *schema.ResourceData, meta int d.SetId(fsID) - return resourceAwsEfsFileSystemBackupPolicyRead(d, meta) + return resourceAwsEfsBackupPolicyRead(d, meta) } -func resourceAwsEfsFileSystemBackupPolicyRead(d *schema.ResourceData, meta interface{}) error { +func resourceAwsEfsBackupPolicyRead(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).efsconn output, err := finder.BackupPolicyByID(conn, d.Id()) @@ -90,17 +90,17 @@ func resourceAwsEfsFileSystemBackupPolicyRead(d *schema.ResourceData, meta inter return nil } -func resourceAwsEfsFileSystemBackupPolicyUpdate(d *schema.ResourceData, meta interface{}) error { +func resourceAwsEfsBackupPolicyUpdate(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).efsconn if err := efsBackupPolicyPut(conn, d.Id(), d.Get("backup_policy").([]interface{})[0].(map[string]interface{})); err != nil { return err } - return resourceAwsEfsFileSystemBackupPolicyRead(d, meta) + return resourceAwsEfsBackupPolicyRead(d, meta) } -func resourceAwsEfsFileSystemBackupPolicyDelete(d *schema.ResourceData, meta interface{}) error { +func resourceAwsEfsBackupPolicyDelete(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).efsconn err := efsBackupPolicyPut(conn, d.Id(), map[string]interface{}{ diff --git a/aws/resource_aws_efs_file_system_backup_policy_test.go b/aws/resource_aws_efs_backup_policy_test.go similarity index 69% rename from aws/resource_aws_efs_file_system_backup_policy_test.go rename to aws/resource_aws_efs_backup_policy_test.go index 4d08f015ac1..b45e1dfef24 100644 --- a/aws/resource_aws_efs_file_system_backup_policy_test.go +++ b/aws/resource_aws_efs_backup_policy_test.go @@ -13,21 +13,21 @@ import ( "github.com/terraform-providers/terraform-provider-aws/aws/internal/tfresource" ) -func TestAccAWSEFSFileSystemBackupPolicy_basic(t *testing.T) { +func TestAccAWSEFSBackupPolicy_basic(t *testing.T) { var v efs.BackupPolicy - resourceName := "aws_efs_file_system_backup_policy.test" + resourceName := "aws_efs_backup_policy.test" rName := acctest.RandomWithPrefix("tf-acc-test") resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, ErrorCheck: testAccErrorCheck(t, efs.EndpointsID), Providers: testAccProviders, - CheckDestroy: testAccCheckEfsFileSystemBackupPolicyDestroy, + CheckDestroy: testAccCheckEfsBackupPolicyDestroy, Steps: []resource.TestStep{ { - Config: testAccAWSEFSFileSystemBackupPolicyConfig(rName, "ENABLED"), + Config: testAccAWSEFSBackupPolicyConfig(rName, "ENABLED"), Check: resource.ComposeTestCheckFunc( - testAccCheckEFSFileSystemBackupPolicyExists(resourceName, &v), + testAccCheckEFSBackupPolicyExists(resourceName, &v), resource.TestCheckResourceAttr(resourceName, "backup_policy.#", "1"), resource.TestCheckResourceAttr(resourceName, "backup_policy.0.status", "ENABLED"), ), @@ -41,9 +41,9 @@ func TestAccAWSEFSFileSystemBackupPolicy_basic(t *testing.T) { }) } -func TestAccAWSEFSFileSystemBackupPolicy_disappears_fs(t *testing.T) { +func TestAccAWSEFSBackupPolicy_disappears_fs(t *testing.T) { var v efs.BackupPolicy - resourceName := "aws_efs_file_system_backup_policy.test" + resourceName := "aws_efs_backup_policy.test" fsResourceName := "aws_efs_file_system.test" rName := acctest.RandomWithPrefix("tf-acc-test") @@ -51,12 +51,12 @@ func TestAccAWSEFSFileSystemBackupPolicy_disappears_fs(t *testing.T) { PreCheck: func() { testAccPreCheck(t) }, ErrorCheck: testAccErrorCheck(t, efs.EndpointsID), Providers: testAccProviders, - CheckDestroy: testAccCheckEfsFileSystemBackupPolicyDestroy, + CheckDestroy: testAccCheckEfsBackupPolicyDestroy, Steps: []resource.TestStep{ { - Config: testAccAWSEFSFileSystemBackupPolicyConfig(rName, "ENABLED"), + Config: testAccAWSEFSBackupPolicyConfig(rName, "ENABLED"), Check: resource.ComposeTestCheckFunc( - testAccCheckEFSFileSystemBackupPolicyExists(resourceName, &v), + testAccCheckEFSBackupPolicyExists(resourceName, &v), testAccCheckResourceDisappears(testAccProvider, resourceAwsEfsFileSystem(), fsResourceName), ), ExpectNonEmptyPlan: true, @@ -65,21 +65,21 @@ func TestAccAWSEFSFileSystemBackupPolicy_disappears_fs(t *testing.T) { }) } -func TestAccAWSEFSFileSystemBackupPolicy_update(t *testing.T) { +func TestAccAWSEFSBackupPolicy_update(t *testing.T) { var v efs.BackupPolicy - resourceName := "aws_efs_file_system_backup_policy.test" + resourceName := "aws_efs_backup_policy.test" rName := acctest.RandomWithPrefix("tf-acc-test") resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, ErrorCheck: testAccErrorCheck(t, efs.EndpointsID), Providers: testAccProviders, - CheckDestroy: testAccCheckEfsFileSystemBackupPolicyDestroy, + CheckDestroy: testAccCheckEfsBackupPolicyDestroy, Steps: []resource.TestStep{ { - Config: testAccAWSEFSFileSystemBackupPolicyConfig(rName, "DISABLED"), + Config: testAccAWSEFSBackupPolicyConfig(rName, "DISABLED"), Check: resource.ComposeTestCheckFunc( - testAccCheckEFSFileSystemBackupPolicyExists(resourceName, &v), + testAccCheckEFSBackupPolicyExists(resourceName, &v), resource.TestCheckResourceAttr(resourceName, "backup_policy.#", "1"), resource.TestCheckResourceAttr(resourceName, "backup_policy.0.status", "DISABLED"), ), @@ -90,17 +90,17 @@ func TestAccAWSEFSFileSystemBackupPolicy_update(t *testing.T) { ImportStateVerify: true, }, { - Config: testAccAWSEFSFileSystemBackupPolicyConfig(rName, "ENABLED"), + Config: testAccAWSEFSBackupPolicyConfig(rName, "ENABLED"), Check: resource.ComposeTestCheckFunc( - testAccCheckEFSFileSystemBackupPolicyExists(resourceName, &v), + testAccCheckEFSBackupPolicyExists(resourceName, &v), resource.TestCheckResourceAttr(resourceName, "backup_policy.#", "1"), resource.TestCheckResourceAttr(resourceName, "backup_policy.0.status", "ENABLED"), ), }, { - Config: testAccAWSEFSFileSystemBackupPolicyConfig(rName, "DISABLED"), + Config: testAccAWSEFSBackupPolicyConfig(rName, "DISABLED"), Check: resource.ComposeTestCheckFunc( - testAccCheckEFSFileSystemBackupPolicyExists(resourceName, &v), + testAccCheckEFSBackupPolicyExists(resourceName, &v), resource.TestCheckResourceAttr(resourceName, "backup_policy.#", "1"), resource.TestCheckResourceAttr(resourceName, "backup_policy.0.status", "DISABLED"), ), @@ -109,7 +109,7 @@ func TestAccAWSEFSFileSystemBackupPolicy_update(t *testing.T) { }) } -func testAccCheckEFSFileSystemBackupPolicyExists(name string, v *efs.BackupPolicy) resource.TestCheckFunc { +func testAccCheckEFSBackupPolicyExists(name string, v *efs.BackupPolicy) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[name] if !ok { @@ -134,11 +134,11 @@ func testAccCheckEFSFileSystemBackupPolicyExists(name string, v *efs.BackupPolic } } -func testAccCheckEfsFileSystemBackupPolicyDestroy(s *terraform.State) error { +func testAccCheckEfsBackupPolicyDestroy(s *terraform.State) error { conn := testAccProvider.Meta().(*AWSClient).efsconn for _, rs := range s.RootModule().Resources { - if rs.Type != "aws_efs_file_system_backup_policy" { + if rs.Type != "aws_efs_backup_policy" { continue } @@ -162,13 +162,13 @@ func testAccCheckEfsFileSystemBackupPolicyDestroy(s *terraform.State) error { return nil } -func testAccAWSEFSFileSystemBackupPolicyConfig(rName, status string) string { +func testAccAWSEFSBackupPolicyConfig(rName, status string) string { return fmt.Sprintf(` resource "aws_efs_file_system" "test" { creation_token = %[1]q } -resource "aws_efs_file_system_backup_policy" "test" { +resource "aws_efs_backup_policy" "test" { file_system_id = aws_efs_file_system.test.id backup_policy { diff --git a/website/docs/r/efs_file_system_backup_policy.html.markdown b/website/docs/r/efs_backup_policy.html.markdown similarity index 67% rename from website/docs/r/efs_file_system_backup_policy.html.markdown rename to website/docs/r/efs_backup_policy.html.markdown index c98df2cb734..50a0f45387a 100644 --- a/website/docs/r/efs_file_system_backup_policy.html.markdown +++ b/website/docs/r/efs_backup_policy.html.markdown @@ -1,14 +1,15 @@ --- subcategory: "EFS" layout: "aws" -page_title: "AWS: aws_efs_file_system_backup_policy" +page_title: "AWS: aws_efs_backup_policy" description: |- - Provides an Elastic File System (EFS) File System Backup Policy resource. + Provides an Elastic File System (EFS) Backup Policy resource. --- # Resource: aws_efs_file_system_backup_policy -Provides an Elastic File System (EFS) File System Backup Policy resource. +Provides an Elastic File System (EFS) Backup Policy resource. +Backup policies turn automatic backups on or off for an existing file system. ## Example Usage @@ -17,7 +18,7 @@ resource "aws_efs_file_system" "fs" { creation_token = "my-product" } -resource "aws_efs_file_system_backup_policy" "policy" { +resource "aws_efs_backup_policy" "policy" { file_system_id = aws_efs_file_system.fs.id backup_policy { @@ -31,7 +32,7 @@ resource "aws_efs_file_system_backup_policy" "policy" { The following arguments are supported: * `file_system_id` - (Required) The ID of the EFS file system. -* `backup_policy` - (Required) A file system backup_policy object (documented below). +* `backup_policy` - (Required) A backup_policy object (documented below). ### Backup Policy Arguments For **backup_policy** the following attributes are supported: @@ -46,7 +47,7 @@ In addition to all arguments above, the following attributes are exported: ## Import -The EFS file system backup policies can be imported using the `id`, e.g. +The EFS backup policies can be imported using the `id`, e.g. ``` $ terraform import aws_efs_file_system_backup_policy.example fs-6fa144c6 From 5f0f1c7eca612ee2dba061079a103d0bb2f383c2 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Wed, 23 Jun 2021 10:34:39 -0400 Subject: [PATCH 09/11] Add CHANGELOG entry. --- .changelog/18006.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/18006.txt diff --git a/.changelog/18006.txt b/.changelog/18006.txt new file mode 100644 index 00000000000..544efe7c548 --- /dev/null +++ b/.changelog/18006.txt @@ -0,0 +1,3 @@ +```release-note:new-resource +aws_efs_backup_policy +``` \ No newline at end of file From 63ac7cb07ce234dd7f410753743a74d926894e3f Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Wed, 23 Jun 2021 11:18:53 -0400 Subject: [PATCH 10/11] r/aws_backup_vault: Delete any recovery points during sweep. Acceptance test output: % TEST=./aws SWEEP=us-east-1,us-west-2 SWEEPARGS=-sweep-run=aws_backup_vault make sweep WARNING: This will destroy infrastructure. Use only in development accounts. go test ./aws -v -sweep=us-east-1,us-west-2 -sweep-run=aws_backup_vault -timeout 60m 2021/06/23 11:17:07 [DEBUG] Running Sweepers for region (us-east-1): 2021/06/23 11:17:07 [DEBUG] Sweeper (aws_backup_vault) has dependency (aws_backup_vault_notifications), running.. 2021/06/23 11:17:07 [DEBUG] Running Sweeper (aws_backup_vault_notifications) in region (us-east-1) 2021/06/23 11:17:07 [INFO] AWS Auth provider used: "EnvProvider" 2021/06/23 11:17:07 [DEBUG] Trying to get account information via sts:GetCallerIdentity 2021/06/23 11:17:07 [DEBUG] Trying to get account information via sts:GetCallerIdentity 2021/06/23 11:17:10 [DEBUG] Waiting for state to become: [success] 2021/06/23 11:17:10 [DEBUG] Sweeper (aws_backup_vault) has dependency (aws_backup_vault_policy), running.. 2021/06/23 11:17:10 [DEBUG] Running Sweeper (aws_backup_vault_policy) in region (us-east-1) 2021/06/23 11:17:10 [INFO] Deleting Backup Vault Policies Default 2021/06/23 11:17:10 [DEBUG] Running Sweeper (aws_backup_vault) in region (us-east-1) 2021/06/23 11:17:10 [INFO] Skipping Backup Vault: Default 2021/06/23 11:17:10 [DEBUG] Sweeper (aws_backup_vault_notifications) already ran in region (us-east-1) 2021/06/23 11:17:10 [DEBUG] Sweeper (aws_backup_vault_policy) already ran in region (us-east-1) 2021/06/23 11:17:10 Sweeper Tests ran successfully: - aws_backup_vault_notifications - aws_backup_vault_policy - aws_backup_vault 2021/06/23 11:17:10 [DEBUG] Running Sweepers for region (us-west-2): 2021/06/23 11:17:10 [DEBUG] Sweeper (aws_backup_vault) has dependency (aws_backup_vault_notifications), running.. 2021/06/23 11:17:10 [DEBUG] Running Sweeper (aws_backup_vault_notifications) in region (us-west-2) 2021/06/23 11:17:10 [INFO] AWS Auth provider used: "EnvProvider" 2021/06/23 11:17:10 [DEBUG] Trying to get account information via sts:GetCallerIdentity 2021/06/23 11:17:10 [DEBUG] Trying to get account information via sts:GetCallerIdentity 2021/06/23 11:17:11 [DEBUG] Waiting for state to become: [success] 2021/06/23 11:17:11 [DEBUG] Waiting for state to become: [success] 2021/06/23 11:17:12 [DEBUG] Sweeper (aws_backup_vault) has dependency (aws_backup_vault_policy), running.. 2021/06/23 11:17:12 [DEBUG] Running Sweeper (aws_backup_vault_policy) in region (us-west-2) 2021/06/23 11:17:12 [INFO] Deleting Backup Vault Policies Default 2021/06/23 11:17:13 [INFO] Deleting Backup Vault Policies aws/efs/automatic-backup-vault 2021/06/23 11:17:13 [DEBUG] Running Sweeper (aws_backup_vault) in region (us-west-2) 2021/06/23 11:17:14 [INFO] Skipping Backup Vault: Default 2021/06/23 11:17:14 [INFO] Skipping Backup Vault: aws/efs/automatic-backup-vault 2021/06/23 11:17:14 [DEBUG] Sweeper (aws_backup_vault_notifications) already ran in region (us-west-2) 2021/06/23 11:17:14 [DEBUG] Sweeper (aws_backup_vault_policy) already ran in region (us-west-2) 2021/06/23 11:17:14 Sweeper Tests ran successfully: - aws_backup_vault_notifications - aws_backup_vault_policy - aws_backup_vault ok github.com/terraform-providers/terraform-provider-aws/aws 10.782s --- aws/resource_aws_backup_vault.go | 8 ++-- aws/resource_aws_backup_vault_test.go | 65 ++++++++++++++++++--------- 2 files changed, 48 insertions(+), 25 deletions(-) diff --git a/aws/resource_aws_backup_vault.go b/aws/resource_aws_backup_vault.go index 2b15de218ad..ec0018f6da8 100644 --- a/aws/resource_aws_backup_vault.go +++ b/aws/resource_aws_backup_vault.go @@ -139,13 +139,13 @@ func resourceAwsBackupVaultUpdate(d *schema.ResourceData, meta interface{}) erro func resourceAwsBackupVaultDelete(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).backupconn - input := &backup.DeleteBackupVaultInput{ + log.Printf("[DEBUG] Deleting Backup Vault: %s", d.Id()) + _, err := conn.DeleteBackupVault(&backup.DeleteBackupVaultInput{ BackupVaultName: aws.String(d.Id()), - } + }) - _, err := conn.DeleteBackupVault(input) if err != nil { - return fmt.Errorf("error deleting Backup Vault (%s): %s", d.Id(), err) + return fmt.Errorf("error deleting Backup Vault (%s): %w", d.Id(), err) } return nil diff --git a/aws/resource_aws_backup_vault_test.go b/aws/resource_aws_backup_vault_test.go index fa9e1f2726c..5d2ad43ed48 100644 --- a/aws/resource_aws_backup_vault_test.go +++ b/aws/resource_aws_backup_vault_test.go @@ -3,12 +3,11 @@ package aws import ( "fmt" "log" - "strings" "testing" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/backup" - "github.com/hashicorp/go-multierror" + multierror "github.com/hashicorp/go-multierror" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" @@ -31,12 +30,10 @@ func testSweepBackupVaults(region string) error { if err != nil { return fmt.Errorf("Error getting client: %w", err) } - conn := client.(*AWSClient).backupconn - sweepResources := make([]*testSweepResource, 0) - var errs *multierror.Error - input := &backup.ListBackupVaultsInput{} + var sweeperErrs *multierror.Error + sweepResources := make([]*testSweepResource, 0) err = conn.ListBackupVaultsPages(input, func(page *backup.ListBackupVaultsOutput, lastPage bool) bool { if page == nil { @@ -44,22 +41,48 @@ func testSweepBackupVaults(region string) error { } for _, vault := range page.BackupVaultList { - if vault == nil { - continue + failedToDeleteRecoveryPoint := false + name := aws.StringValue(vault.BackupVaultName) + input := &backup.ListRecoveryPointsByBackupVaultInput{ + BackupVaultName: aws.String(name), } - name := aws.StringValue(vault.BackupVaultName) + err := conn.ListRecoveryPointsByBackupVaultPages(input, func(page *backup.ListRecoveryPointsByBackupVaultOutput, lastPage bool) bool { + if page == nil { + return !lastPage + } + + for _, recoveryPoint := range page.RecoveryPoints { + arn := aws.StringValue(recoveryPoint.RecoveryPointArn) + + log.Printf("[INFO] Deleting Recovery Point (%s) in Backup Vault (%s)", arn, name) + _, err := conn.DeleteRecoveryPoint(&backup.DeleteRecoveryPointInput{ + BackupVaultName: aws.String(name), + RecoveryPointArn: aws.String(arn), + }) - // Ignore Default Backup Vault in region (cannot be deleted) - // and automated Backups that result in AccessDeniedException when deleted - if name == "Default" || strings.Contains(name, "automatic-backup-vault") { + if err != nil { + log.Printf("[WARN] Failed to delete Recovery Point (%s) in Backup Vault (%s): %s", arn, name, err) + failedToDeleteRecoveryPoint = true + } + } + + return !lastPage + }) + + if err != nil { + sweeperErrs = multierror.Append(sweeperErrs, fmt.Errorf("error listing Reovery Points in Backup Vault (%s) for %s: %w", name, region, err)) + } + + // Ignore Default and Automatic EFS Backup Vaults in region (cannot be deleted) + if name == "Default" || name == "aws/efs/automatic-backup-vault" { log.Printf("[INFO] Skipping Backup Vault: %s", name) continue } // Backup Vault deletion only supported when empty // Reference: https://docs.aws.amazon.com/aws-backup/latest/devguide/API_DeleteBackupVault.html - if aws.Int64Value(vault.NumberOfRecoveryPoints) != 0 { + if failedToDeleteRecoveryPoint { log.Printf("[INFO] Skipping Backup Vault (%s): not empty", name) continue } @@ -74,20 +97,20 @@ func testSweepBackupVaults(region string) error { return !lastPage }) - if err != nil { - errs = multierror.Append(errs, fmt.Errorf("error listing Backup Vaults for %s: %w", region, err)) + if testSweepSkipSweepError(err) { + log.Printf("[WARN] Skipping Backup Vaults sweep for %s: %s", region, err) + return sweeperErrs.ErrorOrNil() } - if err := testSweepResourceOrchestrator(sweepResources); err != nil { - errs = multierror.Append(errs, fmt.Errorf("error sweeping Backup Vaults for %s: %w", region, err)) + if err != nil { + sweeperErrs = multierror.Append(sweeperErrs, fmt.Errorf("error listing Backup Vaults for %s: %w", region, err)) } - if testSweepSkipSweepError(errs.ErrorOrNil()) { - log.Printf("[WARN] Skipping Backup Vaults sweep for %s: %s", region, errs) - return nil + if err := testSweepResourceOrchestrator(sweepResources); err != nil { + sweeperErrs = multierror.Append(sweeperErrs, fmt.Errorf("error sweeping Backup Vaults for %s: %w", region, err)) } - return errs.ErrorOrNil() + return sweeperErrs.ErrorOrNil() } func TestAccAwsBackupVault_basic(t *testing.T) { From f7af1ead791c844d7db7e6504a162788793dfb49 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Wed, 23 Jun 2021 11:31:19 -0400 Subject: [PATCH 11/11] Fix tfproviderdocs error. --- website/docs/r/efs_backup_policy.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/website/docs/r/efs_backup_policy.html.markdown b/website/docs/r/efs_backup_policy.html.markdown index 50a0f45387a..bd10cec3910 100644 --- a/website/docs/r/efs_backup_policy.html.markdown +++ b/website/docs/r/efs_backup_policy.html.markdown @@ -6,7 +6,7 @@ description: |- Provides an Elastic File System (EFS) Backup Policy resource. --- -# Resource: aws_efs_file_system_backup_policy +# Resource: aws_efs_backup_policy Provides an Elastic File System (EFS) Backup Policy resource. Backup policies turn automatic backups on or off for an existing file system. @@ -50,5 +50,5 @@ In addition to all arguments above, the following attributes are exported: The EFS backup policies can be imported using the `id`, e.g. ``` -$ terraform import aws_efs_file_system_backup_policy.example fs-6fa144c6 +$ terraform import aws_efs_backup_policy.example fs-6fa144c6 ```