Re-allow spot + tenancy. Validate tenancy is set to a usable value

This commit is contained in:
Evan Pipho 2020-10-12 22:05:58 +00:00
parent d5d1a8708e
commit 608307cd1e
2 changed files with 19 additions and 16 deletions

View File

@ -378,7 +378,9 @@ type RunConfig struct {
SubnetId string `mapstructure:"subnet_id" required:"false"` SubnetId string `mapstructure:"subnet_id" required:"false"`
// [Tenancy](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/dedicated-instance.html) used // [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. // 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"` Tenancy string `mapstructure:"tenancy" required:"false"`
// The name of the temporary key pair to // The name of the temporary key pair to
// generate. By default, Packer generates a name that looks like // 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.Tenancy != "" &&
if c.SpotPrice != "" { c.Tenancy != "default" &&
errs = append(errs, fmt.Errorf("Error: Non-default tenancy cannot be used in conjunction with Spot Instances")) c.Tenancy != "dedicated" &&
} c.Tenancy != "host" {
errs = append(errs, fmt.Errorf("Error: Unknown tenancy type %s", c.Tenancy))
} }
return errs return errs

View File

@ -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 := testConfig()
c.Tenancy = "dedicated" c.Tenancy = "not_real"
c.SpotPrice = "1"
if err := c.Prepare(nil); len(err) != 1 { 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) { func TestRunConfigPrepare_TenancyGood(t *testing.T) {
c := testConfig() validTenancy := []string{"", "default", "dedicated", "host"}
c.Tenancy = "default" for _, vt := range validTenancy {
c.SpotPrice = "1" c := testConfig()
c.Tenancy = vt
if err := c.Prepare(nil); len(err) != 0 { if err := c.Prepare(nil); len(err) != 0 {
t.Fatal("Should not error if tenancy is set to default with spot price") t.Fatalf("Should not error if tenancy is set to %s", vt)
}
} }
} }