Skip to content

Commit

Permalink
more changes based on feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
slapula committed Apr 4, 2019
1 parent 5132ea5 commit c80dd6f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 35 deletions.
34 changes: 6 additions & 28 deletions aws/resource_aws_backup_selection.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package aws

import (
"bytes"
"fmt"
"log"
"regexp"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/backup"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)
Expand Down Expand Up @@ -40,7 +38,7 @@ func resourceAwsBackupSelection() *schema.Resource {
ForceNew: true,
ValidateFunc: validateArn,
},
"tag": {
"selection_tag": {
Type: schema.TypeSet,
Optional: true,
ForceNew: true,
Expand All @@ -63,7 +61,6 @@ func resourceAwsBackupSelection() *schema.Resource {
},
},
},
Set: resourceAwsConditionTagHash,
},
"resources": {
Type: schema.TypeList,
Expand All @@ -80,7 +77,7 @@ func resourceAwsBackupSelectionCreate(d *schema.ResourceData, meta interface{})

selection := &backup.Selection{
IamRoleArn: aws.String(d.Get("iam_role_arn").(string)),
ListOfTags: expandBackupConditionTags(d.Get("tag").(*schema.Set).List()),
ListOfTags: expandBackupConditionTags(d.Get("selection_tag").(*schema.Set).List()),
Resources: expandStringList(d.Get("resources").([]interface{})),
SelectionName: aws.String(d.Get("name").(string)),
}
Expand Down Expand Up @@ -124,7 +121,7 @@ func resourceAwsBackupSelectionRead(d *schema.ResourceData, meta interface{}) er
d.Set("iam_role_arn", resp.BackupSelection.IamRoleArn)

if resp.BackupSelection.ListOfTags != nil {
tag := &schema.Set{F: resourceAwsConditionTagHash}
tags := make([]map[string]interface{}, 0)

for _, r := range resp.BackupSelection.ListOfTags {
m := make(map[string]interface{})
Expand All @@ -133,11 +130,11 @@ func resourceAwsBackupSelectionRead(d *schema.ResourceData, meta interface{}) er
m["key"] = aws.StringValue(r.ConditionKey)
m["value"] = aws.StringValue(r.ConditionValue)

tag.Add(m)
tags = append(tags, m)
}

if err := d.Set("tag", tag); err != nil {
return fmt.Errorf("error setting tag: %s", err)
if err := d.Set("selection_tag", tags); err != nil {
return fmt.Errorf("error setting selection tag: %s", err)
}
}
if resp.BackupSelection.Resources != nil {
Expand Down Expand Up @@ -181,22 +178,3 @@ func expandBackupConditionTags(tagList []interface{}) []*backup.Condition {

return conditions
}

func resourceAwsConditionTagHash(v interface{}) int {
var buf bytes.Buffer
m := v.(map[string]interface{})

if v, ok := m["type"]; ok {
buf.WriteString(fmt.Sprintf("%s-", v.(string)))
}

if v, ok := m["key"]; ok {
buf.WriteString(fmt.Sprintf("%s-", v.(string)))
}

if v, ok := m["value"]; ok {
buf.WriteString(fmt.Sprintf("%s-", v.(string)))
}

return hashcode.String(buf.String())
}
10 changes: 5 additions & 5 deletions aws/resource_aws_backup_selection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestAccAwsBackupSelection_withTags(t *testing.T) {
Config: testAccBackupSelectionConfigWithTags(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsBackupSelectionExists("aws_backup_selection.test", &selection1),
resource.TestCheckResourceAttr("aws_backup_selection.test", "tag.#", "2"),
resource.TestCheckResourceAttr("aws_backup_selection.test", "selection_tag.#", "2"),
),
},
},
Expand Down Expand Up @@ -180,7 +180,7 @@ resource "aws_backup_selection" "test" {
name = "tf_acc_test_backup_selection_%d"
iam_role_arn = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/service-role/AWSBackupDefaultServiceRole"
tag {
selection_tag {
type = "STRINGEQUALS"
key = "foo"
value = "bar"
Expand All @@ -201,13 +201,13 @@ resource "aws_backup_selection" "test" {
name = "tf_acc_test_backup_selection_%d"
iam_role_arn = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/service-role/AWSBackupDefaultServiceRole"
tag {
selection_tag {
type = "STRINGEQUALS"
key = "foo"
value = "bar"
}
tag {
selection_tag {
type = "STRINGEQUALS"
key = "boo"
value = "far"
Expand All @@ -228,7 +228,7 @@ resource "aws_backup_selection" "test" {
name = "tf_acc_test_backup_selection_%d"
iam_role_arn = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/service-role/AWSBackupDefaultServiceRole"
tag {
selection_tag {
type = "STRINGEQUALS"
key = "foo"
value = "bar"
Expand Down
4 changes: 2 additions & 2 deletions website/docs/r/backup_selection.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ The following arguments are supported:
* `name` - (Required) The display name of a resource selection document.
* `plan_id` - (Required) The backup plan ID to be associated with the selection of resources.
* `iam_role` - (Required) The ARN of the IAM role that AWS Backup uses to authenticate when restoring the target resource.
* `tag` - (Optional) Tag-based conditions used to specify a set of resources to assign to a backup plan.
* `selection_tag` - (Optional) Tag-based conditions used to specify a set of resources to assign to a backup plan.
* `resources` - (Optional) An array of strings that either contain Amazon Resource Names (ARNs) or match patterns of resources to assign to a backup plan..

Tag conditions (`tag`) support the following:
Tag conditions (`selection_tag`) support the following:

* `type` - (Required) An operation, such as `StringEquals`, that is applied to a key-value pair used to filter resources in a selection.
* `key` - (Required) The key in a key-value pair.
Expand Down

0 comments on commit c80dd6f

Please sign in to comment.