Skip to content

Commit

Permalink
Merge branch 'slapula-resource-aws-backup-selection'
Browse files Browse the repository at this point in the history
  • Loading branch information
bflad committed Apr 4, 2019
2 parents 26fea57 + 7524304 commit a896105
Show file tree
Hide file tree
Showing 5 changed files with 485 additions and 0 deletions.
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ func Provider() terraform.ResourceProvider {
"aws_autoscaling_policy": resourceAwsAutoscalingPolicy(),
"aws_autoscaling_schedule": resourceAwsAutoscalingSchedule(),
"aws_backup_plan": resourceAwsBackupPlan(),
"aws_backup_selection": resourceAwsBackupSelection(),
"aws_backup_vault": resourceAwsBackupVault(),
"aws_budgets_budget": resourceAwsBudgetsBudget(),
"aws_cloud9_environment_ec2": resourceAwsCloud9EnvironmentEc2(),
Expand Down
180 changes: 180 additions & 0 deletions aws/resource_aws_backup_selection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
package aws

import (
"fmt"
"log"
"regexp"

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

func resourceAwsBackupSelection() *schema.Resource {
return &schema.Resource{
Create: resourceAwsBackupSelectionCreate,
Read: resourceAwsBackupSelectionRead,
Delete: resourceAwsBackupSelectionDelete,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.All(
validation.StringLenBetween(1, 50),
validation.StringMatch(regexp.MustCompile(`^[a-zA-Z0-9\-\_\.]+$`), "must contain only alphanumeric, hyphen, underscore, and period characters"),
),
},
"plan_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"iam_role_arn": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateArn,
},
"selection_tag": {
Type: schema.TypeSet,
Optional: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
backup.ConditionTypeStringequals,
}, false),
},
"key": {
Type: schema.TypeString,
Required: true,
},
"value": {
Type: schema.TypeString,
Required: true,
},
},
},
},
"resources": {
Type: schema.TypeSet,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}

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

selection := &backup.Selection{
IamRoleArn: aws.String(d.Get("iam_role_arn").(string)),
ListOfTags: expandBackupConditionTags(d.Get("selection_tag").(*schema.Set).List()),
Resources: expandStringSet(d.Get("resources").(*schema.Set)),
SelectionName: aws.String(d.Get("name").(string)),
}

input := &backup.CreateBackupSelectionInput{
BackupPlanId: aws.String(d.Get("plan_id").(string)),
BackupSelection: selection,
}

resp, err := conn.CreateBackupSelection(input)
if err != nil {
return fmt.Errorf("error creating Backup Selection: %s", err)
}

d.SetId(*resp.SelectionId)

return resourceAwsBackupSelectionRead(d, meta)
}

func resourceAwsBackupSelectionRead(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.Id()),
}

resp, err := conn.GetBackupSelection(input)
if isAWSErr(err, backup.ErrCodeResourceNotFoundException, "") {
log.Printf("[WARN] Backup Selection (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

if err != nil {
return fmt.Errorf("error reading Backup Selection: %s", err)
}

d.Set("plan_id", resp.BackupPlanId)
d.Set("name", resp.BackupSelection.SelectionName)
d.Set("iam_role_arn", resp.BackupSelection.IamRoleArn)

if resp.BackupSelection.ListOfTags != nil {
tags := make([]map[string]interface{}, 0)

for _, r := range resp.BackupSelection.ListOfTags {
m := make(map[string]interface{})

m["type"] = aws.StringValue(r.ConditionType)
m["key"] = aws.StringValue(r.ConditionKey)
m["value"] = aws.StringValue(r.ConditionValue)

tags = append(tags, m)
}

if err := d.Set("selection_tag", tags); err != nil {
return fmt.Errorf("error setting selection tag: %s", err)
}
}
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
}

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

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

_, err := conn.DeleteBackupSelection(input)
if err != nil {
return fmt.Errorf("error deleting Backup Selection: %s", err)
}

return nil
}

func expandBackupConditionTags(tagList []interface{}) []*backup.Condition {
conditions := []*backup.Condition{}

for _, i := range tagList {
item := i.(map[string]interface{})
tag := &backup.Condition{}

tag.ConditionType = aws.String(item["type"].(string))
tag.ConditionKey = aws.String(item["key"].(string))
tag.ConditionValue = aws.String(item["value"].(string))

conditions = append(conditions, tag)
}

return conditions
}
Loading

0 comments on commit a896105

Please sign in to comment.