Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Backup Region Settings #16114

Merged
merged 8 commits into from
Nov 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,7 @@ func Provider() *schema.Provider {
"aws_autoscaling_schedule": resourceAwsAutoscalingSchedule(),
"aws_autoscalingplans_scaling_plan": resourceAwsAutoScalingPlansScalingPlan(),
"aws_backup_plan": resourceAwsBackupPlan(),
"aws_backup_region_settings": resourceAwsBackupRegionSettings(),
"aws_backup_selection": resourceAwsBackupSelection(),
"aws_backup_vault": resourceAwsBackupVault(),
"aws_backup_vault_notifications": resourceAwsBackupVaultNotifications(),
Expand Down
65 changes: 65 additions & 0 deletions aws/resource_aws_backup_region_settings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package aws

import (
"fmt"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/backup"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func resourceAwsBackupRegionSettings() *schema.Resource {
return &schema.Resource{
Create: resourceAwsBackupRegionSettingsUpdate,
Update: resourceAwsBackupRegionSettingsUpdate,
Read: resourceAwsBackupRegionSettingsRead,
Delete: schema.Noop,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"resource_type_opt_in_preference": {
Type: schema.TypeMap,
Required: true,
Elem: &schema.Schema{Type: schema.TypeBool},
},
},
}
}

func resourceAwsBackupRegionSettingsUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).backupconn

prefrences := d.Get("resource_type_opt_in_preference").(map[string]interface{})
list := make(map[string]*bool, len(prefrences))
for i, v := range prefrences {
list[i] = aws.Bool(v.(bool))
}

input := &backup.UpdateRegionSettingsInput{
ResourceTypeOptInPreference: list,
}

_, err := conn.UpdateRegionSettings(input)
if err != nil {
return fmt.Errorf("error setting Backup Region Settings (%s): %w", d.Id(), err)
}

d.SetId(meta.(*AWSClient).region)

return resourceAwsBackupRegionSettingsRead(d, meta)
}

func resourceAwsBackupRegionSettingsRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).backupconn

resp, err := conn.DescribeRegionSettings(&backup.DescribeRegionSettingsInput{})
if err != nil {
return fmt.Errorf("error reading Backup Region Settings (%s): %w", d.Id(), err)
}

d.Set("resource_type_opt_in_preference", aws.BoolValueMap(resp.ResourceTypeOptInPreference))

return nil
}
128 changes: 128 additions & 0 deletions aws/resource_aws_backup_region_settings_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package aws

