class Puppet::Property::KeyValue
This subclass of {Puppet::Property} manages string key value pairs. In order to use this property:
-
the should value must be an array of key-value pairs separated by the 'separator'
-
the retrieve method should return a hash with the keys as symbols
@note *IMPORTANT*: In order for this property to work there must also be a 'membership' parameter
The class that inherits from property should override that method with the symbol for the membership
@todo The node with an important message is not very clear.
Attributes
This is a class-level variable that child properties can override if they wish.
Public Instance Methods
@return [String] Returns a default delimiter of “;”
# File lib/puppet/property/keyvalue.rb 102 def delimiter 103 ";" 104 end
# File lib/puppet/property/keyvalue.rb 24 def hash_to_key_value_s(hash) 25 if self.class.log_only_changed_or_new_keys 26 hash = hash.select { |k, _| @changed_or_new_keys.include?(k) } 27 end 28 29 hash.map { |*pair| pair.join(separator) }.join(delimiter) 30 end
# File lib/puppet/property/keyvalue.rb 48 def hashify_should 49 # Puppet casts all should values to arrays. Thus, if the user 50 # passed in a hash for our property's should value, the should_value 51 # parameter will be a single element array so we just extract our value 52 # directly. 53 if ! @should.empty? && @should.first.is_a?(Hash) 54 return @should.first 55 end 56 57 # Here, should is an array of key/value pairs. 58 @should.inject({}) do |hash, key_value| 59 tmp = key_value.split(separator) 60 hash[tmp[0].strip.intern] = tmp[1] 61 hash 62 end 63 end
# File lib/puppet/property/keyvalue.rb 44 def inclusive? 45 @resource[membership] == :inclusive 46 end
Returns true if there is no is value, else returns if is is equal to should using == as comparison. @return [Boolean] whether the property is in sync or not.
# File lib/puppet/property/keyvalue.rb 121 def insync?(is) 122 return true unless is 123 124 (is == self.should) 125 end
# File lib/puppet/property/keyvalue.rb 36 def is_to_s(current_value) 37 hash_to_key_value_s(current_value) 38 end
# File lib/puppet/property/keyvalue.rb 40 def membership 41 :key_value_membership 42 end
# File lib/puppet/property/keyvalue.rb 65 def process_current_hash(current) 66 return {} if current == :absent 67 68 #inclusive means we are managing everything so if it isn't in should, its gone 69 current.each_key { |key| current[key] = nil } if inclusive? 70 current 71 end
Retrieves the key-hash from the provider by invoking its method named the same as this property. @return [Hash] the hash from the provider, or `:absent`
# File lib/puppet/property/keyvalue.rb 109 def retrieve 110 #ok, some 'convention' if the keyvalue property is named properties, provider should implement a properties method 111 key_hash = provider.send(name) if provider 112 if key_hash && key_hash != :absent 113 return key_hash 114 else 115 return :absent 116 end 117 end
@return [String] Returns a default separator of “=”
# File lib/puppet/property/keyvalue.rb 97 def separator 98 "=" 99 end
# File lib/puppet/property/keyvalue.rb 73 def should 74 return nil unless @should 75 76 members = hashify_should 77 current = process_current_hash(retrieve) 78 79 #shared keys will get overwritten by members 80 should_value = current.merge(members) 81 82 # Figure out the keys that will actually change in our Puppet run. 83 # This lets us reduce the verbosity of Puppet's logging for instances 84 # of this class when we want to. 85 # 86 # NOTE: We use ||= here because we only need to compute the 87 # changed_or_new_keys once (since this property will only be synced once). 88 # 89 @changed_or_new_keys ||= should_value.keys.select do |key| 90 ! current.key?(key) || current[key] != should_value[key] 91 end 92 93 should_value 94 end
# File lib/puppet/property/keyvalue.rb 32 def should_to_s(should_value) 33 hash_to_key_value_s(should_value) 34 end