-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16475 from DrFaust92/r/backup_global_settings
r/backup_global_settings - add new resource
- Loading branch information
Showing
4 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |