summaryrefslogtreecommitdiff
path: root/lib/chef/node_map.rb
diff options
context:
space:
mode:
authorJohn Keiser <john@johnkeiser.com>2015-06-08 11:05:12 -0700
committerJohn Keiser <john@johnkeiser.com>2015-06-08 11:05:12 -0700
commitd795db97bb8a79b179cd2f61355f04a856df3bd8 (patch)
tree58690703f387ce0d3d9c71f3e41f4c47f04e648c /lib/chef/node_map.rb
parent4936ddb3b2391a6cab7adf9f3883ab97a3c57272 (diff)
downloadchef-d795db97bb8a79b179cd2f61355f04a856df3bd8.tar.gz
Make resource_for_short_name look up the canonical resourcejk/automatic-automatic-name
Diffstat (limited to 'lib/chef/node_map.rb')
-rw-r--r--lib/chef/node_map.rb22
1 files changed, 18 insertions, 4 deletions
diff --git a/lib/chef/node_map.rb b/lib/chef/node_map.rb
index f07c44192f..f547018a38 100644
--- a/lib/chef/node_map.rb
+++ b/lib/chef/node_map.rb
@@ -71,12 +71,14 @@ class Chef
# @param node [Chef::Node] The Chef::Node object for the run, or `nil` to
# ignore all filters.
# @param key [Object] Key to look up
+ # @param canonical [Boolean] `true` or `false` to match canonical or
+ # non-canonical values only. `nil` to ignore canonicality. Default: `nil`
#
# @return [Object] Value
#
- def get(node, key)
+ def get(node, key, canonical: nil)
raise ArgumentError, "first argument must be a Chef::Node" unless node.is_a?(Chef::Node) || node.nil?
- list(node, key).first
+ list(node, key, canonical: canonical).first
end
#
@@ -86,14 +88,16 @@ class Chef
# @param node [Chef::Node] The Chef::Node object for the run, or `nil` to
# ignore all filters.
# @param key [Object] Key to look up
+ # @param canonical [Boolean] `true` or `false` to match canonical or
+ # non-canonical values only. `nil` to ignore canonicality. Default: `nil`
#
# @return [Object] Value
#
- def list(node, key)
+ def list(node, key, canonical: nil)
raise ArgumentError, "first argument must be a Chef::Node" unless node.is_a?(Chef::Node) || node.nil?
return [] unless @map.has_key?(key)
@map[key].select do |matcher|
- !node || (filters_match?(node, matcher[:filters]) && block_matches?(node, matcher[:block]))
+ node_matches?(node, matcher) && canonical_matches?(canonical, matcher)
end.map { |matcher| matcher[:value] }
end
@@ -183,5 +187,15 @@ class Chef
return true if block.nil?
block.call node
end
+
+ def node_matches?(node, matcher)
+ return true if !node
+ filters_match?(node, matcher[:filters]) && block_matches?(node, matcher[:block])
+ end
+
+ def canonical_matches?(canonical, matcher)
+ return true if canonical.nil?
+ !!canonical == !!matcher[:canonical]
+ end
end
end