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, klass) ⇒ 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



150
151
152
153
154
155
156
157
158
159
160
# File 'lib/chef/node_map.rb', line 150

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

#delete_class(klass) ⇒ Hash

Remove a class from all its matchers in the node_map, will remove mappings completely if its the last matcher left

Note that this leaks the internal structure out a bit, but the main consumer of this (poise/halite) cares only about the keys in the returned Hash.



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/chef/node_map.rb', line 129

def delete_class(klass)
  raise "please use a Class type for the klass argument" unless klass.is_a?(Class)
  deleted = {}
  map.each do |key, matchers|
    deleted_matchers = []
    matchers.delete_if do |matcher|
      # because matcher[:klass] may be a string (which needs to die), coerce both to strings to compare somewhat canonically
      if matcher[:klass].to_s == klass.to_s
        deleted_matchers << matcher
        true
      end
    end
    deleted[key] = deleted_matchers unless deleted_matchers.empty?
    map.delete(key) if matchers.empty?
  end
  deleted
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.



94
95
96
97
98
99
100
# File 'lib/chef/node_map.rb', line 94

def get(node, key, canonical: nil)
  return nil unless map.has_key?(key)
  map[key].map do |matcher|
    return matcher[:klass] if node_matches?(node, matcher) && canonical_matches?(canonical, matcher)
  end
  nil
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.



114
115
116
117
118
119
# File 'lib/chef/node_map.rb', line 114

def list(node, key, canonical: nil)
  return [] unless map.has_key?(key)
  map[key].select do |matcher|
    node_matches?(node, matcher) && canonical_matches?(canonical, matcher)
  end.map { |matcher| matcher[:klass] }
end

#set(key, klass, 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.

Yields:

  • (node)

    Arbitrary node filter as a block which takes a node argument



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/chef/node_map.rb', line 53

def set(key, klass, platform: nil, platform_version: nil, platform_family: nil, os: nil, canonical: nil, override: nil, &block)
  new_matcher = { klass: klass }
  new_matcher[:platform] = platform if platform
  new_matcher[:platform_version] = platform_version if platform_version
  new_matcher[:platform_family] = platform_family if platform_family
  new_matcher[:os] = os if os
  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)
    if cmp && cmp <= 0
      insert_at = index
      break
    end
  end
  if insert_at
    map[key].insert(insert_at, new_matcher)
  else
    map[key] << new_matcher
  end
  map
end