Class: Puppet::Property::KeyValue

Inherits:
Puppet::Property show all
Defined in:
lib/puppet/property/keyvalue.rb

Overview

TODO:

The node with an important message is not very clear.

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

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

Constant Summary

Constants included from Util::Docs

Util::Docs::HEADER_LEVELS

Constants included from Util

Util::ALNUM, Util::ALPHA, Util::AbsolutePathPosix, Util::AbsolutePathWindows, Util::DEFAULT_POSIX_MODE, Util::DEFAULT_WINDOWS_MODE, Util::ESCAPED, Util::HEX, Util::HttpProxy, Util::PUPPET_STACK_INSERTION_FRAME, Util::RESERVED, Util::RFC_3986_URI_REGEX, Util::UNRESERVED, Util::UNSAFE

Constants included from Util::POSIX

Util::POSIX::LOCALE_ENV_VARS, Util::POSIX::USER_ENV_VARS

Constants included from Util::SymbolicFileMode

Util::SymbolicFileMode::SetGIDBit, Util::SymbolicFileMode::SetUIDBit, Util::SymbolicFileMode::StickyBit, Util::SymbolicFileMode::SymbolicMode, Util::SymbolicFileMode::SymbolicSpecialToBit

Class Attribute Summary collapse

Attributes inherited from Puppet::Property

#noop, #shouldorig

Attributes inherited from Puppet::Parameter

#name, #parent, #resource, #sensitive

Attributes included from Util::Docs

#doc, #nodoc

Instance Method Summary collapse

Methods inherited from Puppet::Property

#call_provider, #change_to_s, #event, #event_name, idempotent, idempotent=, #idempotent?, #insync_values?, #log, #match_all?, method_added, #name, newvalue, #property_matches?, #safe_insync?, #set, #should=, #sync, #unsafe_validate, #validate_features_per_value, #value, #value=, value_name, value_option

Methods inherited from Puppet::Parameter

aliasvalue, defaultto, desc, doc, #file, #format, format_value_for_display, #initialize, initvars, isnamevar, #isnamevar?, isnamevar?, isrequired, #line, #log, #metaparam?, munge, #munge, newvalues, nodefault, #noop, #path, #pathbuilder, #provider, proxymethods, #remove, #required?, required?, sensitive, #tags, #to_s, unmunge, #unmunge, #unsafe_munge, #unsafe_unmunge, #unsafe_validate, validate, #validate, #value, #value=, #version

Methods included from Util::Docs

#desc, #dochook, #doctable, #markdown_definitionlist, #markdown_header, #nodoc?, #pad, scrub

Methods included from Util

absolute_path?, benchmark, chuser, clear_environment, create_erb, default_env, deterministic_rand, deterministic_rand_int, exit_on_fail, format_backtrace_array, format_puppetstack_frame, get_env, get_environment, logmethods, merge_environment, path_to_uri, pretty_backtrace, replace_file, resolve_stackframe, rfc2396_escape, safe_posix_fork, set_env, skip_external_facts, symbolizehash, thinmark, uri_encode, uri_query_encode, uri_to_path, uri_unescape, which, withenv, withumask

Methods included from Util::POSIX

#get_posix_field, #gid, groups_of, #idfield, #methodbyid, #methodbyname, #search_posix_field, #uid

Methods included from Util::SymbolicFileMode

#display_mode, #normalize_symbolic_mode, #symbolic_mode_to_int, #valid_symbolic_mode?

Methods included from Util::Logging

#clear_deprecation_warnings, #debug, #deprecation_warning, #format_backtrace, #format_exception, #get_deprecation_offender, #log_and_raise, #log_deprecations_to_file, #log_exception, #puppet_deprecation_warning, #send_log, setup_facter_logging!, #warn_once

Methods included from Util::Errors

#adderrorcontext, #devfail, #error_context, error_location, error_location_with_space, error_location_with_unknowns, #exceptwrap, #fail

Constructor Details

This class inherits a constructor from Puppet::Parameter

Class Attribute Details

.log_only_changed_or_new_keysObject

This is a class-level variable that child properties can override if they wish.



