-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
resource_aws_ebs_snapshot.go
167 lines (141 loc) · 3.76 KB
/
resource_aws_ebs_snapshot.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package aws
import (
"fmt"
"log"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsEbsSnapshot() *schema.Resource {
return &schema.Resource{
Create: resourceAwsEbsSnapshotCreate,
Read: resourceAwsEbsSnapshotRead,
Delete: resourceAwsEbsSnapshotDelete,
Schema: map[string]*schema.Schema{
"volume_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"owner_id": {
Type: schema.TypeString,
Computed: true,
},
"owner_alias": {
Type: schema.TypeString,
Computed: true,
},
"encrypted": {
Type: schema.TypeBool,
Computed: true,
},
"volume_size": {
Type: schema.TypeInt,
Computed: true,
},
"kms_key_id": {
Type: schema.TypeString,
Computed: true,
},
"data_encryption_key_id": {
Type: schema.TypeString,
Computed: true,
},
"tags": {
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
},
},
}
}
func resourceAwsEbsSnapshotCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
request := &ec2.CreateSnapshotInput{
VolumeId: aws.String(d.Get("volume_id").(string)),
}
if v, ok := d.GetOk("description"); ok {
request.Description = aws.String(v.(string))
}
res, err := conn.CreateSnapshot(request)
if err != nil {
return err
}
d.SetId(*res.SnapshotId)
err = resourceAwsEbsSnapshotWaitForAvailable(d.Id(), conn)
if err != nil {
return err
}
if err := setTags(conn, d); err != nil {
log.Printf("[WARN] error setting tags: %s", err)
}
return resourceAwsEbsSnapshotRead(d, meta)
}
func resourceAwsEbsSnapshotRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
req := &ec2.DescribeSnapshotsInput{
SnapshotIds: []*string{aws.String(d.Id())},
}
res, err := conn.DescribeSnapshots(req)
if err != nil {
if isAWSErr(err, "InvalidSnapshot.NotFound", "") {
log.Printf("[WARN] Snapshot %q Not found - removing from state", d.Id())
d.SetId("")
return nil
}
return err
}
if len(res.Snapshots) == 0 {
d.SetId("")
return nil
}
snapshot := res.Snapshots[0]
d.Set("description", snapshot.Description)
d.Set("owner_id", snapshot.OwnerId)
d.Set("encrypted", snapshot.Encrypted)
d.Set("owner_alias", snapshot.OwnerAlias)
d.Set("volume_id", snapshot.VolumeId)
d.Set("data_encryption_key_id", snapshot.DataEncryptionKeyId)
d.Set("kms_key_id", snapshot.KmsKeyId)
d.Set("volume_size", snapshot.VolumeSize)
if err := d.Set("tags", tagsToMap(snapshot.Tags)); err != nil {
log.Printf("[WARN] error saving tags to state: %s", err)
}
return nil
}
func resourceAwsEbsSnapshotDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
return resource.Retry(5*time.Minute, func() *resource.RetryError {
request := &ec2.DeleteSnapshotInput{
SnapshotId: aws.String(d.Id()),
}
_, err := conn.DeleteSnapshot(request)
if err == nil {
return nil
}
ebsErr, ok := err.(awserr.Error)
if ebsErr.Code() == "SnapshotInUse" {
return resource.RetryableError(fmt.Errorf("EBS SnapshotInUse - trying again while it detaches"))
}
if !ok {
return resource.NonRetryableError(err)
}
return resource.NonRetryableError(err)
})
}
func resourceAwsEbsSnapshotWaitForAvailable(id string, conn *ec2.EC2) error {
log.Printf("Waiting for Snapshot %s to become available...", id)
req := &ec2.DescribeSnapshotsInput{
SnapshotIds: []*string{aws.String(id)},
}
err := conn.WaitUntilSnapshotCompleted(req)
return err
}