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_vault: Adding resource to manage AWS Backup vaults #7207

Merged
merged 14 commits into from
Feb 6, 2019
Merged
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ func Provider() terraform.ResourceProvider {
"aws_autoscaling_notification": resourceAwsAutoscalingNotification(),
"aws_autoscaling_policy": resourceAwsAutoscalingPolicy(),
"aws_autoscaling_schedule": resourceAwsAutoscalingSchedule(),
"aws_backup_vault": resourceAwsBackupVault(),
"aws_budgets_budget": resourceAwsBudgetsBudget(),
"aws_cloud9_environment_ec2": resourceAwsCloud9EnvironmentEc2(),
"aws_cloudformation_stack": resourceAwsCloudFormationStack(),
Expand Down
110 changes: 110 additions & 0 deletions aws/resource_aws_backup_vault.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package aws

import (
"regexp"

"github.com/hashicorp/terraform/helper/validation"

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

func resourceAwsBackupVault() *schema.Resource {
return &schema.Resource{
Create: resourceAwsBackupVaultCreate,
Read: resourceAwsBackupVaultRead,
Update: resourceAwsBackupVaultUpdate,
slapula marked this conversation as resolved.
Show resolved Hide resolved
Delete: resourceAwsBackupVaultDelete,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringMatch(regexp.MustCompile("[a-z0-9\\-]+"), "must contain alphanumeric characters or underscores"),
},
"tags": {
slapula marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"kms_key_arn": {
bflad marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateArn,
},
"arn": {
Type: schema.TypeString,
Computed: true,
},
"recovery_points": {
Type: schema.TypeInt,
Computed: true,
},
},
}
}

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

input := &backup.CreateBackupVaultInput{
BackupVaultName: aws.String(d.Get("name").(string)),
}

if v, ok := d.GetOk("tags"); ok {
input.BackupVaultTags = v.(map[string]*string)
}

if v, ok := d.GetOk("kms_key_arn"); ok {
input.EncryptionKeyArn = aws.String(v.(string))
}

_, err := conn.CreateBackupVault(input)
if err != nil {
return err
slapula marked this conversation as resolved.
Show resolved Hide resolved
}

d.SetId(d.Get("name").(string))

return resourceAwsBackupVaultRead(d, meta)
}

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

input := &backup.DescribeBackupVaultInput{
BackupVaultName: aws.String(d.Id()),
}

resp, err := conn.DescribeBackupVault(input)
if err != nil {
return err
slapula marked this conversation as resolved.
Show resolved Hide resolved
}

d.Set("arn", resp.BackupVaultArn)
d.Set("recovery_points", resp.NumberOfRecoveryPoints)

return nil
}

func resourceAwsBackupVaultUpdate(d *schema.ResourceData, meta interface{}) error {
return resourceAwsBackupVaultRead(d, meta)
}

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

input := &backup.DeleteBackupVaultInput{
BackupVaultName: aws.String(d.Get("name").(string)),
}

_, err := conn.DeleteBackupVault(input)
if err != nil {
return err
slapula marked this conversation as resolved.
Show resolved Hide resolved
}

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

import (
"fmt"
"testing"

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

func TestAccAwsBackupVault_basic(t *testing.T) {
rInt := acctest.RandInt()
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAwsBackupVaultDestroy,
slapula marked this conversation as resolved.
Show resolved Hide resolved
Steps: []resource.TestStep{
{
Config: testAccBackupVaultConfig(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsBackupVaultExists("aws_backup_vault.test"),
),
},
},
})
}

func TestAccAwsBackupVault_withKmsKey(t *testing.T) {
rInt := acctest.RandInt()
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAwsBackupVaultDestroy,
Steps: []resource.TestStep{
{
Config: testAccBackupVaultWithKmsKey(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsBackupVaultExists("aws_backup_vault.test"),
),
},
},
})
}

func testAccCheckAwsBackupVaultDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).backupconn
for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_backup_vault" {
continue
}

input := &backup.DescribeBackupVaultInput{
BackupVaultName: aws.String(rs.Primary.ID),
}

resp, err := conn.DescribeBackupVault(input)
if err != nil {
return err
}

if err == nil {
if *resp.BackupVaultName == rs.Primary.ID {
return fmt.Errorf("Vault '%s' was not deleted properly", rs.Primary.ID)
}
}
}

return nil
}

func testAccCheckAwsBackupVaultExists(name string) resource.TestCheckFunc {
slapula marked this conversation as resolved.
Show resolved Hide resolved
return func(s *terraform.State) error {
_, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("not found: %s, %v", name, s.RootModule().Resources)
}
return nil
}
}

func testAccBackupVaultConfig(randInt int) string {
return fmt.Sprintf(`
resource "aws_backup_vault" "test" {
name = "tf_acc_test_backup_vault_%d"
}
`, randInt)
}

func testAccBackupVaultWithKmsKey(randInt int) string {
return fmt.Sprintf(`
resource "aws_kms_key" "test" {
description = "Test KMS Key for AWS Backup Vault"
deletion_window_in_days = 10
}

resource "aws_backup_vault" "test" {
name = "tf_acc_test_backup_vault_%d"
kms_key_arm = "${aws_backup_vault.test.arn}"
}
`, randInt)
}
9 changes: 9 additions & 0 deletions website/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,15 @@
</ul>
</li>

<li<%= sidebar_current("docs-aws-resource-backup") %>>
<a href="#">Backup Resources</a>
<ul class="nav nav-visible">
<li<%= sidebar_current("docs-aws-resource-backup-vault") %>>
<a href="/docs/providers/aws/r/backup_vault.html">aws_backup_vault</a>
</li>
</ul>
</li>

<li<%= sidebar_current("docs-aws-resource-batch") %>>
<a href="#">Batch Resources</a>
<ul class="nav nav-visible">
Expand Down
36 changes: 36 additions & 0 deletions website/docs/r/backup_vault.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
layout: "aws"
page_title: "AWS: aws_backup_vault"
sidebar_current: "docs-aws-resource-backup-vault"
description: |-
Provides an AWS Backup vault resource.
---

# aws_backup_vault

Provides an AWS Backup vault resource.

## Example Usage

```hcl
resource "aws_backup_vault" "example" {
name = "example_backup_vault"
kms_key_arm = "${aws_backup_vault.example.arn}"
slapula marked this conversation as resolved.
Show resolved Hide resolved
}
```

## Argument Reference

The following arguments are supported:

* `name` - (Required) Name of the backup vault to create.
* `tags` - (Optional) Metadata that you can assign to help organize the resources that you create.
* `kms_key_arn` - (Optional) The server-side encryption key that is used to protect your backups.

## Attributes Reference

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

* `id` - The name of the vault.
* `arn` - The ARN of the vault.
* `recovery_points` - The number of recovery points that are stored in a backup vault.