20
21
22
# File 'lib/puppet/property/keyvalue.rb', line 20

def log_only_changed_or_new_keys
  @log_only_changed_or_new_keys
end

Instance Method Details

#delimiterString

Returns a default delimiter of “;”

Returns:

  • (String)

    Returns a default delimiter of “;”



103
104
105
# File 'lib/puppet/property/keyvalue.rb', line 103

def delimiter
  ";"
end

#hash_to_key_value_s(hash) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/puppet/property/keyvalue.rb', line 25

def hash_to_key_value_s(hash)
  if self.class.log_only_changed_or_new_keys
    hash = hash.select { |k, _| @changed_or_new_keys.include?(k) }
  end

  hash.map { |*pair| pair.join(separator) }.join(delimiter)
end

#hashify_shouldObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/puppet/property/keyvalue.rb', line 49

def hashify_should
  # Puppet casts all should values to arrays. Thus, if the user
  # passed in a hash for our property's should value, the should_value
  # parameter will be a single element array so we just extract our value
  # directly.
  if ! @should.empty? && @should.first.is_a?(Hash)
    return @should.first
  end

  # Here, should is an array of key/value pairs.
  @should.inject({}) do |hash, key_value|
    tmp = key_value.split(separator)
    hash[tmp[0].strip.intern] = tmp[1]
    hash
  end
end

#inclusive?Boolean

Returns:



45
46
47
# File 'lib/puppet/property/keyvalue.rb', line 45

def inclusive?
  @resource[membership] == :inclusive
end

#insync?(is) ⇒ Boolean

Returns true if there is no is value, else returns if is is equal to should using == as comparison.

Returns:

  • (Boolean)

    whether the property is in sync or not.



122
123
124
125
126
# File 'lib/puppet/property/keyvalue.rb', line 122

def insync?(is)
  return true unless is

  (is == self.should)
end

#is_to_s(current_value) ⇒ Object

rubocop:disable Naming/PredicateName



37
38
39
# File 'lib/puppet/property/keyvalue.rb', line 37

def is_to_s(current_value) # rubocop:disable Naming/PredicateName
  hash_to_key_value_s(current_value)
end

#membershipObject



41
42
43
# File 'lib/puppet/property/keyvalue.rb', line 41

def membership
  :key_value_membership
end

#process_current_hash(current) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/puppet/property/keyvalue.rb', line 66

def process_current_hash(current)
  return {} if current == :absent

  # inclusive means we are managing everything so if it isn't in should, its gone
  current.each_key { |key| current[key] = nil } if inclusive?
  current
end

#retrieveHash

Retrieves the key-hash from the provider by invoking its method named the same as this property.

Returns:

  • (Hash)

    the hash from the provider, or ‘:absent`



110
111
112
113
114
115
116
117
118
# File 'lib/puppet/property/keyvalue.rb', line 110

def retrieve
  # ok, some 'convention' if the keyvalue property is named properties, provider should implement a properties method
  key_hash = provider.send(name) if provider
  if key_hash && key_hash != :absent
    return key_hash
  else
    return :absent
  end
end

#separatorString

Returns a default separator of “=”

Returns:

  • (String)

    Returns a default separator of “=”



98
99
100
# File 'lib/puppet/property/keyvalue.rb', line 98

def separator
  "="
end

#shouldObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/puppet/property/keyvalue.rb', line 74

def should
  return nil unless @should

  members = hashify_should
  current = process_current_hash(retrieve)

  # shared keys will get overwritten by members
  should_value = current.merge(members)

  # Figure out the keys that will actually change in our Puppet run.
  # This lets us reduce the verbosity of Puppet's logging for instances
  # of this class when we want to.
  #
  # NOTE: We use ||= here because we only need to compute the
  # changed_or_new_keys once (since this property will only be synced once).
  #
  @changed_or_new_keys ||= should_value.keys.select do |key|
    ! current.key?(key) || current[key] != should_value[key]
  end

  should_value
end

#should_to_s(should_value) ⇒ Object



33
34
35
# File 'lib/puppet/property/keyvalue.rb', line 33

def should_to_s(should_value)
  hash_to_key_value_s(should_value)
end