-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
resource_aws_backup_vault.go
139 lines (116 loc) · 3.87 KB
/
resource_aws_backup_vault.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)
func resourceAwsBackupVault() *schema.Resource {
return &schema.Resource{
Create: resourceAwsBackupVaultCreate,
Read: resourceAwsBackupVaultRead,
Update: resourceAwsBackupVaultUpdate,
Delete: resourceAwsBackupVaultDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringMatch(regexp.MustCompile(`^[a-zA-Z0-9\-\_\.]{1,50}$`), "must consist of lowercase letters, numbers, and hyphens."),
},
"tags": tagsSchema(),
"kms_key_arn": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: 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)),
BackupVaultTags: keyvaluetags.New(d.Get("tags").(map[string]interface{})).IgnoreAws().BackupTags(),
}
if v, ok := d.GetOk("kms_key_arn"); ok {
input.EncryptionKeyArn = aws.String(v.(string))
}
_, err := conn.CreateBackupVault(input)
if err != nil {
return fmt.Errorf("error creating Backup Vault (%s): %s", d.Id(), err)
}
d.SetId(d.Get("name").(string))
return resourceAwsBackupVaultRead(d, meta)
}
func resourceAwsBackupVaultRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).backupconn
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig
input := &backup.DescribeBackupVaultInput{
BackupVaultName: aws.String(d.Id()),
}
resp, err := conn.DescribeBackupVault(input)
if isAWSErr(err, backup.ErrCodeResourceNotFoundException, "") {
log.Printf("[WARN] Backup Vault %s not found, removing from state", d.Id())
d.SetId("")
return nil
}
if isAWSErr(err, "AccessDeniedException", "") {
log.Printf("[WARN] Backup Vault %s not found, removing from state", d.Id())
d.SetId("")
return nil
}
if err != nil {
return fmt.Errorf("error reading Backup Vault (%s): %s", d.Id(), err)
}
d.Set("name", resp.BackupVaultName)
d.Set("kms_key_arn", resp.EncryptionKeyArn)
d.Set("arn", resp.BackupVaultArn)
d.Set("recovery_points", resp.NumberOfRecoveryPoints)
tags, err := keyvaluetags.BackupListTags(conn, d.Get("arn").(string))
if err != nil {
return fmt.Errorf("error listing tags for Backup Vault (%s): %s", d.Id(), err)
}
if err := d.Set("tags", tags.IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}
return nil
}
func resourceAwsBackupVaultUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).backupconn
if d.HasChange("tags") {
o, n := d.GetChange("tags")
if err := keyvaluetags.BackupUpdateTags(conn, d.Get("arn").(string), o, n); err != nil {
return fmt.Errorf("error updating tags for Backup Vault (%s): %s", d.Id(), err)
}
}
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 fmt.Errorf("error deleting Backup Vault (%s): %s", d.Id(), err)
}
return nil
}