Skip to content

Commit

Permalink
Merge pull request #16475 from DrFaust92/r/backup_global_settings
Browse files Browse the repository at this point in the history
r/backup_global_settings - add new resource
  • Loading branch information
breathingdust authored Jan 18, 2021
2 parents de1654b + 4e39c14 commit 1889f69
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 0 deletions.
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@ func Provider() *schema.Provider {
"aws_autoscaling_policy": resourceAwsAutoscalingPolicy(),
"aws_autoscaling_schedule": resourceAwsAutoscalingSchedule(),
"aws_autoscalingplans_scaling_plan": resourceAwsAutoScalingPlansScalingPlan(),
"aws_backup_global_settings": resourceAwsBackupGlobalSettings(),
"aws_backup_plan": resourceAwsBackupPlan(),
"aws_backup_region_settings": resourceAwsBackupRegionSettings(),
"aws_backup_selection": resourceAwsBackupSelection(),
Expand Down
61 changes: 61 additions & 0 deletions aws/resource_aws_backup_global_settings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
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 resourceAwsBackupGlobalSettings() *schema.Resource {
return &schema.Resource{
Create: resourceAwsBackupGlobalSettingsUpdate,
Update: resourceAwsBackupGlobalSettingsUpdate,
Read: resourceAwsBackupGlobalSettingsRead,
Delete: schema.Noop,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

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

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

input := &backup.UpdateGlobalSettingsInput{
GlobalSettings: stringMapToPointers(d.Get("global_settings").(map[string]interface{})),
}

_, err := conn.UpdateGlobalSettings(input)
if err != nil {
return fmt.Errorf("error setting Backup Global Settings (%s): %w", meta.(*AWSClient).accountid, err)
}

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

return resourceAwsBackupGlobalSettingsRead(d, meta)
}

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

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

if err := d.Set("global_settings", aws.StringValueMap(resp.GlobalSettings)); err != nil {
return fmt.Errorf("error setting global_settings: %w", err)
}

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

import (
"fmt"
"testing"

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

func TestAccAwsBackupGlobalSettings_basic(t *testing.T) {
var settings backup.DescribeGlobalSettingsOutput

resourceName := "aws_backup_global_settings.test"
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckAWSBackup(t)
},
Providers: testAccProviders,
CheckDestroy: nil,
Steps: []resource.TestStep{
{
Config: testAccBackupGlobalSettingsConfig("true"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsBackupGlobalSettingsExists(&settings),
resource.TestCheckResourceAttr(resourceName, "global_settings.%", "1"),
resource.TestCheckResourceAttr(resourceName, "global_settings.isCrossAccountBackupEnabled", "true"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccBackupGlobalSettingsConfig("false"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsBackupGlobalSettingsExists(&settings),
resource.TestCheckResourceAttr(resourceName, "global_settings.%", "1"),
resource.TestCheckResourceAttr(resourceName, "global_settings.isCrossAccountBackupEnabled", "false"),
),
},
{
Config: testAccBackupGlobalSettingsConfig("true"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsBackupGlobalSettingsExists(&settings),
resource.TestCheckResourceAttr(resourceName, "global_settings.%", "1"),
resource.TestCheckResourceAttr(resourceName, "global_settings.isCrossAccountBackupEnabled", "true"),
),
},
},
})
}

func testAccCheckAwsBackupGlobalSettingsExists(settings *backup.DescribeGlobalSettingsOutput) resource.TestCheckFunc {
return func(s *terraform.State) error {

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

*settings = *resp

return nil
}
}

func testAccBackupGlobalSettingsConfig(setting string) string {
return fmt.Sprintf(`
resource "aws_backup_global_settings" "test" {
global_settings = {
"isCrossAccountBackupEnabled" = %[1]q
}
}
`, setting)
}
41 changes: 41 additions & 0 deletions website/docs/r/backup_global_settings.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
subcategory: "Backup"
layout: "aws"
page_title: "AWS: aws_backup_global_settings"
description: |-
Provides an AWS Backup Global Settings resource.
---

# Resource: aws_backup_global_settings

Provides an AWS Backup Global Settings resource.

## Example Usage

```hcl
resource "aws_backup_global_settings" "test" {
global_settings = {
"isCrossAccountBackupEnabled" = "true"
}
}
```

## Argument Reference

The following arguments are supported:

* `global_settings` - (Required) A list of resources along with the opt-in preferences for the account.

## Attributes Reference

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

* `id` - The AWS Account ID.

## Import

Backup Global Settings can be imported using the `id`, e.g.

```
$ terraform import aws_backup_global_settings.example 123456789012
```

0 comments on commit 1889f69

Please sign in to comment.