provisioners/chef: check IsComputed for JSON attributes

Fixes #10788

This checks `IsComputed` prior to attempting to use the JSON
configurations. Due to a change in 0.8, the prior check for simply map
existence would always succeed even with a computed value (as designed),
but we forgot to update provisioners to not do that.

There are other provisioners that also do this but to no ill effect
currently. I've only changed Chef since we know that is an issue.

This issue doesn't affect 0.9 due to helper/schema doing this
automatically for provisioners.
This commit is contained in:
Mitchell Hashimoto 2017-01-29 12:30:44 -08:00
parent c01680b7a9
commit 5fc516f99d
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
2 changed files with 24 additions and 2 deletions

View File

@ -354,7 +354,7 @@ func (r *ResourceProvisioner) decodeConfig(c *terraform.ResourceConfig) (*Provis
p.UserKey = p.ValidationKey
}
if attrs, ok := c.Config["attributes_json"].(string); ok {
if attrs, ok := c.Config["attributes_json"].(string); ok && !c.IsComputed("attributes_json") {
var m map[string]interface{}
if err := json.Unmarshal([]byte(attrs), &m); err != nil {
return nil, fmt.Errorf("Error parsing attributes_json: %v", err)
@ -362,7 +362,7 @@ func (r *ResourceProvisioner) decodeConfig(c *terraform.ResourceConfig) (*Provis
p.attributes = m
}
if vaults, ok := c.Config["vault_json"].(string); ok {
if vaults, ok := c.Config["vault_json"].(string); ok && !c.IsComputed("vault_json") {
var m map[string]interface{}
if err := json.Unmarshal([]byte(vaults), &m); err != nil {
return nil, fmt.Errorf("Error parsing vault_json: %v", err)

View File

@ -47,6 +47,28 @@ func TestResourceProvider_Validate_bad(t *testing.T) {
}
}
// Test that the JSON attributes with an unknown value don't
// validate.
func TestResourceProvider_Validate_computedValues(t *testing.T) {
c := testConfig(t, map[string]interface{}{
"environment": "_default",
"node_name": "nodename1",
"run_list": []interface{}{"cookbook::recipe"},
"server_url": "https://chef.local",
"user_name": "bob",
"user_key": "USER-KEY",
"attributes_json": config.UnknownVariableValue,
})
r := new(ResourceProvisioner)
warn, errs := r.Validate(c)
if len(warn) > 0 {
t.Fatalf("Warnings: %v", warn)
}
if len(errs) > 0 {
t.Fatalf("Errors: %v", errs)
}
}
func testConfig(t *testing.T, c map[string]interface{}) *terraform.ResourceConfig {
r, err := config.NewRawConfig(c)
if err != nil {