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

add backup_* data sources #13035

Merged
merged 4 commits into from
Apr 28, 2020
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
62 changes: 62 additions & 0 deletions aws/data_source_aws_backup_plan.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
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/helper/schema"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)

func dataSourceAwsBackupPlan() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsBackupPlanRead,

Schema: map[string]*schema.Schema{
"plan_id": {
Type: schema.TypeString,
Required: true,
},
"arn": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"tags": tagsSchemaComputed(),
"version": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceAwsBackupPlanRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).backupconn
id := d.Get("plan_id").(string)

resp, err := conn.GetBackupPlan(&backup.GetBackupPlanInput{
BackupPlanId: aws.String(id),
})
if err != nil {
return fmt.Errorf("Error getting Backup Plan: %v", err)
}

d.SetId(aws.StringValue(resp.BackupPlanId))
d.Set("arn", resp.BackupPlanArn)
d.Set("name", resp.BackupPlan.BackupPlanName)
d.Set("version", resp.VersionId)

tags, err := keyvaluetags.BackupListTags(conn, aws.StringValue(resp.BackupPlanArn))
if err != nil {
return fmt.Errorf("error listing tags for Backup Plan (%s): %s", id, err)
}
if err := d.Set("tags", tags.IgnoreAws().Map()); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}

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

import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccAWSBackupPlanDataSource_basic(t *testing.T) {
datasourceName := "data.aws_backup_plan.test"
resourceName := "aws_backup_plan.test"
rInt := acctest.RandInt()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccAwsBackupPlanDataSourceConfig_nonExistent,
ExpectError: regexp.MustCompile(`Error getting Backup Plan`),
},
{
Config: testAccAwsBackupPlanDataSourceConfig_basic(rInt),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(datasourceName, "name", resourceName, "name"),
resource.TestCheckResourceAttrPair(datasourceName, "arn", resourceName, "arn"),
resource.TestCheckResourceAttrPair(datasourceName, "version", resourceName, "version"),
resource.TestCheckResourceAttrPair(datasourceName, "tags.%", resourceName, "tags.%"),
),
},
},
})
}

const testAccAwsBackupPlanDataSourceConfig_nonExistent = `
data "aws_backup_plan" "test" {
plan_id = "tf-acc-test-does-not-exist"
}`

func testAccAwsBackupPlanDataSourceConfig_basic(rInt int) string {
return fmt.Sprintf(`
resource "aws_backup_vault" "test" {
name = "tf_acc_test_backup_vault_%[1]d"
}

resource "aws_backup_plan" "test" {
name = "tf_acc_test_backup_plan_%[1]d"

rule {
rule_name = "tf_acc_test_backup_rule_%[1]d"
target_vault_name = "${aws_backup_vault.test.name}"
schedule = "cron(0 12 * * ? *)"
}

tags = {
Name = "Value%[1]d"
Key2 = "Value2b"
Key3 = "Value3"
}
}

data "aws_backup_plan" "test" {
plan_id = aws_backup_plan.test.id
}
`, rInt)
}
64 changes: 64 additions & 0 deletions aws/data_source_aws_backup_selection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
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/helper/schema"
)

func dataSourceAwsBackupSelection() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsBackupSelectionRead,

Schema: map[string]*schema.Schema{
"plan_id": {
Type: schema.TypeString,
Required: true,
},
"selection_id": {
Type: schema.TypeString,
Required: true,
},
"iam_role_arn": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"resources": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}

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

input := &backup.GetBackupSelectionInput{
BackupPlanId: aws.String(d.Get("plan_id").(string)),
SelectionId: aws.String(d.Get("selection_id").(string)),
}

resp, err := conn.GetBackupSelection(input)
if err != nil {
return fmt.Errorf("Error getting Backup Selection: %s", err)
}

d.SetId(aws.StringValue(resp.SelectionId))
d.Set("iam_role_arn", resp.BackupSelection.IamRoleArn)
d.Set("name", resp.BackupSelection.SelectionName)

