From 608307cd1ec2c4d8197d4e93e9849c266d78545e Mon Sep 17 00:00:00 2001 From: Evan Pipho Date: Mon, 12 Oct 2020 22:05:58 +0000 Subject: [PATCH] Re-allow spot + tenancy. Validate tenancy is set to a usable value --- builder/amazon/common/run_config.go | 13 ++++++++----- builder/amazon/common/run_config_test.go | 22 +++++++++++----------- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/builder/amazon/common/run_config.go b/builder/amazon/common/run_config.go index f2aea20a6..932e416ad 100644 --- a/builder/amazon/common/run_config.go +++ b/builder/amazon/common/run_config.go @@ -378,7 +378,9 @@ type RunConfig struct { SubnetId string `mapstructure:"subnet_id" required:"false"` // [Tenancy](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/dedicated-instance.html) used // when Packer launches the EC2 instance, allowing it to be launched on dedicated hardware. - // If unset, the default shared tenancy will be used. + // + // The default is "default", meaning shared tenancy. Allowed values are "default", + // "dedicated" and "host". Tenancy string `mapstructure:"tenancy" required:"false"` // The name of the temporary key pair to // generate. By default, Packer generates a name that looks like @@ -635,10 +637,11 @@ func (c *RunConfig) Prepare(ctx *interpolate.Context) []error { } } - if c.Tenancy != "" && c.Tenancy != "default" { - if c.SpotPrice != "" { - errs = append(errs, fmt.Errorf("Error: Non-default tenancy cannot be used in conjunction with Spot Instances")) - } + if c.Tenancy != "" && + c.Tenancy != "default" && + c.Tenancy != "dedicated" && + c.Tenancy != "host" { + errs = append(errs, fmt.Errorf("Error: Unknown tenancy type %s", c.Tenancy)) } return errs diff --git a/builder/amazon/common/run_config_test.go b/builder/amazon/common/run_config_test.go index 546ede250..452cdab22 100644 --- a/builder/amazon/common/run_config_test.go +++ b/builder/amazon/common/run_config_test.go @@ -233,22 +233,22 @@ func TestRunConfigPrepare_TemporaryKeyPairName(t *testing.T) { } } -func TestRunConfigPrepare_TenancySpot(t *testing.T) { +func TestRunConfigPrepare_TenancyBad(t *testing.T) { c := testConfig() - c.Tenancy = "dedicated" - c.SpotPrice = "1" + c.Tenancy = "not_real" if err := c.Prepare(nil); len(err) != 1 { - t.Fatal("Should error if non-default tenancy and spot price are both set") + t.Fatal("Should error if tenancy is set to an invalid type") } } -func TestRunConfigPrepare_TenancySpotDefault(t *testing.T) { - c := testConfig() - c.Tenancy = "default" - c.SpotPrice = "1" - - if err := c.Prepare(nil); len(err) != 0 { - t.Fatal("Should not error if tenancy is set to default with spot price") +func TestRunConfigPrepare_TenancyGood(t *testing.T) { + validTenancy := []string{"", "default", "dedicated", "host"} + for _, vt := range validTenancy { + c := testConfig() + c.Tenancy = vt + if err := c.Prepare(nil); len(err) != 0 { + t.Fatalf("Should not error if tenancy is set to %s", vt) + } } }