diff --git a/aws/provider.go b/aws/provider.go index 316ed6f74fd..3a2aa20c745 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -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(), diff --git a/aws/resource_aws_backup_global_settings.go b/aws/resource_aws_backup_global_settings.go new file mode 100644 index 00000000000..58ffd4e7fbb --- /dev/null +++ b/aws/resource_aws_backup_global_settings.go @@ -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 +} diff --git a/aws/resource_aws_backup_global_settings_test.go b/aws/resource_aws_backup_global_settings_test.go new file mode 100644 index 00000000000..bca27e84086 --- /dev/null +++ b/aws/resource_aws_backup_global_settings_test.go @@ -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) +} diff --git a/website/docs/r/backup_global_settings.html.markdown b/website/docs/r/backup_global_settings.html.markdown new file mode 100644 index 00000000000..aaac9911e7b --- /dev/null +++ b/website/docs/r/backup_global_settings.html.markdown @@ -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 +```