From ac0cbd400efdb052cde4ae35aeaa756cdee77dfb Mon Sep 17 00:00:00 2001 From: Sander van Harmelen Date: Tue, 9 Feb 2016 11:11:46 +0100 Subject: [PATCH] Add `attributes_json` param for consistency Add `attributes_json` param for both consistency and easier management of deprecating the old `attributes` param. --- .../chef/linux_provisioner_test.go | 54 +++++++++---------- .../provisioners/chef/resource_provisioner.go | 32 ++++++----- .../chef/windows_provisioner_test.go | 52 +++++++++--------- .../docs/provisioners/chef.html.markdown | 7 +-- 4 files changed, 72 insertions(+), 73 deletions(-) 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,33 +236,7 @@ func TestResourceProvider_linuxCreateConfigFiles(t *testing.T) { }, }, - "String Attributes": { - Config: testConfig(t, map[string]interface{}{ - "attributes": `{"key1":{"subkey1":{"subkey2a":["val1","val2","val3"],` + - `"subkey2b":{"subkey3":"value3"}}},"key2":"value2"}`, - "node_name": "nodename1", - "prevent_sudo": true, - "run_list": []interface{}{"cookbook::recipe"}, - "secret_key_path": "test-fixtures/encrypted_data_bag_secret", - "server_url": "https://chef.local", - "validation_client_name": "validator", - "validation_key_path": "test-fixtures/validator.pem", - }), - - Commands: map[string]bool{ - "mkdir -p " + linuxConfDir: true, - }, - - Uploads: map[string]string{ - linuxConfDir + "/client.rb": defaultLinuxClientConf, - linuxConfDir + "/encrypted_data_bag_secret": "SECRET-KEY-FILE", - linuxConfDir + "/validation.pem": "VALIDATOR-PEM-FILE", - linuxConfDir + "/first-boot.json": `{"key1":{"subkey1":{"subkey2a":["val1","val2","val3"],` + - `"subkey2b":{"subkey3":"value3"}}},"key2":"value2","run_list":["cookbook::recipe"]}`, - }, - }, - - "Map Attributes": { + "Attributes": { Config: testConfig(t, map[string]interface{}{ "attributes": []map[string]interface{}{ map[string]interface{}{ @@ -306,6 +280,32 @@ func TestResourceProvider_linuxCreateConfigFiles(t *testing.T) { `"subkey2b":{"subkey3":"value3"}}},"key2":"value2","run_list":["cookbook::recipe"]}`, }, }, + + "Attributes JSON": { + Config: testConfig(t, map[string]interface{}{ + "attributes_json": `{"key1":{"subkey1":{"subkey2a":["val1","val2","val3"],` + + `"subkey2b":{"subkey3":"value3"}}},"key2":"value2"}`, + "node_name": "nodename1", + "prevent_sudo": true, + "run_list": []interface{}{"cookbook::recipe"}, + "secret_key_path": "test-fixtures/encrypted_data_bag_secret", + "server_url": "https://chef.local", + "validation_client_name": "validator", + "validation_key_path": "test-fixtures/validator.pem", + }), + + Commands: map[string]bool{ + "mkdir -p " + linuxConfDir: true, + }, + + Uploads: map[string]string{ + linuxConfDir + "/client.rb": defaultLinuxClientConf, + linuxConfDir + "/encrypted_data_bag_secret": "SECRET-KEY-FILE", + linuxConfDir + "/validation.pem": "VALIDATOR-PEM-FILE", + linuxConfDir + "/first-boot.json": `{"key1":{"subkey1":{"subkey2a":["val1","val2","val3"],` + + `"subkey2b":{"subkey3":"value3"}}},"key2":"value2","run_list":["cookbook::recipe"]}`, + }, + }, } r := new(ResourceProvisioner) 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,21 +298,20 @@ 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,32 +153,7 @@ func TestResourceProvider_windowsCreateConfigFiles(t *testing.T) { }, }, - "String Attributes": { - Config: testConfig(t, map[string]interface{}{ - "attributes": `{"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", - "server_url": "https://chef.local", - "validation_client_name": "validator", - "validation_key_path": "test-fixtures/validator.pem", - }), - - Commands: map[string]bool{ - fmt.Sprintf("cmd /c if not exist %q mkdir %q", windowsConfDir, windowsConfDir): true, - }, - - Uploads: map[string]string{ - windowsConfDir + "/client.rb": defaultWindowsClientConf, - windowsConfDir + "/encrypted_data_bag_secret": "SECRET-KEY-FILE", - windowsConfDir + "/validation.pem": "VALIDATOR-PEM-FILE", - windowsConfDir + "/first-boot.json": `{"key1":{"subkey1":{"subkey2a":["val1","val2","val3"],` + - `"subkey2b":{"subkey3":"value3"}}},"key2":"value2","run_list":["cookbook::recipe"]}`, - }, - }, - - "Map Attributes": { + "Attributes": { Config: testConfig(t, map[string]interface{}{ "attributes": []map[string]interface{}{ map[string]interface{}{ @@ -221,6 +196,31 @@ func TestResourceProvider_windowsCreateConfigFiles(t *testing.T) { `"subkey2b":{"subkey3":"value3"}}},"key2":"value2","run_list":["cookbook::recipe"]}`, }, }, + + "Attributes JSON": { + Config: testConfig(t, map[string]interface{}{ + "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", + "server_url": "https://chef.local", + "validation_client_name": "validator", + "validation_key_path": "test-fixtures/validator.pem", + }), + + Commands: map[string]bool{ + fmt.Sprintf("cmd /c if not exist %q mkdir %q", windowsConfDir, windowsConfDir): true, + }, + + Uploads: map[string]string{ + windowsConfDir + "/client.rb": defaultWindowsClientConf, + windowsConfDir + "/encrypted_data_bag_secret": "SECRET-KEY-FILE", + windowsConfDir + "/validation.pem": "VALIDATOR-PEM-FILE", + windowsConfDir + "/first-boot.json": `{"key1":{"subkey1":{"subkey2a":["val1","val2","val3"],` + + `"subkey2b":{"subkey3":"value3"}}},"key2":"value2","run_list":["cookbook::recipe"]}`, + }, + }, } r := new(ResourceProvisioner) 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 = <