diff --git a/builder/googlecompute/config.go b/builder/googlecompute/config.go index 5bf44632c..99c52a704 100644 --- a/builder/googlecompute/config.go +++ b/builder/googlecompute/config.go @@ -207,6 +207,14 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) { errs = packer.MultiErrorAppend(fmt.Errorf("'use_internal_ip' must be true if 'omit_external_ip' is true")) } + if c.AcceleratorCount > 0 && len(c.AcceleratorType) == 0 { + errs = packer.MultiErrorAppend(fmt.Errorf("'accelerator_type' must be set when 'accelerator_count' is more than 0")) + } + + if c.AcceleratorCount > 0 && c.OnHostMaintenance != "TERMINATE" { + errs = packer.MultiErrorAppend(fmt.Errorf("'on_host_maintenance' must be set to 'TERMINATE' when 'accelerator_count' is more than 0")) + } + // Check for any errors. if errs != nil && len(errs.Errors) > 0 { return nil, nil, errs diff --git a/builder/googlecompute/config_test.go b/builder/googlecompute/config_test.go index e65da2794..7255add30 100644 --- a/builder/googlecompute/config_test.go +++ b/builder/googlecompute/config_test.go @@ -1,6 +1,7 @@ package googlecompute import ( + "fmt" "io/ioutil" "strings" "testing" @@ -190,6 +191,65 @@ func TestConfigPrepare(t *testing.T) { } } +func TestConfigPrepareAccelerator(t *testing.T) { + cases := []struct { + Keys []string + Values []interface{} + Err bool + }{ + { + []string{"accelerator_count", "on_host_maintenance", "accelerator_type"}, + []interface{}{1, "MIGRATE", "something_valid"}, + true, + }, + { + []string{"accelerator_count", "on_host_maintenance", "accelerator_type"}, + []interface{}{1, "TERMINATE", "something_valid"}, + false, + }, + { + []string{"accelerator_count", "on_host_maintenance", "accelerator_type"}, + []interface{}{1, "TERMINATE", nil}, + true, + }, + { + []string{"accelerator_count", "on_host_maintenance", "accelerator_type"}, + []interface{}{1, "TERMINATE", ""}, + true, + }, + { + []string{"accelerator_count", "on_host_maintenance", "accelerator_type"}, + []interface{}{1, "TERMINATE", "something_valid"}, + false, + }, + } + + for _, tc := range cases { + raw := testConfig(t) + + errStr := "" + for k := range tc.Keys { + + // Create the string for error reporting + // convert value to string if it can be converted + errStr += fmt.Sprintf("%s:%v, ", tc.Keys[k], tc.Values[k]) + if tc.Values[k] == nil { + delete(raw, tc.Keys[k]) + } else { + raw[tc.Keys[k]] = tc.Values[k] + } + } + + _, warns, errs := NewConfig(raw) + + if tc.Err { + testConfigErr(t, warns, errs, strings.TrimRight(errStr, ", ")) + } else { + testConfigOk(t, warns, errs) + } + } +} + func TestConfigDefaults(t *testing.T) { cases := []struct { Read func(c *Config) interface{}