diff --git a/builtin/provisioners/chef/linux_provisioner_test.go b/builtin/provisioners/chef/linux_provisioner_test.go index 02b58c910d..ec72f7debf 100644 --- a/builtin/provisioners/chef/linux_provisioner_test.go +++ b/builtin/provisioners/chef/linux_provisioner_test.go @@ -236,10 +236,29 @@ func TestResourceProvider_linuxCreateConfigFiles(t *testing.T) { }, }, - "String Attributes": { + "Attributes": { Config: testConfig(t, map[string]interface{}{ - "attributes": `{"key1":{"subkey1":{"subkey2a":["val1","val2","val3"],` + - `"subkey2b":{"subkey3":"value3"}}},"key2":"value2"}`, + "attributes": []map[string]interface{}{ + map[string]interface{}{ + "key1": []map[string]interface{}{ + map[string]interface{}{ + "subkey1": []map[string]interface{}{ + map[string]interface{}{ + "subkey2a": []interface{}{ + "val1", "val2", "val3", + }, + "subkey2b": []map[string]interface{}{ + map[string]interface{}{ + "subkey3": "value3", + }, + }, + }, + }, + }, + }, + "key2": "value2", + }, + }, "node_name": "nodename1", "prevent_sudo": true, "run_list": []interface{}{"cookbook::recipe"}, @@ -262,29 +281,10 @@ func TestResourceProvider_linuxCreateConfigFiles(t *testing.T) { }, }, - "Map Attributes": { + "Attributes JSON": { Config: testConfig(t, map[string]interface{}{ - "attributes": []map[string]interface{}{ - map[string]interface{}{ - "key1": []map[string]interface{}{ - map[string]interface{}{ - "subkey1": []map[string]interface{}{ - map[string]interface{}{ - "subkey2a": []interface{}{ - "val1", "val2", "val3", - }, - "subkey2b": []map[string]interface{}{ - map[string]interface{}{ - "subkey3": "value3", - }, - }, - }, - }, - }, - }, - "key2": "value2", - }, - }, + "attributes_json": `{"key1":{"subkey1":{"subkey2a":["val1","val2","val3"],` + + `"subkey2b":{"subkey3":"value3"}}},"key2":"value2"}`, "node_name": "nodename1", "prevent_sudo": true, "run_list": []interface{}{"cookbook::recipe"}, diff --git a/builtin/provisioners/chef/resource_provisioner.go b/builtin/provisioners/chef/resource_provisioner.go index 14afaad669..085603948f 100644 --- a/builtin/provisioners/chef/resource_provisioner.go +++ b/builtin/provisioners/chef/resource_provisioner.go @@ -77,6 +77,7 @@ ENV['no_proxy'] = "{{ join .NOProxy "," }}" // Provisioner represents a specificly configured chef provisioner type Provisioner struct { Attributes interface{} `mapstructure:"attributes"` + AttributesJSON string `mapstructure:"attributes_json"` ClientOptions []string `mapstructure:"client_options"` DisableReporting bool `mapstructure:"disable_reporting"` Environment string `mapstructure:"environment"` @@ -235,11 +236,9 @@ func (r *ResourceProvisioner) Validate(c *terraform.ResourceConfig) (ws []string ws = append(ws, "secret_key_path is deprecated, please use "+ "secret_key instead and load the key contents via file()") } - if attrs, ok := c.Config["attributes"]; ok { - if _, ok := attrs.(string); !ok { - ws = append(ws, "using map style attribute values is deprecated, "+ - " please use a single raw JSON string instead") - } + if _, ok := c.Config["attributes"]; ok { + ws = append(ws, "using map style attribute values is deprecated, "+ + " please use a single raw JSON string instead") } return ws, es @@ -299,19 +298,18 @@ func (r *ResourceProvisioner) decodeConfig(c *terraform.ResourceConfig) (*Provis } if attrs, ok := c.Config["attributes"]; ok { - switch attrs := attrs.(type) { - case string: - var m map[string]interface{} - if err := json.Unmarshal([]byte(attrs), &m); err != nil { - return nil, fmt.Errorf("Error parsing the attributes: %v", err) - } - p.Attributes = m - default: - p.Attributes, err = rawToJSON(attrs) - if err != nil { - return nil, fmt.Errorf("Error parsing the attributes: %v", err) - } + p.Attributes, err = rawToJSON(attrs) + if err != nil { + return nil, fmt.Errorf("Error parsing the attributes: %v", err) + } + } + + if attrs, ok := c.Config["attributes_json"]; ok { + var m map[string]interface{} + if err := json.Unmarshal([]byte(attrs.(string)), &m); err != nil { + return nil, fmt.Errorf("Error parsing the attributes: %v", err) } + p.Attributes = m } return p, nil diff --git a/builtin/provisioners/chef/windows_provisioner_test.go b/builtin/provisioners/chef/windows_provisioner_test.go index 5067057950..8dd0dee28e 100644 --- a/builtin/provisioners/chef/windows_provisioner_test.go +++ b/builtin/provisioners/chef/windows_provisioner_test.go @@ -153,10 +153,29 @@ func TestResourceProvider_windowsCreateConfigFiles(t *testing.T) { }, }, - "String Attributes": { + "Attributes": { Config: testConfig(t, map[string]interface{}{ - "attributes": `{"key1":{"subkey1":{"subkey2a":["val1","val2","val3"],` + - `"subkey2b":{"subkey3":"value3"}}},"key2":"value2"}`, + "attributes": []map[string]interface{}{ + map[string]interface{}{ + "key1": []map[string]interface{}{ + map[string]interface{}{ + "subkey1": []map[string]interface{}{ + map[string]interface{}{ + "subkey2a": []interface{}{ + "val1", "val2", "val3", + }, + "subkey2b": []map[string]interface{}{ + map[string]interface{}{ + "subkey3": "value3", + }, + }, + }, + }, + }, + }, + "key2": "value2", + }, + }, "node_name": "nodename1", "run_list": []interface{}{"cookbook::recipe"}, "secret_key_path": "test-fixtures/encrypted_data_bag_secret", @@ -178,29 +197,10 @@ func TestResourceProvider_windowsCreateConfigFiles(t *testing.T) { }, }, - "Map Attributes": { + "Attributes JSON": { Config: testConfig(t, map[string]interface{}{ - "attributes": []map[string]interface{}{ - map[string]interface{}{ - "key1": []map[string]interface{}{ - map[string]interface{}{ - "subkey1": []map[string]interface{}{ - map[string]interface{}{ - "subkey2a": []interface{}{ - "val1", "val2", "val3", - }, - "subkey2b": []map[string]interface{}{ - map[string]interface{}{ - "subkey3": "value3", - }, - }, - }, - }, - }, - }, - "key2": "value2", - }, - }, + "attributes_json": `{"key1":{"subkey1":{"subkey2a":["val1","val2","val3"],` + + `"subkey2b":{"subkey3":"value3"}}},"key2":"value2"}`, "node_name": "nodename1", "run_list": []interface{}{"cookbook::recipe"}, "secret_key_path": "test-fixtures/encrypted_data_bag_secret", diff --git a/website/source/docs/provisioners/chef.html.markdown b/website/source/docs/provisioners/chef.html.markdown index 9e5a8b9a93..9345272697 100644 --- a/website/source/docs/provisioners/chef.html.markdown +++ b/website/source/docs/provisioners/chef.html.markdown @@ -25,7 +25,7 @@ available on the target machine. resource "aws_instance" "web" { ... provisioner "chef" { - attributes = <