2015-04-23 20:35:51 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-05-28 00:07:04 +02:00
|
|
|
"log"
|
|
|
|
"time"
|
2015-04-23 20:35:51 +02:00
|
|
|
|
2015-06-03 20:36:57 +02:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2015-04-23 20:35:51 +02:00
|
|
|
|
2015-05-28 00:07:04 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
2015-04-23 20:35:51 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceAwsEbsVolume() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceAwsEbsVolumeCreate,
|
|
|
|
Read: resourceAwsEbsVolumeRead,
|
2015-05-29 09:05:21 +02:00
|
|
|
Update: resourceAWSEbsVolumeUpdate,
|
2015-04-23 20:35:51 +02:00
|
|
|
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,
|
|
|
|
},
|
2015-05-29 09:05:21 +02:00
|
|
|
"tags": tagsSchema(),
|
2015-04-23 20:35:51 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2015-07-28 22:29:46 +02:00
|
|
|
request.Encrypted = aws.Bool(value.(bool))
|
2015-04-23 20:35:51 +02:00
|
|
|
}
|
|
|
|
if value, ok := d.GetOk("iops"); ok {
|
2015-08-17 20:27:16 +02:00
|
|
|
request.Iops = aws.Int64(int64(value.(int)))
|
2015-04-23 20:35:51 +02:00
|
|
|
}
|
|
|
|
if value, ok := d.GetOk("kms_key_id"); ok {
|
2015-08-17 20:27:16 +02:00
|
|
|
request.KmsKeyId = aws.String(value.(string))
|
2015-04-23 20:35:51 +02:00
|
|
|
}
|
|
|
|
if value, ok := d.GetOk("size"); ok {
|
2015-07-28 22:29:46 +02:00
|
|
|
request.Size = aws.Int64(int64(value.(int)))
|
2015-04-23 20:35:51 +02:00
|
|
|
}
|
|
|
|
if value, ok := d.GetOk("snapshot_id"); ok {
|
2015-08-17 20:27:16 +02:00
|
|
|
request.SnapshotId = aws.String(value.(string))
|
2015-04-23 20:35:51 +02:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
2015-05-28 00:07:04 +02:00
|
|
|
|
|
|
|
log.Printf(
|
|
|
|
"[DEBUG] Waiting for Volume (%s) to become available",
|
|
|
|
d.Id())
|
|
|
|
|
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Pending: []string{"creating"},
|
|
|
|
Target: "available",
|
2015-08-17 20:27:16 +02:00
|
|
|
Refresh: volumeStateRefreshFunc(conn, *result.VolumeId),
|
2015-05-28 00:07:04 +02:00
|
|
|
Timeout: 5 * time.Minute,
|
|
|
|
Delay: 10 * time.Second,
|
|
|
|
MinTimeout: 3 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = stateConf.WaitForState()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error waiting for Volume (%s) to become available: %s",
|
2015-08-17 20:27:16 +02:00
|
|
|
*result.VolumeId, err)
|
2015-05-28 00:07:04 +02:00
|
|
|
}
|
|
|
|
|
2015-08-17 20:27:16 +02:00
|
|
|
d.SetId(*result.VolumeId)
|
2015-05-29 09:05:21 +02:00
|
|
|
|
|
|
|
if _, ok := d.GetOk("tags"); ok {
|
|
|
|
setTags(conn, d)
|
|
|
|
}
|
|
|
|
|
2015-04-23 20:35:51 +02:00
|
|
|
return readVolume(d, result)
|
|
|
|
}
|
|
|
|
|
2015-05-29 09:05:21 +02:00
|
|
|
func resourceAWSEbsVolumeUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).ec2conn
|
|
|
|
if _, ok := d.GetOk("tags"); ok {
|
|
|
|
setTags(conn, d)
|
|
|
|
}
|
|
|
|
return resourceAwsEbsVolumeRead(d, meta)
|
|
|
|
}
|
|
|
|
|
2015-05-28 00:07:04 +02:00
|
|
|
// volumeStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch
|
|
|
|
// a the state of a Volume. Returns successfully when volume is available
|
|
|
|
func volumeStateRefreshFunc(conn *ec2.EC2, volumeID string) resource.StateRefreshFunc {
|
|
|
|
return func() (interface{}, string, error) {
|
|
|
|
resp, err := conn.DescribeVolumes(&ec2.DescribeVolumesInput{
|
2015-08-17 20:27:16 +02:00
|
|
|
VolumeIds: []*string{aws.String(volumeID)},
|
2015-05-28 00:07:04 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if ec2err, ok := err.(awserr.Error); ok {
|
|
|
|
// Set this to nil as if we didn't find anything.
|
|
|
|
log.Printf("Error on Volume State Refresh: message: \"%s\", code:\"%s\"", ec2err.Message(), ec2err.Code())
|
|
|
|
resp = nil
|
|
|
|
return nil, "", err
|
|
|
|
} else {
|
|
|
|
log.Printf("Error on Volume State Refresh: %s", err)
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
v := resp.Volumes[0]
|
|
|
|
return v, *v.State, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-23 20:35:51 +02:00
|
|
|
func resourceAwsEbsVolumeRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).ec2conn
|
|
|
|
|
|
|
|
request := &ec2.DescribeVolumesInput{
|
2015-08-17 20:27:16 +02:00
|
|
|
VolumeIds: []*string{aws.String(d.Id())},
|
2015-04-23 20:35:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
response, err := conn.DescribeVolumes(request)
|
|
|
|
if err != nil {
|
2015-05-20 13:21:23 +02:00
|
|
|
if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidVolume.NotFound" {
|
2015-04-23 20:35:51 +02:00
|
|
|
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{
|
2015-08-17 20:27:16 +02:00
|
|
|
VolumeId: aws.String(d.Id()),
|
2015-04-23 20:35:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_, 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 {
|
2015-08-17 20:27:16 +02:00
|
|
|
d.SetId(*volume.VolumeId)
|
2015-04-23 20:35:51 +02:00
|
|
|
|
|
|
|
d.Set("availability_zone", *volume.AvailabilityZone)
|
|
|
|
if volume.Encrypted != nil {
|
|
|
|
d.Set("encrypted", *volume.Encrypted)
|
|
|
|
}
|
2015-08-17 20:27:16 +02:00
|
|
|
if volume.Iops != nil {
|
|
|
|
d.Set("iops", *volume.Iops)
|
2015-04-23 20:35:51 +02:00
|
|
|
}
|
2015-08-17 20:27:16 +02:00
|
|
|
if volume.KmsKeyId != nil {
|
|
|
|
d.Set("kms_key_id", *volume.KmsKeyId)
|
2015-04-23 20:35:51 +02:00
|
|
|
}
|
|
|
|
if volume.Size != nil {
|
|
|
|
d.Set("size", *volume.Size)
|
|
|
|
}
|
2015-08-17 20:27:16 +02:00
|
|
|
if volume.SnapshotId != nil {
|
|
|
|
d.Set("snapshot_id", *volume.SnapshotId)
|
2015-04-23 20:35:51 +02:00
|
|
|
}
|
|
|
|
if volume.VolumeType != nil {
|
|
|
|
d.Set("type", *volume.VolumeType)
|
|
|
|
}
|
2015-05-29 09:05:21 +02:00
|
|
|
if volume.Tags != nil {
|
|
|
|
d.Set("tags", tagsToMap(volume.Tags))
|
|
|
|
}
|
2015-04-23 20:35:51 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|