Merge pull request #4146 from hashicorp/b-aws-ebs-validate

providers/aws: Validate IOPs for EBS Volumes
This commit is contained in:
Clint 2015-12-15 11:33:49 -06:00
commit 64482422b5
3 changed files with 61 additions and 11 deletions

View File

@ -76,9 +76,6 @@ func resourceAwsEbsVolumeCreate(d *schema.ResourceData, meta interface{}) error
if value, ok := d.GetOk("encrypted"); ok { if value, ok := d.GetOk("encrypted"); ok {
request.Encrypted = aws.Bool(value.(bool)) request.Encrypted = aws.Bool(value.(bool))
} }
if value, ok := d.GetOk("iops"); ok {
request.Iops = aws.Int64(int64(value.(int)))
}
if value, ok := d.GetOk("kms_key_id"); ok { if value, ok := d.GetOk("kms_key_id"); ok {
request.KmsKeyId = aws.String(value.(string)) request.KmsKeyId = aws.String(value.(string))
} }
@ -88,18 +85,35 @@ func resourceAwsEbsVolumeCreate(d *schema.ResourceData, meta interface{}) error
if value, ok := d.GetOk("snapshot_id"); ok { if value, ok := d.GetOk("snapshot_id"); ok {
request.SnapshotId = aws.String(value.(string)) request.SnapshotId = aws.String(value.(string))
} }
// IOPs are only valid, and required for, storage type io1. The current minimu
// is 100. Instead of a hard validation we we only apply the IOPs to the
// request if the type is io1, and log a warning otherwise. This allows users
// to "disable" iops. See https://github.com/hashicorp/terraform/pull/4146
var t string
if value, ok := d.GetOk("type"); ok { if value, ok := d.GetOk("type"); ok {
request.VolumeType = aws.String(value.(string)) t = value.(string)
request.VolumeType = aws.String(t)
} }
iops := d.Get("iops").(int)
if t != "io1" && iops > 0 {
log.Printf("[WARN] IOPs is only valid for storate type io1 for EBS Volumes")
} else if t == "io1" {
// We add the iops value without validating it's size, to allow AWS to
// enforce a size requirement (currently 100)
request.Iops = aws.Int64(int64(iops))
}
log.Printf(
"[DEBUG] EBS Volume create opts: %s", request)
result, err := conn.CreateVolume(request) result, err := conn.CreateVolume(request)
if err != nil { if err != nil {
return fmt.Errorf("Error creating EC2 volume: %s", err) return fmt.Errorf("Error creating EC2 volume: %s", err)
} }
log.Printf( log.Println(
"[DEBUG] Waiting for Volume (%s) to become available", "[DEBUG] Waiting for Volume to become available")
d.Id())
stateConf := &resource.StateChangeConf{ stateConf := &resource.StateChangeConf{
Pending: []string{"creating"}, Pending: []string{"creating"},
@ -199,9 +213,6 @@ func readVolume(d *schema.ResourceData, volume *ec2.Volume) error {
if volume.Encrypted != nil { if volume.Encrypted != nil {
d.Set("encrypted", *volume.Encrypted) d.Set("encrypted", *volume.Encrypted)
} }
if volume.Iops != nil {
d.Set("iops", *volume.Iops)
}
if volume.KmsKeyId != nil { if volume.KmsKeyId != nil {
d.Set("kms_key_id", *volume.KmsKeyId) d.Set("kms_key_id", *volume.KmsKeyId)
} }
@ -214,6 +225,17 @@ func readVolume(d *schema.ResourceData, volume *ec2.Volume) error {
if volume.VolumeType != nil { if volume.VolumeType != nil {
d.Set("type", *volume.VolumeType) d.Set("type", *volume.VolumeType)
} }
if volume.VolumeType != nil && *volume.VolumeType == "io1" {
// Only set the iops attribute if the volume type is io1. Setting otherwise
// can trigger a refresh/plan loop based on the computed value that is given
// from AWS, and prevent us from specifying 0 as a valid iops.
// See https://github.com/hashicorp/terraform/pull/4146
if volume.Iops != nil {
d.Set("iops", *volume.Iops)
}
}
if volume.Tags != nil { if volume.Tags != nil {
d.Set("tags", tagsToMap(volume.Tags)) d.Set("tags", tagsToMap(volume.Tags))
} }

View File

@ -26,6 +26,22 @@ func TestAccAWSEBSVolume_basic(t *testing.T) {
}) })
} }
func TestAccAWSEBSVolume_NoIops(t *testing.T) {
var v ec2.Volume
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAwsEbsVolumeConfigWithNoIops,
Check: resource.ComposeTestCheckFunc(
testAccCheckVolumeExists("aws_ebs_volume.iops_test", &v),
),
},
},
})
}
func TestAccAWSEBSVolume_withTags(t *testing.T) { func TestAccAWSEBSVolume_withTags(t *testing.T) {
var v ec2.Volume var v ec2.Volume
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
@ -86,3 +102,15 @@ resource "aws_ebs_volume" "tags_test" {
} }
} }
` `
const testAccAwsEbsVolumeConfigWithNoIops = `
resource "aws_ebs_volume" "iops_test" {
availability_zone = "us-west-2a"
size = 10
type = "gp2"
iops = 0
tags {
Name = "TerraformTest"
}
}
`

View File

@ -14,7 +14,7 @@ Manages a single EBS volume.
``` ```
resource "aws_ebs_volume" "example" { resource "aws_ebs_volume" "example" {
availability_zone = "us-west-1a" availability_zone = "us-west-2a"
size = 40 size = 40
tags { tags {
Name = "HelloWorld" Name = "HelloWorld"