builder/googlecompute: disambiguate disable_default_service_account

This change requires 'disable_default_service_account=false' in order to
set 'service_account_email'.

This is a guard against an incorrect assumption
that disabling the default service account would mean that no service
account would be used.
This commit is contained in:
Evan Brown 2018-03-13 12:36:14 -07:00
parent 4a2c124ea2
commit 7eda44d28c
2 changed files with 54 additions and 0 deletions

View File

@ -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

View File

@ -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{}