1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
require_relative '../../puppet/parameter/value'
# A collection of values and regular expressions, used for specifying allowed values
# in a given parameter.
# @note This class is considered part of the internal implementation of {Puppet::Parameter}, and
# {Puppet::Property} and the functionality provided by this class should be used via their interfaces.
# @comment This class probably have several problems when trying to use it with a combination of
# regular expressions and aliases as it finds an acceptable value holder vi "name" which may be
# a regular expression...
#
# @api private
#
class Puppet::Parameter::ValueCollection
# Aliases the given existing _other_ value with the additional given _name_.
# @return [void]
# @api private
#
def aliasvalue(name, other)
other = other.to_sym
value = match?(other)
raise Puppet::DevError, _("Cannot alias nonexistent value %{value}") % { value: other } unless value
value.alias(name)
end
# Returns a doc string (enumerating the acceptable values) for all of the values in this parameter/property.
# @return [String] a documentation string.
# @api private
#
def doc
unless defined?(@doc)
@doc = ""
unless values.empty?
@doc << "Valid values are "
@doc << @strings.collect do |value|
aliases = value.aliases
if aliases && ! aliases.empty?
"`#{value.name}` (also called `#{aliases.join(", ")}`)"
else
"`#{value.name}`"
end
end.join(", ") << ". "
end
unless regexes.empty?
@doc << "Values can match `#{regexes.join("`, `")}`."
end
end
@doc
end
# @return [Boolean] Returns whether the set of allowed values is empty or not.
# @api private
#
def empty?
@values.empty?
end
# @api private
#
def initialize
# We often look values up by name, so a hash makes more sense.
@values = {}
# However, we want to retain the ability to match values in order,
# but we always prefer directly equality (i.e., strings) over regex matches.
@regexes = []
@strings = []
end
# Checks if the given value is acceptable (matches one of the literal values or patterns) and returns
# the "matcher" that matched.
# Literal string matchers are tested first, if both a literal and a regexp match would match, the literal
# match wins.
#
# @param test_value [Object] the value to test if it complies with the configured rules
# @return [Puppet::Parameter::Value, nil] The instance of Puppet::Parameter::Value that matched the given value, or nil if there was no match.
# @api private
#
def match?(test_value)
# First look for normal values
value = @strings.find { |v| v.match?(test_value) }
return value if value
# Then look for a regex match
@regexes.find { |v| v.match?(test_value) }
end
# Munges the value if it is valid, else produces the same value.
# @param value [Object] the value to munge
# @return [Object] the munged value, or the given value
# @todo This method does not seem to do any munging. It just returns the value if it matches the
# regexp, or the (most likely Symbolic) allowed value if it matches (which is more of a replacement
# of one instance with an equal one. Is the intent that this method should be specialized?
# @api private
#
def munge(value)
return value if empty?
instance = match?(value)
if instance
if instance.regex?
return value
else
return instance.name
end
else
return value
end
end
# Defines a new valid value for a {Puppet::Property}.
# A valid value is specified as a literal (typically a Symbol), but can also be
# specified with a regexp.
#
# @param name [Symbol, Regexp] a valid literal value, or a regexp that matches a value
# @param options [Hash] a hash with options
# @option options [Symbol] :event The event that should be emitted when this value is set.
# @todo Option :event original comment says "event should be returned...", is "returned" the correct word
# to use?
# @option options [Symbol] :invalidate_refreshes True if a change on this property should invalidate and
# remove any scheduled refreshes (from notify or subscribe) targeted at the same resource. For example, if
# a change in this property takes into account any changes that a scheduled refresh would have performed,
# then the scheduled refresh would be deleted.
# @option options [Object] _any_ Any other option is treated as a call to a setter having the given
# option name (e.g. `:required_features` calls `required_features=` with the option's value as an
# argument).
# @api private
#
def newvalue(name, options = {}, &block)
call_opt = options[:call]
unless call_opt.nil?
devfail "Cannot use obsolete :call value '#{call_opt}' for property '#{self.class.name}'" unless call_opt == :none || call_opt == :instead
#TRANSLATORS ':call' is a property and should not be translated
message = _("Property option :call is deprecated and no longer used.")
message += ' ' + _("Please remove it.")
Puppet.deprecation_warning(message)
options = options.reject { |k,v| k == :call }
end
value = Puppet::Parameter::Value.new(name)
@values[value.name] = value
if value.regex?
@regexes << value
else
@strings << value
end
options.each { |opt, arg| value.send(opt.to_s + "=", arg) }
if block_given?
devfail "Cannot use :call value ':none' in combination with a block for property '#{self.class.name}'" if call_opt == :none
value.block = block
value.method ||= "set_#{value.name}" if !value.regex?
else
devfail "Cannot use :call value ':instead' without a block for property '#{self.class.name}'" if call_opt == :instead
end
value
end
# Defines one or more valid values (literal or regexp) for a parameter or property.
# @return [void]
# @dsl type
# @api private
#
def newvalues(*names)
names.each { |name| newvalue(name) }
end
# @return [Array<String>] An array of the regular expressions in string form, configured as matching valid values.
# @api private
#
def regexes
@regexes.collect { |r| r.name.inspect }
end
# Validates the given value against the set of valid literal values and regular expressions.
# @raise [ArgumentError] if the value is not accepted
# @return [void]
# @api private
#
def validate(value)
return if empty?
unless @values.detect {|name, v| v.match?(value)}
str = _("Invalid value %{value}.") % { value: value.inspect }
str += " " + _("Valid values are %{value_list}.") % { value_list: values.join(", ") } unless values.empty?
str += " " + _("Valid values match %{pattern}.") % { pattern: regexes.join(", ") } unless regexes.empty?
raise ArgumentError, str
end
end
# Returns a valid value matcher (a literal or regular expression)
# @todo This looks odd, asking for an instance that matches a symbol, or an instance that has
# a regexp. What is the intention here? Marking as api private...
#
# @return [Puppet::Parameter::Value] a valid value matcher
# @api private
#
def value(name)
@values[name]
end
# @return [Array<Symbol>] Returns a list of valid literal values.
# @see regexes
# @api private
#
def values
@strings.collect { |s| s.name }
end
end
|