diff options
author | danielsdeleo <dan@opscode.com> | 2012-10-25 14:17:59 -0700 |
---|---|---|
committer | danielsdeleo <dan@opscode.com> | 2012-11-02 09:26:30 -0700 |
commit | 9aebc51a4e836bc7f993dca16dd5f926570ec86e (patch) | |
tree | 430270b02a10a6806914d80ff89bd46cccd4b9a3 /lib/chef | |
parent | 7d109ade7745863616cc3f4b4222592adcbb7539 (diff) | |
download | chef-9aebc51a4e836bc7f993dca16dd5f926570ec86e.tar.gz |
[CHEF-2936] ensure all default and override attrs go in node json
default and override attributes are now split into components, so all
the components need to be merged together to generate the serialized
representation of a node. Unfortunately, this means that serializing a
node is now lossy because the information about which component default
and override attributes belong to is lost. In practice, this is not a
major issue, since automatic, override, and default attributes are
cleared by chef for each run.
Diffstat (limited to 'lib/chef')
-rw-r--r-- | lib/chef/node.rb | 14 | ||||
-rw-r--r-- | lib/chef/node/attribute.rb | 37 |
2 files changed, 46 insertions, 5 deletions
diff --git a/lib/chef/node.rb b/lib/chef/node.rb index c664a344c5..7f0a7bda1e 100644 --- a/lib/chef/node.rb +++ b/lib/chef/node.rb @@ -355,14 +355,18 @@ class Chef display["chef_environment"] = chef_environment display["automatic"] = automatic_attrs display["normal"] = normal_attrs - display["default"] = default_attrs - display["override"] = override_attrs + display["default"] = attributes.combined_default + display["override"] = attributes.combined_override display["run_list"] = run_list.run_list display end # Serialize this object as a hash def to_json(*a) + for_json.to_json(*a) + end + + def for_json result = { "name" => name, "chef_environment" => chef_environment, @@ -370,12 +374,12 @@ class Chef "automatic" => attributes.automatic, "normal" => attributes.normal, "chef_type" => "node", - "default" => attributes.default, - "override" => attributes.override, + "default" => attributes.combined_default, + "override" => attributes.combined_override, #Render correctly for run_list items so malformed json does not result "run_list" => run_list.run_list.map { |item| item.to_s } } - result.to_json(*a) + result end def update_from!(o) diff --git a/lib/chef/node/attribute.rb b/lib/chef/node/attribute.rb index c20abf532e..854a6fe971 100644 --- a/lib/chef/node/attribute.rb +++ b/lib/chef/node/attribute.rb @@ -49,6 +49,19 @@ class Chef :@automatic ].freeze + DEFAULT_COMPONENTS = [ + :@default, + :@env_default, + :@role_default + ] + + + OVERRIDE_COMPONENTS = [ + :@override, + :@role_override, + :@env_override + ] + attr_reader :serial_number [:all?, @@ -173,6 +186,8 @@ class Chef @automatic = VividMash.new(self, automatic) @merged_attributes = nil + @combined_override = nil + @combined_default = nil end # Enables or disables `||=`-like attribute setting. See, e.g., Node#set_unless @@ -187,6 +202,8 @@ class Chef def reset_cache @serial_number += 1 @merged_attributes = nil + @combined_default = nil + @combined_override = nil end alias :reset :reset_cache @@ -248,6 +265,26 @@ class Chef end end + def combined_override + @combined_override ||= begin + resolved_attrs = OVERRIDE_COMPONENTS.inject(Mash.new) do |merged, component_ivar| + component_value = instance_variable_get(component_ivar) + Chef::Mixin::DeepMerge.merge(merged, component_value) + end + immutablize(self, resolved_attrs) + end + end + + def combined_default + @combined_default ||= begin + resolved_attrs = DEFAULT_COMPONENTS.inject(Mash.new) do |merged, component_ivar| + component_value = instance_variable_get(component_ivar) + Chef::Mixin::DeepMerge.merge(merged, component_value) + end + immutablize(self, resolved_attrs) + end + end + def [](key) merged_attributes[key] end |