Class: Chef::NodeMap

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/node_map.rb

Instance Method Summary collapse

Instance Method Details

#delete_canonical(key, value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Seriously, don't use this, it's nearly certain to change on you

Returns:

  • remaining


101
102
103
104
105
106
107
108
109
110
111
# File 'lib/chef/node_map.rb', line 101

def delete_canonical(key, value)
  remaining = map[key]
  if remaining
    remaining.delete_if { |matcher| matcher[:canonical] && Array(matcher[:value]) == Array(value) }
    if remaining.empty?
      map.delete(key)
      remaining = nil
    end
  end
  remaining
end

#get(node, key, canonical: nil) ⇒ Object

Get a value from the NodeMap via applying the node to the filters that were set on the key.

Parameters:

  • node (Chef::Node)

    The Chef::Node object for the run, or nil to ignore all filters.

  • key (Object)

    Key to look up

  • canonical (Boolean)

    true or false to match canonical or non-canonical values only. nil to ignore canonicality. Default: nil

Returns:

  • (Object)

    Value

Raises:

  • (ArgumentError)

73
74
75
76
# File 'lib/chef/node_map.rb', line 73

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, canonical: canonical).first
end

#list(node, key, canonical: nil) ⇒ Object

List all matches for the given node and key from the NodeMap, from most-recently added to oldest.

Parameters:

  • node (Chef::Node)

    The Chef::Node object for the run, or nil to ignore all filters.

  • key (Object)

    Key to look up

  • canonical (Boolean)

    true or false to match canonical or non-canonical values only. nil to ignore canonicality. Default: nil

Returns:

  • (Object)

    Value

Raises:

  • (ArgumentError)

90
91
92
93
94
95
96
# File 'lib/chef/node_map.rb', line 90

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_matches?(node, matcher) && canonical_matches?(canonical, matcher)
  end.map { |matcher| matcher[:value] }
end

#set(key, value, platform: nil, platform_version: nil, platform_family: nil, os: nil, canonical: nil, override: nil) {|node| ... } ⇒ NodeMap

Set a key/value pair on the map with a filter. The filter must be true when applied to the node in order to retrieve the value.

Parameters:

  • key (Object)

    Key to store

  • value (Object)

    Value associated with the key

  • filters (Hash)

    Node filter options to apply to key retrieval

Yields:

  • (node)

    Arbitrary node filter as a block which takes a node argument

Returns:

  • (NodeMap)

    Returns self for possible chaining


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/chef/node_map.rb', line 34

def set(key, value, platform: nil, platform_version: nil, platform_family: nil, os: nil, canonical: nil, override: nil, &block)
  filters = {}
  filters[:platform] = platform if platform
  filters[:platform_version] = platform_version if platform_version
  filters[:platform_family] = platform_family if platform_family
  filters[:os] = os if os
  new_matcher = { value: value, filters: filters }
  new_matcher[:block] = block if block
  new_matcher[:canonical] = canonical if canonical
  new_matcher[:override] = override if override

  # The map is sorted in order of preference already; we just need to find
  # our place in it (just before the first value with the same preference level).
  insert_at = nil
  map[key] ||= []
  map[key].each_with_index do |matcher, index|
    cmp = compare_matchers(key, new_matcher, matcher)
    insert_at ||= index if cmp && cmp <= 0
  end
  if insert_at
    map[key].insert(insert_at, new_matcher)
  else
    map[key] << new_matcher
  end
  map
end