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 101 def delimiter 102 ";" 103 end
# File lib/puppet/property/keyvalue.rb 23 def hash_to_key_value_s(hash) 24 if self.class.log_only_changed_or_new_keys 25 hash = hash.select { |k, _| @changed_or_new_keys.include?(k) } 26 end 27 28 hash.map { |*pair| pair.join(separator) }.join(delimiter) 29 end
# File lib/puppet/property/keyvalue.rb 47 def hashify_should 48 # Puppet casts all should values to arrays. Thus, if the user 49 # passed in a hash for our property's should value, the should_value 50 # parameter will be a single element array so we just extract our value 51 # directly. 52 if ! @should.empty? && @should.first.is_a?(Hash) 53 return @should.first 54 end 55 56 # Here, should is an array of key/value pairs. 57 @should.inject({}) do |hash, key_value| 58 tmp = key_value.split(separator) 59 hash[tmp[0].strip.intern] = tmp[1] 60 hash 61 end 62 end
# File lib/puppet/property/keyvalue.rb 43 def inclusive? 44 @resource[membership] == :inclusive 45 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 120 def insync?(is) 121 return true unless is 122 123 (is == self.should) 124 end
# File lib/puppet/property/keyvalue.rb 35 def is_to_s(current_value) 36 hash_to_key_value_s(current_value) 37 end
# File lib/puppet/property/keyvalue.rb 39 def membership 40 :key_value_membership 41 end
# File lib/puppet/property/keyvalue.rb 64 def process_current_hash(current) 65 return {} if current == :absent 66 67 #inclusive means we are managing everything so if it isn't in should, its gone 68 current.each_key { |key| current[key] = nil } if inclusive? 69 current 70 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 108 def retrieve 109 #ok, some 'convention' if the keyvalue property is named properties, provider should implement a properties method 110 key_hash = provider.send(name) if provider 111 if key_hash && key_hash != :absent 112 return key_hash 113 else 114 return :absent 115 end 116 end
@return [String] Returns a default separator of “=”
# File lib/puppet/property/keyvalue.rb 96 def separator 97 "=" 98 end
# File lib/puppet/property/keyvalue.rb 72 def should 73 return nil unless @should 74 75 members = hashify_should 76 current = process_current_hash(retrieve) 77 78 #shared keys will get overwritten by members 79 should_value = current.merge(members) 80 81 # Figure out the keys that will actually change in our Puppet run. 82 # This lets us reduce the verbosity of Puppet's logging for instances 83 # of this class when we want to. 84 # 85 # NOTE: We use ||= here because we only need to compute the 86 # changed_or_new_keys once (since this property will only be synced once). 87 # 88 @changed_or_new_keys ||= should_value.keys.select do |key| 89 ! current.key?(key) || current[key] != should_value[key] 90 end 91 92 should_value 93 end
# File lib/puppet/property/keyvalue.rb 31 def should_to_s(should_value) 32 hash_to_key_value_s(should_value) 33 end