Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

r/aws_backup_region_settings: Add ResourceTypeManagementPreference #22021

Merged
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
3 changes: 3 additions & 0 deletions .changelog/22021.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_backup_region_settings: Add `resource_type_management_preference` argument
```
9 changes: 9 additions & 0 deletions internal/flex/flex.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ func ExpandStringMap(m map[string]interface{}) map[string]*string {
return stringMap
}

// Expands a map of string to interface to a map of string to *bool
func ExpandBoolMap(m map[string]interface{}) map[string]*bool {
boolMap := make(map[string]*bool, len(m))
for k, v := range m {
boolMap[k] = aws.Bool(v.(bool))
}
return boolMap
}

// Takes the result of schema.Set of strings and returns a []*string
func ExpandStringSet(configured *schema.Set) []*string {
return ExpandStringList(configured.List()) // nosemgrep: helper-schema-Set-extraneous-ExpandStringList-with-List
Expand Down
22 changes: 16 additions & 6 deletions internal/service/backup/region_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/aws/aws-sdk-go/service/backup"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/flex"
)

func ResourceRegionSettings() *schema.Resource {
Expand All @@ -20,6 +21,12 @@ func ResourceRegionSettings() *schema.Resource {
},

Schema: map[string]*schema.Schema{
"resource_type_management_preference": {
Type: schema.TypeMap,
Optional: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeBool},
},
"resource_type_opt_in_preference": {
Type: schema.TypeMap,
Required: true,
Expand All @@ -32,17 +39,18 @@ func ResourceRegionSettings() *schema.Resource {
func resourceRegionSettingsUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.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{}

if v, ok := d.GetOk("resource_type_management_preference"); ok && len(v.(map[string]interface{})) > 0 {
input.ResourceTypeManagementPreference = flex.ExpandBoolMap(v.(map[string]interface{}))
}

input := &backup.UpdateRegionSettingsInput{
ResourceTypeOptInPreference: list,
if v, ok := d.GetOk("resource_type_opt_in_preference"); ok && len(v.(map[string]interface{})) > 0 {
input.ResourceTypeOptInPreference = flex.ExpandBoolMap(v.(map[string]interface{}))
}

_, err := conn.UpdateRegionSettings(input)

if err != nil {
return fmt.Errorf("error setting Backup Region Settings (%s): %w", d.Id(), err)
}
Expand All @@ -56,11 +64,13 @@ func resourceRegionSettingsRead(d *schema.ResourceData, meta interface{}) error
conn := meta.(*conns.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))
d.Set("resource_type_management_preference", aws.BoolValueMap(resp.ResourceTypeManagementPreference))

return nil
}
82 changes: 67 additions & 15 deletions internal/service/backup/region_settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/aws/aws-sdk-go/service/backup"
"github.com/aws/aws-sdk-go/service/fsx"
sdkacctest "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/hashicorp/terraform-provider-aws/internal/acctest"
Expand All @@ -14,9 +13,8 @@ import (

func TestAccBackupRegionSettings_basic(t *testing.T) {
var settings backup.DescribeRegionSettingsOutput

rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_backup_region_settings.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acctest.PreCheck(t)
Expand All @@ -28,18 +26,24 @@ func TestAccBackupRegionSettings_basic(t *testing.T) {
CheckDestroy: nil,
Steps: []resource.TestStep{
{
Config: testAccBackupRegionSettingsConfig1(rName),
Config: testAccBackupRegionSettingsConfig1(),
Check: resource.ComposeTestCheckFunc(
testAccCheckRegionSettingsExists(&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.%", "11"),
resource.TestCheckResourceAttr(resourceName, "resource_type_opt_in_preference.Aurora", "true"),
resource.TestCheckResourceAttr(resourceName, "resource_type_opt_in_preference.DocumentDB", "true"),
resource.TestCheckResourceAttr(resourceName, "resource_type_opt_in_preference.DynamoDB", "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.Neptune", "true"),
resource.TestCheckResourceAttr(resourceName, "resource_type_opt_in_preference.RDS", "true"),
resource.TestCheckResourceAttr(resourceName, "resource_type_opt_in_preference.Storage Gateway", "true"),
resource.TestCheckResourceAttr(resourceName, "resource_type_opt_in_preference.VirtualMachine", "true"),
resource.TestCheckResourceAttr(resourceName, "resource_type_management_preference.%", "2"),
resource.TestCheckResourceAttrSet(resourceName, "resource_type_management_preference.DynamoDB"),
resource.TestCheckResourceAttrSet(resourceName, "resource_type_management_preference.EFS"),
),
},
{
Expand All @@ -48,33 +52,45 @@ func TestAccBackupRegionSettings_basic(t *testing.T) {
ImportStateVerify: true,
},
{
Config: testAccBackupRegionSettingsConfig2(rName),
Config: testAccBackupRegionSettingsConfig2(),
Check: resource.ComposeTestCheckFunc(
testAccCheckRegionSettingsExists(&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.%", "11"),
resource.TestCheckResourceAttr(resourceName, "resource_type_opt_in_preference.Aurora", "false"),
resource.TestCheckResourceAttr(resourceName, "resource_type_opt_in_preference.DocumentDB", "true"),
resource.TestCheckResourceAttr(resourceName, "resource_type_opt_in_preference.DynamoDB", "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.Neptune", "true"),
resource.TestCheckResourceAttr(resourceName, "resource_type_opt_in_preference.RDS", "true"),
resource.TestCheckResourceAttr(resourceName, "resource_type_opt_in_preference.Storage Gateway", "true"),
resource.TestCheckResourceAttr(resourceName, "resource_type_opt_in_preference.VirtualMachine", "true"),
resource.TestCheckResourceAttr(resourceName, "resource_type_management_preference.%", "2"),
resource.TestCheckResourceAttr(resourceName, "resource_type_management_preference.DynamoDB", "true"),
resource.TestCheckResourceAttr(resourceName, "resource_type_management_preference.EFS", "true"),
),
},
{
Config: testAccBackupRegionSettingsConfig1(rName),
Config: testAccBackupRegionSettingsConfig3(),
Check: resource.ComposeTestCheckFunc(
testAccCheckRegionSettingsExists(&settings),
resource.TestCheckResourceAttr(resourceName, "resource_type_opt_in_preference.%", "8"),
resource.TestCheckResourceAttr(resourceName, "resource_type_opt_in_preference.%", "11"),
resource.TestCheckResourceAttr(resourceName, "resource_type_opt_in_preference.Aurora", "false"),
resource.TestCheckResourceAttr(resourceName, "resource_type_opt_in_preference.DocumentDB", "true"),
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.Neptune", "true"),
resource.TestCheckResourceAttr(resourceName, "resource_type_opt_in_preference.RDS", "true"),
resource.TestCheckResourceAttr(resourceName, "resource_type_opt_in_preference.Storage Gateway", "true"),
resource.TestCheckResourceAttr(resourceName, "resource_type_opt_in_preference.VirtualMachine", "false"),
resource.TestCheckResourceAttr(resourceName, "resource_type_management_preference.%", "2"),
resource.TestCheckResourceAttr(resourceName, "resource_type_management_preference.DynamoDB", "false"),
resource.TestCheckResourceAttr(resourceName, "resource_type_management_preference.EFS", "true"),
),
},
},
Expand All @@ -96,35 +112,71 @@ func testAccCheckRegionSettingsExists(settings *backup.DescribeRegionSettingsOut
}
}

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

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

resource_type_management_preference = {
"DynamoDB" = true
"EFS" = true
}
}
`
}