import (
"testing"

"github.com/aws/aws-sdk-go/service/backup"
"github.com/aws/aws-sdk-go/service/fsx"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
DrFaust92 marked this conversation as resolved.
Show resolved Hide resolved
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func TestAccAwsBackupRegionSettings_basic(t *testing.T) {
var settings backup.DescribeRegionSettingsOutput
Copy link
Member

Choose a reason for hiding this comment

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

This test is failing in GovCloud as FSx is not available there, can we use describeRegionSettings to populate the list to check? cc: @YakDriver

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The api allows for partial update (but describe actually return all services) so I can add computed to the map to reduce service keys in tests to something simple like RDS. I just wanted it to prevent any state drift issues

Copy link
Member

Choose a reason for hiding this comment

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

See below


rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_backup_region_settings.test"
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPartitionHasServicePreCheck(fsx.EndpointsID, t)
testAccPreCheckAWSBackup(t)
},
Providers: testAccProviders,
CheckDestroy: nil,
Steps: []resource.TestStep{
{
Config: testAccBackupRegionSettingsConfig1(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsBackupRegionSettingsExists(&settings),
resource.TestCheckResourceAttr(resourceName, "resource_type_opt_in_preference.%", "8"),
resource.TestCheckResourceAttr(resourceName, "resource_type_opt_in_preference.DynamoDB", "true"),
resource.TestCheckResourceAttr(resourceName, "resource_type_opt_in_preference.Aurora", "true"),
resource.TestCheckResourceAttr(resourceName, "resource_type_opt_in_preference.EBS", "true"),
resource.TestCheckResourceAttr(resourceName, "resource_type_opt_in_preference.EC2", "true"),
resource.TestCheckResourceAttr(resourceName, "resource_type_opt_in_preference.EFS", "true"),
resource.TestCheckResourceAttr(resourceName, "resource_type_opt_in_preference.FSx", "true"),
resource.TestCheckResourceAttr(resourceName, "resource_type_opt_in_preference.RDS", "true"),
resource.TestCheckResourceAttr(resourceName, "resource_type_opt_in_preference.Storage Gateway", "true"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccBackupRegionSettingsConfig2(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsBackupRegionSettingsExists(&settings),
resource.TestCheckResourceAttr(resourceName, "resource_type_opt_in_preference.%", "8"),
resource.TestCheckResourceAttr(resourceName, "resource_type_opt_in_preference.DynamoDB", "true"),
resource.TestCheckResourceAttr(resourceName, "resource_type_opt_in_preference.Aurora", "false"),
resource.TestCheckResourceAttr(resourceName, "resource_type_opt_in_preference.EBS", "true"),
resource.TestCheckResourceAttr(resourceName, "resource_type_opt_in_preference.EC2", "true"),
resource.TestCheckResourceAttr(resourceName, "resource_type_opt_in_preference.EFS", "true"),
resource.TestCheckResourceAttr(resourceName, "resource_type_opt_in_preference.FSx", "true"),
resource.TestCheckResourceAttr(resourceName, "resource_type_opt_in_preference.RDS", "true"),
resource.TestCheckResourceAttr(resourceName, "resource_type_opt_in_preference.Storage Gateway", "true"),
),
},
{
Config: testAccBackupRegionSettingsConfig1(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsBackupRegionSettingsExists(&settings),
resource.TestCheckResourceAttr(resourceName, "resource_type_opt_in_preference.%", "8"),
resource.TestCheckResourceAttr(resourceName, "resource_type_opt_in_preference.DynamoDB", "true"),
resource.TestCheckResourceAttr(resourceName, "resource_type_opt_in_preference.Aurora", "true"),
resource.TestCheckResourceAttr(resourceName, "resource_type_opt_in_preference.EBS", "true"),
resource.TestCheckResourceAttr(resourceName, "resource_type_opt_in_preference.EC2", "true"),
resource.TestCheckResourceAttr(resourceName, "resource_type_opt_in_preference.EFS", "true"),
resource.TestCheckResourceAttr(resourceName, "resource_type_opt_in_preference.FSx", "true"),
resource.TestCheckResourceAttr(resourceName, "resource_type_opt_in_preference.RDS", "true"),
resource.TestCheckResourceAttr(resourceName, "resource_type_opt_in_preference.Storage Gateway", "true"),
),
},
},
})
}

func testAccCheckAwsBackupRegionSettingsExists(settings *backup.DescribeRegionSettingsOutput) resource.TestCheckFunc {
return func(s *terraform.State) error {

conn := testAccProvider.Meta().(*AWSClient).backupconn
resp, err := conn.DescribeRegionSettings(&backup.DescribeRegionSettingsInput{})
if err != nil {
return err
}

*settings = *resp

return nil
}
}

func testAccBackupRegionSettingsConfig1(rName string) string {
return `
resource "aws_backup_region_settings" "test" {
resource_type_opt_in_preference = {
"DynamoDB" = true
"Aurora" = true
"EBS" = true
"EC2" = true
"EFS" = true
"FSx" = true
"RDS" = true
"Storage Gateway" = true
}
}
`
}

func testAccBackupRegionSettingsConfig2(rName string) string {
return `
resource "aws_backup_region_settings" "test" {
resource_type_opt_in_preference = {
"DynamoDB" = true
"Aurora" = false
"EBS" = true
"EC2" = true
"EFS" = true
"FSx" = true
"RDS" = true
"Storage Gateway" = true
}
}
`
}
48 changes: 48 additions & 0 deletions website/docs/r/backup_region_settings.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
subcategory: "Backup"
layout: "aws"
page_title: "AWS: aws_backup_region_settings"
description: |-
Provides an AWS Backup Region Settings resource.
---

# Resource: aws_backup_region_settings

Provides an AWS Backup Region Settings resource.

## Example Usage

```hcl
resource "aws_backup_region_settings" "test" {
resource_type_opt_in_preference = {
"DynamoDB" = true
"Aurora" = true
"EBS" = true
"EC2" = true
"EFS" = true
"FSx" = true
"RDS" = true
"Storage Gateway" = true
}
}
```

## Argument Reference

The following arguments are supported:

* `resource_type_opt_in_preference` - (Required) A map of services along with the opt-in preferences for the Region.

## Attributes Reference

In addition to all arguments above, the following attributes are exported:

* `id` - The AWS region.

## Import

Backup Region Settings can be imported using the `region`, e.g.

```
$ terraform import aws_backup_region_settings.test us-west-2
```