if resp.BackupSelection.Resources != nil {
if err := d.Set("resources", aws.StringValueSlice(resp.BackupSelection.Resources)); err != nil {
return fmt.Errorf("error setting resources: %s", err)
}
}

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

import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccAWSBackupSelectionDataSource_basic(t *testing.T) {
datasourceName := "data.aws_backup_selection.test"
resourceName := "aws_backup_selection.test"
rInt := acctest.RandInt()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccAwsBackupSelectionDataSourceConfig_nonExistent,
ExpectError: regexp.MustCompile(`Error getting Backup Selection`),
},
{
Config: testAccAwsBackupSelectionDataSourceConfig_basic(rInt),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(datasourceName, "name", resourceName, "name"),
resource.TestCheckResourceAttrPair(datasourceName, "iam_role_arn", resourceName, "iam_role_arn"),
resource.TestCheckResourceAttrPair(datasourceName, "resources.#", resourceName, "resources.#"),
),
},
},
})
}

const testAccAwsBackupSelectionDataSourceConfig_nonExistent = `
data "aws_backup_selection" "test" {
plan_id = "tf-acc-test-does-not-exist"
selection_id = "tf-acc-test-dne"
}`

func testAccAwsBackupSelectionDataSourceConfig_basic(rInt int) string {
return fmt.Sprintf(`
data "aws_caller_identity" "current" {}

data "aws_partition" "current" {}

data "aws_region" "current" {}

resource "aws_backup_vault" "test" {
name = "tf_acc_test_backup_vault_%[1]d"
}

resource "aws_backup_plan" "test" {
name = "tf_acc_test_backup_plan_%[1]d"

rule {
rule_name = "tf_acc_test_backup_rule_%[1]d"
target_vault_name = "${aws_backup_vault.test.name}"
schedule = "cron(0 12 * * ? *)"
}
}

resource "aws_backup_selection" "test" {
plan_id = "${aws_backup_plan.test.id}"
name = "tf_acc_test_backup_selection_%[1]d"
iam_role_arn = "arn:${data.aws_partition.current.partition}:iam::${data.aws_caller_identity.current.account_id}:role/service-role/AWSBackupDefaultServiceRole"

selection_tag {
type = "STRINGEQUALS"
key = "foo"
value = "bar"
}

resources = [
"arn:${data.aws_partition.current.partition}:ec2:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:volume/"
]
}

data "aws_backup_selection" "test" {
plan_id = aws_backup_plan.test.id
selection_id = aws_backup_selection.test.id
}
`, rInt)
}
64 changes: 64 additions & 0 deletions aws/data_source_aws_backup_vault.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
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/helper/schema"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)

func dataSourceAwsBackupVault() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsBackupVaultRead,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"arn": {
Type: schema.TypeString,
Computed: true,
},
"kms_key_arn": {
Type: schema.TypeString,
Computed: true,
},
"recovery_points": {
Type: schema.TypeInt,
Computed: true,
},
"tags": tagsSchemaComputed(),
},
}
}

func dataSourceAwsBackupVaultRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).backupconn
name := d.Get("name").(string)
input := &backup.DescribeBackupVaultInput{
BackupVaultName: aws.String(name),
}

resp, err := conn.DescribeBackupVault(input)
if err != nil {
return fmt.Errorf("Error getting Backup Vault: %v", err)
}

d.SetId(aws.StringValue(resp.BackupVaultName))
d.Set("arn", resp.BackupVaultArn)
d.Set("kms_key_arn", resp.EncryptionKeyArn)
d.Set("name", resp.BackupVaultName)
d.Set("recovery_points", resp.NumberOfRecoveryPoints)

tags, err := keyvaluetags.BackupListTags(conn, aws.StringValue(resp.BackupVaultArn))
if err != nil {
return fmt.Errorf("error listing tags for Backup Vault (%s): %s", name, err)
}
if err := d.Set("tags", tags.IgnoreAws().Map()); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}

return nil
}
Loading