func testAccBackupRegionSettingsConfig3() string {
return `
resource "aws_backup_region_settings" "test" {
resource_type_opt_in_preference = {
"Aurora" = false
"DocumentDB" = true
"DynamoDB" = true
"EBS" = true
"EC2" = true
"EFS" = true
"FSx" = true
"Neptune" = true
"RDS" = true
"Storage Gateway" = true
"VirtualMachine" = false
}

resource_type_management_preference = {
"DynamoDB" = false
"EFS" = true
}
}
`
Expand Down
11 changes: 10 additions & 1 deletion website/docs/r/backup_region_settings.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,22 @@ Provides an AWS Backup Region Settings resource.
```terraform
resource "aws_backup_region_settings" "test" {
resource_type_opt_in_preference = {
"DynamoDB" = true
"Aurora" = true
"DocumentDB" = true
"DynamoDB" = true
"EBS" = true
"EC2" = true
"EFS" = true
"FSx" = true
"Neptune" = true
"RDS" = true
"Storage Gateway" = true
"VirtualMachine" = true
}

resource_type_management_preference = {
"DynamoDB" = true
"EFS" = true
}
}
```
Expand All @@ -32,6 +40,7 @@ resource "aws_backup_region_settings" "test" {
The following arguments are supported:

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

## Attributes Reference

Expand Down