summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNoah Kantrowitz <noah@coderanger.net>2017-04-04 00:19:41 -0700
committerNoah Kantrowitz <noah@coderanger.net>2017-04-04 00:19:41 -0700
commitdbb339175b445bcd4dfd6c54bababf9dd7908993 (patch)
treecb3b3f7762d0fb9916946c77d25471ce96dca694
parente77023bdce2db2522f0320237c532e6c6eaa93a2 (diff)
downloadchef-dbb339175b445bcd4dfd6c54bababf9dd7908993.tar.gz
Cope with the common case of passing node[“whatever”] as template variables.
Signed-off-by: Noah Kantrowitz <noah@coderanger.net>
-rw-r--r--lib/chef/provider/template/content.rb6
-rw-r--r--spec/functional/resource/template_spec.rb10
2 files changed, 15 insertions, 1 deletions
diff --git a/lib/chef/provider/template/content.rb b/lib/chef/provider/template/content.rb
index 76a3e6d169..b40794564a 100644
--- a/lib/chef/provider/template/content.rb
+++ b/lib/chef/provider/template/content.rb
@@ -40,8 +40,12 @@ class Chef
visitor = lambda do |obj|
case obj
when Hash
+ # If this is an Attribute object, we need to change class otherwise
+ # we get the immutable behavior. This could probably be fixed by
+ # using Hash#transform_values once we only support Ruby 2.4.
+ obj_class = obj.is_a?(Chef::Node::ImmutableMash) ? Mash : obj.class
# Avoid mutating hashes in the resource in case we're changing anything.
- obj.each_with_object(obj.class.new) do |(key, value), memo|
+ obj.each_with_object(obj_class.new) do |(key, value), memo|
memo[key] = visitor.call(value)
end
when Array
diff --git a/spec/functional/resource/template_spec.rb b/spec/functional/resource/template_spec.rb
index ad1966aa29..b9a39255f4 100644
--- a/spec/functional/resource/template_spec.rb
+++ b/spec/functional/resource/template_spec.rb
@@ -32,6 +32,7 @@ describe Chef::Resource::Template do
let(:node) do
node = Chef::Node.new
node.normal[:slappiness] = "a warm gun"
+ node.normal[:nested][:secret] = "value"
node
end
@@ -232,4 +233,13 @@ describe Chef::Resource::Template do
end
end
+ describe "when passing a node attribute mash as a template variable" do
+ it "uses the node attributes like a hash" do
+ resource.source("openldap_variable_stuff.conf.erb")
+ resource.variables(node[:nested])
+ resource.run_action(:create)
+ expect(IO.read(path)).to eq("super secret is value")
+ end
+ end
+
end