forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_aws_ebs_volume.go
153 lines (135 loc) · 3.55 KB
/
resource_aws_ebs_volume.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
package aws
import (
"fmt"
"github.com/awslabs/aws-sdk-go/aws"
"github.com/awslabs/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsEbsVolume() *schema.Resource {
return &schema.Resource{
Create: resourceAwsEbsVolumeCreate,
Read: resourceAwsEbsVolumeRead,
Delete: resourceAwsEbsVolumeDelete,
Schema: map[string]*schema.Schema{
"availability_zone": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"encrypted": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Computed: true,
ForceNew: true,
},
"iops": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: true,
},
"kms_key_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"size": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: true,
},
"snapshot_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"type": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
},
}
}
func resourceAwsEbsVolumeCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
request := &ec2.CreateVolumeInput{
AvailabilityZone: aws.String(d.Get("availability_zone").(string)),
}
if value, ok := d.GetOk("encrypted"); ok {
request.Encrypted = aws.Boolean(value.(bool))
}
if value, ok := d.GetOk("iops"); ok {
request.IOPS = aws.Long(int64(value.(int)))
}
if value, ok := d.GetOk("kms_key_id"); ok {
request.KMSKeyID = aws.String(value.(string))
}
if value, ok := d.GetOk("size"); ok {
request.Size = aws.Long(int64(value.(int)))
}
if value, ok := d.GetOk("snapshot_id"); ok {
request.SnapshotID = aws.String(value.(string))
}
if value, ok := d.GetOk("type"); ok {
request.VolumeType = aws.String(value.(string))
}
result, err := conn.CreateVolume(request)
if err != nil {
return fmt.Errorf("Error creating EC2 volume: %s", err)
}
return readVolume(d, result)
}
func resourceAwsEbsVolumeRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
request := &ec2.DescribeVolumesInput{
VolumeIDs: []*string{aws.String(d.Id())},
}
response, err := conn.DescribeVolumes(request)
if err != nil {
if ec2err, ok := err.(aws.APIError); ok && ec2err.Code == "InvalidVolume.NotFound" {
d.SetId("")
return nil
}
return fmt.Errorf("Error reading EC2 volume %s: %#v", d.Id(), err)
}
return readVolume(d, response.Volumes[0])
}
func resourceAwsEbsVolumeDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
request := &ec2.DeleteVolumeInput{
VolumeID: aws.String(d.Id()),
}
_, err := conn.DeleteVolume(request)
if err != nil {
return fmt.Errorf("Error deleting EC2 volume %s: %s", d.Id(), err)
}
return nil
}
func readVolume(d *schema.ResourceData, volume *ec2.Volume) error {
d.SetId(*volume.VolumeID)
d.Set("availability_zone", *volume.AvailabilityZone)
if volume.Encrypted != nil {
d.Set("encrypted", *volume.Encrypted)
}
if volume.IOPS != nil {
d.Set("iops", *volume.IOPS)
}
if volume.KMSKeyID != nil {
d.Set("kms_key_id", *volume.KMSKeyID)
}
if volume.Size != nil {
d.Set("size", *volume.Size)
}
if volume.SnapshotID != nil {
d.Set("snapshot_id", *volume.SnapshotID)
}
if volume.VolumeType != nil {
d.Set("type", *volume.VolumeType)
}
return nil
}