diff --git a/builder/googlecompute/config.go b/builder/googlecompute/config.go index 537fc7422..11e8ac47f 100644 --- a/builder/googlecompute/config.go +++ b/builder/googlecompute/config.go @@ -225,6 +225,11 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) { errs = packer.MultiErrorAppend(fmt.Errorf("'on_host_maintenance' must be set to 'TERMINATE' when 'accelerator_count' is more than 0")) } + // If DisableDefaultServiceAccount is provided, don't allow a value for ServiceAccountEmail + if c.DisableDefaultServiceAccount && c.ServiceAccountEmail != "" { + errs = packer.MultiErrorAppend(fmt.Errorf("you may not specify a 'service_account_email' when 'disable_default_service_account' is true")) + } + // 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 508b3c528..d3ed84506 100644 --- a/builder/googlecompute/config_test.go +++ b/builder/googlecompute/config_test.go @@ -276,6 +276,55 @@ func TestConfigPrepareAccelerator(t *testing.T) { } } +func TestConfigPrepareServiceAccount(t *testing.T) { + cases := []struct { + Keys []string + Values []interface{} + Err bool + }{ + { + []string{"disable_default_service_account", "service_account_email"}, + []interface{}{true, "service@account.email.com"}, + true, + }, + { + []string{"disable_default_service_account", "service_account_email"}, + []interface{}{false, "service@account.email.com"}, + false, + }, + { + []string{"disable_default_service_account", "service_account_email"}, + []interface{}{true, ""}, + 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{}