From 09c2620c485f08bc518491af525d8c5fe017b067 Mon Sep 17 00:00:00 2001 From: Aleksandr Serbin Date: Wed, 4 Nov 2020 20:29:09 +0100 Subject: [PATCH] amazon: validate IOPS max and min values --- builder/amazon/common/block_device.go | 14 ++++++++++-- builder/amazon/common/block_device_test.go | 26 ++++++++++++++++++++-- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/builder/amazon/common/block_device.go b/builder/amazon/common/block_device.go index 8b7fe5246..47d683532 100644 --- a/builder/amazon/common/block_device.go +++ b/builder/amazon/common/block_device.go @@ -13,6 +13,11 @@ import ( "github.com/hashicorp/packer/template/interpolate" ) +const ( + minIops = 100 + maxIops = 64000 +) + // These will be attached when launching your instance. Your // options here may vary depending on the type of VM you use. // @@ -173,11 +178,16 @@ func (b *BlockDevice) Prepare(ctx *interpolate.Context) error { if ratio, ok := iopsRatios[b.VolumeType]; b.VolumeSize != 0 && ok { if b.IOPS/b.VolumeSize > ratio { - return fmt.Errorf("The maximum ratio of provisioned IOPS to requested volume size "+ - "(in GiB) is %v:1 for %s volumes", ratio, b.VolumeType) + return fmt.Errorf("%s: the maximum ratio of provisioned IOPS to requested volume size "+ + "(in GiB) is %v:1 for %s volumes", b.DeviceName, ratio, b.VolumeType) } } + if b.IOPS < minIops || b.IOPS > maxIops { + return fmt.Errorf("IOPS must be between %d and %d for device %s", + minIops, maxIops, b.DeviceName) + } + _, err := interpolate.RenderInterface(&b, ctx) return err } diff --git a/builder/amazon/common/block_device_test.go b/builder/amazon/common/block_device_test.go index d6f9e665a..2e909117a 100644 --- a/builder/amazon/common/block_device_test.go +++ b/builder/amazon/common/block_device_test.go @@ -236,7 +236,7 @@ func TestIOPSValidation(t *testing.T) { IOPS: 2000, }, ok: false, - msg: "The maximum ratio of provisioned IOPS to requested volume size (in GiB) is 50:1 for io1 volumes", + msg: "/dev/sdb: the maximum ratio of provisioned IOPS to requested volume size (in GiB) is 50:1 for io1 volumes", }, { device: BlockDevice{ @@ -246,7 +246,29 @@ func TestIOPSValidation(t *testing.T) { IOPS: 30000, }, ok: false, - msg: "The maximum ratio of provisioned IOPS to requested volume size (in GiB) is 500:1 for io2 volumes", + msg: "/dev/sdb: the maximum ratio of provisioned IOPS to requested volume size (in GiB) is 500:1 for io2 volumes", + }, + // exceed max iops + { + device: BlockDevice{ + DeviceName: "/dev/sdb", + VolumeType: "io2", + VolumeSize: 500, + IOPS: 99999, + }, + ok: false, + msg: "IOPS must be between 100 and 64000 for device /dev/sdb", + }, + // lower than min iops + { + device: BlockDevice{ + DeviceName: "/dev/sdb", + VolumeType: "io2", + VolumeSize: 50, + IOPS: 10, + }, + ok: false, + msg: "IOPS must be between 100 and 64000 for device /dev/sdb", }, }