Add `attributes_json` param for consistency

Add `attributes_json` param for both consistency and easier management
of deprecating the old `attributes` param.
pull/4906/head
Sander van Harmelen 10 years ago
parent 79e2642dab
commit ac0cbd400e

@ -236,10 +236,29 @@ func TestResourceProvider_linuxCreateConfigFiles(t *testing.T) {
}, },
}, },
"String Attributes": { "Attributes": {
Config: testConfig(t, map[string]interface{}{ Config: testConfig(t, map[string]interface{}{
"attributes": `{"key1":{"subkey1":{"subkey2a":["val1","val2","val3"],` + "attributes": []map[string]interface{}{
`"subkey2b":{"subkey3":"value3"}}},"key2":"value2"}`, 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", "node_name": "nodename1",
"prevent_sudo": true, "prevent_sudo": true,
"run_list": []interface{}{"cookbook::recipe"}, "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{}{ Config: testConfig(t, map[string]interface{}{
"attributes": []map[string]interface{}{ "attributes_json": `{"key1":{"subkey1":{"subkey2a":["val1","val2","val3"],` +
map[string]interface{}{ `"subkey2b":{"subkey3":"value3"}}},"key2":"value2"}`,
"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", "node_name": "nodename1",
"prevent_sudo": true, "prevent_sudo": true,
"run_list": []interface{}{"cookbook::recipe"}, "run_list": []interface{}{"cookbook::recipe"},

@ -77,6 +77,7 @@ ENV['no_proxy'] = "{{ join .NOProxy "," }}"
// Provisioner represents a specificly configured chef provisioner // Provisioner represents a specificly configured chef provisioner
type Provisioner struct { type Provisioner struct {
Attributes interface{} `mapstructure:"attributes"` Attributes interface{} `mapstructure:"attributes"`
AttributesJSON string `mapstructure:"attributes_json"`
ClientOptions []string `mapstructure:"client_options"` ClientOptions []string `mapstructure:"client_options"`
DisableReporting bool `mapstructure:"disable_reporting"` DisableReporting bool `mapstructure:"disable_reporting"`
Environment string `mapstructure:"environment"` 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 "+ ws = append(ws, "secret_key_path is deprecated, please use "+
"secret_key instead and load the key contents via file()") "secret_key instead and load the key contents via file()")
} }
if attrs, ok := c.Config["attributes"]; ok { if _, ok := c.Config["attributes"]; ok {
if _, ok := attrs.(string); !ok { ws = append(ws, "using map style attribute values is deprecated, "+
ws = append(ws, "using map style attribute values is deprecated, "+ " please use a single raw JSON string instead")
" please use a single raw JSON string instead")
}
} }
return ws, es return ws, es
@ -299,19 +298,18 @@ func (r *ResourceProvisioner) decodeConfig(c *terraform.ResourceConfig) (*Provis
} }
if attrs, ok := c.Config["attributes"]; ok { if attrs, ok := c.Config["attributes"]; ok {
switch attrs := attrs.(type) { p.Attributes, err = rawToJSON(attrs)
case string: if err != nil {
var m map[string]interface{} return nil, fmt.Errorf("Error parsing the attributes: %v", err)
if err := json.Unmarshal([]byte(attrs), &m); err != nil { }
return nil, fmt.Errorf("Error parsing the attributes: %v", err) }
}
p.Attributes = m if attrs, ok := c.Config["attributes_json"]; ok {
default: var m map[string]interface{}
p.Attributes, err = rawToJSON(attrs) if err := json.Unmarshal([]byte(attrs.(string)), &m); err != nil {
if err != nil { return nil, fmt.Errorf("Error parsing the attributes: %v", err)
return nil, fmt.Errorf("Error parsing the attributes: %v", err)
}
} }
p.Attributes = m
} }
return p, nil return p, nil

@ -153,10 +153,29 @@ func TestResourceProvider_windowsCreateConfigFiles(t *testing.T) {
}, },
}, },
"String Attributes": { "Attributes": {
Config: testConfig(t, map[string]interface{}{ Config: testConfig(t, map[string]interface{}{
"attributes": `{"key1":{"subkey1":{"subkey2a":["val1","val2","val3"],` + "attributes": []map[string]interface{}{
`"subkey2b":{"subkey3":"value3"}}},"key2":"value2"}`, 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", "node_name": "nodename1",
"run_list": []interface{}{"cookbook::recipe"}, "run_list": []interface{}{"cookbook::recipe"},
"secret_key_path": "test-fixtures/encrypted_data_bag_secret", "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{}{ Config: testConfig(t, map[string]interface{}{
"attributes": []map[string]interface{}{ "attributes_json": `{"key1":{"subkey1":{"subkey2a":["val1","val2","val3"],` +
map[string]interface{}{ `"subkey2b":{"subkey3":"value3"}}},"key2":"value2"}`,
"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", "node_name": "nodename1",
"run_list": []interface{}{"cookbook::recipe"}, "run_list": []interface{}{"cookbook::recipe"},
"secret_key_path": "test-fixtures/encrypted_data_bag_secret", "secret_key_path": "test-fixtures/encrypted_data_bag_secret",

@ -25,7 +25,7 @@ available on the target machine.
resource "aws_instance" "web" { resource "aws_instance" "web" {
... ...
provisioner "chef" { provisioner "chef" {
attributes = <<EOF attributes_json = <<EOF
{ {
"key": "value", "key": "value",
"app": { "app": {
@ -54,7 +54,7 @@ resource "aws_instance" "web" {
The following arguments are supported: The following arguments are supported:
* `attributes (string)` - (Optional) A raw JSON string with initial node attributes * `attributes_json (string)` - (Optional) A raw JSON string with initial node attributes
for the new node. These can also be loaded from a file on disk using the [`file()` for the new node. These can also be loaded from a file on disk using the [`file()`
interpolation function](/docs/configuration/interpolation.html#file_path_). interpolation function](/docs/configuration/interpolation.html#file_path_).
@ -128,5 +128,6 @@ The following arguments are supported:
These are supported for backwards compatibility and may be removed in a These are supported for backwards compatibility and may be removed in a
future version: future version:
* `validation_key_path (string)` - __Deprecated: please use `validation_key` instead__. * `attributes (map)` - __Deprecated: please use `attributes_json` instead__.
* `secret_key_path (string)` - __Deprecated: please use `secret_key` instead__. * `secret_key_path (string)` - __Deprecated: please use `secret_key` instead__.
* `validation_key_path (string)` - __Deprecated: please use `validation_key` instead__.

Loading…
Cancel
Save