Class: Puppet::Parameter
- Extended by:
- Util, Util::Docs
- Includes:
- Util, Util::Errors, Util::Logging, Util::MethodHelper
- Defined in:
- lib/puppet/parameter.rb
Overview
The Parameter class is the implementation of a resource’s attributes of parameter kind. The Parameter class is also the base class for Property, and is used to describe meta-parameters (parameters that apply to all resource types). A Parameter (in contrast to a Property) has a single value where a property has both a current and a wanted value. The Parameter class methods are used to configure and create an instance of Parameter that represents one particular attribute data type; its valid value(s), and conversion to/from internal form.
The intention is that a new parameter is created by using the DSL method Type.newparam, or Type.newmetaparam if the parameter should be applicable to all resource types.
A Parameter that does not specify and valid values (via Parameter.newvalues) accepts any value.
Direct Known Subclasses
Boolean, PackageOptions, Path, Property, Type::RelationshipMetaparam
Defined Under Namespace
Classes: Boolean, PackageOptions, Path, Value, ValueCollection
Constant Summary
Constants included from Util::Docs
Constants included from Util
Util::AbsolutePathPosix, Util::AbsolutePathWindows, Util::DEFAULT_POSIX_MODE, Util::DEFAULT_WINDOWS_MODE
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
Constants included from Util::Logging
Util::Logging::FILE_AND_LINE, Util::Logging::FILE_NO_LINE, Util::Logging::MM, Util::Logging::NO_FILE_LINE
Class Attribute Summary collapse
-
.default ⇒ Object
readonly
The default value of the parameter as determined by the Parameter.defaultto method, or nil if no default has been set.
-
.metaparam ⇒ Boolean
Flag indicating whether this parameter is a meta-parameter or not.
-
.name ⇒ Symbol
readonly
The parameter name as given when it was created.
-
.required_features ⇒ Array<Symbol>
The names of the _provider features_ required for this parameter to work.
-
.value_collection ⇒ Puppet::Parameter::ValueCollection
readonly
private
The set of valid values (or an empty set that accepts any value).
Instance Attribute Summary collapse
-
#name ⇒ Symbol
readonly
The parameter’s name as given when it was created.
-
#parent ⇒ Puppet::Parameter
private
A reference to the parameter’s parent kept for backwards compatibility.
-
#resource ⇒ Puppet::Resource
A reference to the resource this parameter is an attribute of (the _associated resource_).
Attributes included from Util::Docs
Class Method Summary collapse
-
.aliasvalue(name, other) ⇒ Object
Makes the given ‘name` an alias for the given `other` name.
-
.defaultto(value = nil, &block) ⇒ void
Defines how the ‘default` value of a parameter is computed.
-
.desc(str) ⇒ String
Sets the documentation for this parameter.
-
.doc ⇒ String
Produces a documentation string.
-
.format_value_for_display(value) ⇒ String
Produces a String with the value formatted for display to a human.
-
.initvars ⇒ void
private
Initializes the instance variables.
-
.isnamevar ⇒ void
Sets a marker indicating that this parameter is the namevar (unique identifier) of the type where the parameter is contained.
-
.isnamevar? ⇒ Boolean
Returns whether this parameter is the namevar or not.
-
.isrequired ⇒ void
Sets a marker indicating that this parameter is required.
-
.munge({|| ... }) ⇒ Object
Defines an optional method used to convert the parameter value from DSL/string form to an internal form.
-
.newvalues(*names) ⇒ void
Defines valid values for the parameter (enumeration or regular expressions).
-
.nodefault ⇒ void
Removes the ‘default` method if defined.
-
.proxymethods(*values) ⇒ Object
private
Creates instance (proxy) methods that delegates to a class method with the same name.
-
.required? ⇒ Boolean
Returns whether this parameter is required or not.
-
.unmunge({|| ... }) ⇒ Object
Defines an optional method used to convert the parameter value to DSL/string form from an internal form.
-
.validate({|| ... }) ⇒ void
Defines an optional method that is used to validate the parameter’s DSL/string value.
Instance Method Summary collapse
-
#file ⇒ Integer
Returns the result of calling the same method on the associated resource.
-
#initialize(options = {}) ⇒ Parameter
constructor
Initializes the parameter with a required resource reference and optional attribute settings.
- #isnamevar? ⇒ Object
-
#line ⇒ Integer
Returns the result of calling the same method on the associated resource.
-
#log(msg) ⇒ void
Writes the given ‘msg` to the log with the loglevel indicated by the associated resource’s ‘loglevel` parameter.
-
#metaparam? ⇒ Boolean
Returns whether this parameter is a meta-parameter or not.
-
#munge(value) ⇒ Object
Munges the value to internal form.
-
#noop ⇒ Boolean
Returns true if this parameter, the associated resource, or overall puppet mode is ‘noop`.
-
#path ⇒ String
Returns a string representation of the resource’s containment path in the catalog.
-
#pathbuilder ⇒ Object
private
Returns an array of strings representing the containment heirarchy (types/classes) that make up the path to the resource from the root of the catalog.
-
#provider ⇒ Puppet::Provider
Returns the provider of the associated resource.
-
#remove ⇒ nil
Sets the associated resource to nil.
- #required? ⇒ Object
-
#tags ⇒ Array<Symbol>
Returns an array of the associated resource’s symbolic tags (including the parameter itself).
-
#to_s ⇒ String
The name of the parameter in string form.
-
#unmunge(value) ⇒ Object
Unmunges the value by transforming it from internal form to DSL form.
-
#unsafe_munge(value) ⇒ Object
private
This is the default implementation of ‘munge` that simply produces the value (if it is valid).
-
#unsafe_validate(value) ⇒ void
private
This is the default implementation of ‘validate` that may be overridden by the DSL method Parameter.validate.
-
#validate(value) ⇒ void
Performs validation of the given value against the rules defined by this parameter.
-
#value ⇒ Object
Gets the value of this parameter after performing any specified unmunging.
-
#value=(value) ⇒ Object
Sets the given value as the value of this parameter.
-
#version ⇒ Integer
Returns the result of calling the same method on the associated resource.
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, default_env, deterministic_rand, deterministic_rand_int, exit_on_fail, get_env, get_environment, logmethods, merge_environment, path_to_uri, pretty_backtrace, replace_file, safe_posix_fork, set_env, symbolizehash, thinmark, uri_to_path, which, withenv, withumask
Methods included from Util::POSIX
#get_posix_field, #gid, #idfield, #methodbyid, #methodbyname, #search_posix_field, #uid
Methods included from Util::SymbolicFileMode
#normalize_symbolic_mode, #symbolic_mode_to_int, #valid_symbolic_mode?
Methods included from Util::MethodHelper
#requiredopts, #set_options, #symbolize_options
Methods included from Util::Logging
#clear_deprecation_warnings, #debug, #deprecation_warning, #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, #exceptwrap, #fail
Constructor Details
#initialize(options = {}) ⇒ Parameter
A parameter should be created via the DSL method Type::newparam
Initializes the parameter with a required resource reference and optional attribute settings. The option ‘:resource` must be specified or an exception is raised. Any additional options passed are used to initialize the attributes of this parameter by treating each key in the `options` hash as the name of the attribute to set, and the value as the value to set.
331 332 333 334 335 336 337 338 339 340 341 |
# File 'lib/puppet/parameter.rb', line 331 def initialize( = {}) = () if resource = [:resource] self.resource = resource .delete(:resource) else raise Puppet::DevError, "No resource set for #{self.class.name}" end () end |
Class Attribute Details
.default ⇒ Object (readonly)
Returns The default value of the parameter as determined by the defaultto method, or nil if no default has been set.
38 39 40 |
# File 'lib/puppet/parameter.rb', line 38 def default @default end |
.metaparam ⇒ Boolean
Returns Flag indicating whether this parameter is a meta-parameter or not.
67 68 69 |
# File 'lib/puppet/parameter.rb', line 67 def @metaparam end |
.name ⇒ Symbol (readonly)
Returns The parameter name as given when it was created.
34 35 36 |
# File 'lib/puppet/parameter.rb', line 34 def name @name end |
.required_features ⇒ Array<Symbol> .required_features=(*args) ⇒ Array<Symbol>
Returns The names of the _provider features_ required for this parameter to work. the returned names are always all lower case symbols.
59 60 61 |
# File 'lib/puppet/parameter.rb', line 59 def required_features @required_features end |
.value_collection ⇒ Puppet::Parameter::ValueCollection (readonly)
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.
Returns The set of valid values (or an empty set that accepts any value).
64 65 66 |
# File 'lib/puppet/parameter.rb', line 64 def value_collection @value_collection end |
Instance Attribute Details
#name ⇒ Symbol (readonly)
Since a Parameter defines the name at the class level, each Parameter class must be unique within a type’s inheritance chain.
Returns The parameter’s name as given when it was created.
365 366 367 |
# File 'lib/puppet/parameter.rb', line 365 def name self.class.name end |
#parent ⇒ Puppet::Parameter
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.
Returns A reference to the parameter’s parent kept for backwards compatibility.
296 297 298 |
# File 'lib/puppet/parameter.rb', line 296 def parent @parent end |
#resource ⇒ Puppet::Resource
Returns A reference to the resource this parameter is an attribute of (the _associated resource_).
290 291 292 |
# File 'lib/puppet/parameter.rb', line 290 def resource @resource end |
Class Method Details
.aliasvalue(name, other) ⇒ Object
Makes the given ‘name` an alias for the given `other` name. Or said differently, the valid value `other` can now also be referred to via the given `name`. Aliasing may affect how the parameter’s value is serialized/stored (it may store the ‘other` value instead of the alias).
266 267 268 |
# File 'lib/puppet/parameter.rb', line 266 def aliasvalue(name, other) @value_collection.aliasvalue(name, other) end |
.defaultto(value) ⇒ void .defaultto({|| ... }) ⇒ void
This method returns an undefined value.
Defines how the ‘default` value of a parameter is computed. The computation of the parameter’s default value is defined by providing a value or a block. A default of ‘nil` can not be used.
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/puppet/parameter.rb', line 84 def defaultto(value = nil, &block) if block define_method(:default, &block) else if value.nil? raise Puppet::DevError, "Either a default value or block must be provided" end define_method(:default) do value end end end |
.desc(str) ⇒ String
Sets the documentation for this parameter.
142 143 144 |
# File 'lib/puppet/parameter.rb', line 142 def desc(str) @doc = str end |
.doc ⇒ String
Produces a documentation string. If an enumeration of _valid values_ has been defined, it is appended to the documentation for this parameter specified with the desc method.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/puppet/parameter.rb', line 102 def doc @doc ||= "" unless defined?(@addeddocvals) @doc = Puppet::Util::Docs.scrub(@doc) if vals = value_collection.doc @doc << "\n\n#{vals}" end if features = self.required_features @doc << "\n\nRequires features #{features.flatten.collect { |f| f.to_s }.join(" ")}." end @addeddocvals = true end @doc end |
.format_value_for_display(value) ⇒ String
this method does not protect against infinite structures.
Produces a String with the value formatted for display to a human. When the parameter value is a:
-
**single valued parameter value** the result is produced on the form ‘’value’‘ where value is the string form of the parameter’s value.
-
Array the list of values is enclosed in ‘[]`, and each produced value is separated by a comma.
-
Hash value is output with keys in sorted order enclosed in ‘{}` with each entry formatted on the form `’k’ => v` where ‘k` is the key in string form and v is the value of the key. Entries are comma separated.
For both Array and Hash this method is called recursively to format contained values.
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 |
# File 'lib/puppet/parameter.rb', line 543 def self.format_value_for_display(value) if value.is_a? Array formatted_values = value.collect {|v| format_value_for_display(v)}.join(', ') "[#{formatted_values}]" elsif value.is_a? Hash # Sorting the hash keys for display is largely for having stable # output to test against, but also helps when scanning for hash # keys, since they will be in ASCIIbetical order. hash = value.keys.sort {|a,b| a.to_s <=> b.to_s}.collect do |k| "'#{k}' => #{format_value_for_display(value[k])}" end.join(', ') "{#{hash}}" else "'#{value}'" end end |
.initvars ⇒ void
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.
This method returns an undefined value.
Initializes the instance variables. Clears the internal value collection (set of allowed values).
151 152 153 |
# File 'lib/puppet/parameter.rb', line 151 def initvars @value_collection = ValueCollection.new end |
.isnamevar ⇒ void
This method returns an undefined value.
Sets a marker indicating that this parameter is the namevar (unique identifier) of the type where the parameter is contained. This also makes the parameter a required value. The marker can not be unset once it has been set.
190 191 192 193 |
# File 'lib/puppet/parameter.rb', line 190 def isnamevar @isnamevar = true @required = true end |
.isnamevar? ⇒ Boolean
Returns whether this parameter is the namevar or not.
198 199 200 |
# File 'lib/puppet/parameter.rb', line 198 def isnamevar? @isnamevar end |
.isrequired ⇒ void
This method returns an undefined value.
Sets a marker indicating that this parameter is required. Once set, it is not possible to make a parameter optional.
208 209 210 |
# File 'lib/puppet/parameter.rb', line 208 def isrequired @required = true end |
.munge({|| ... }) ⇒ Object
This adds a method with the name ‘unsafe_munge` in the created parameter class. Later this method is called in a context where exceptions will be rescued and handled.
Defines an optional method used to convert the parameter value from DSL/string form to an internal form. If a munge method is not defined, the DSL/string value is used as is.
163 164 165 166 167 168 169 |
# File 'lib/puppet/parameter.rb', line 163 def munge(&block) # I need to wrap the unsafe version in begin/rescue parameterments, # but if I directly call the block then it gets bound to the # class's context, not the instance's, thus the two methods, # instead of just one. define_method(:unsafe_munge, &block) end |
.newvalues(*names) ⇒ void
Each call to this method adds to the set of valid values
This method returns an undefined value.
Defines valid values for the parameter (enumeration or regular expressions). The set of valid values for the parameter can be limited to a (mix of) literal values and regular expression patterns.
255 256 257 |
# File 'lib/puppet/parameter.rb', line 255 def newvalues(*names) @value_collection.newvalues(*names) end |
.nodefault ⇒ void
This method returns an undefined value.
Removes the ‘default` method if defined. Has no effect if the default method is not defined. This method is intended to be used in a DSL scenario where a parameter inherits from a parameter with a default value that is not wanted in the derived parameter (otherwise, simply do not define a default value method).
131 132 133 |
# File 'lib/puppet/parameter.rb', line 131 def nodefault undef_method :default if public_method_defined? :default end |
.proxymethods(*values) ⇒ 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.
Creates instance (proxy) methods that delegates to a class method with the same name.
274 275 276 277 278 279 280 |
# File 'lib/puppet/parameter.rb', line 274 def self.proxymethods(*values) values.each { |val| define_method(val) do self.class.send(val) end } end |
.required? ⇒ Boolean
Returns whether this parameter is required or not. A parameter is required if a call has been made to the DSL method isrequired.
227 228 229 |
# File 'lib/puppet/parameter.rb', line 227 def required? @required end |
.unmunge({|| ... }) ⇒ Object
This adds a method with the name ‘unmunge` in the created parameter class.
Defines an optional method used to convert the parameter value to DSL/string form from an internal form. If an ‘unmunge` method is not defined, the internal form is used.
179 180 181 |
# File 'lib/puppet/parameter.rb', line 179 def unmunge(&block) define_method(:unmunge, &block) end |
.validate({|| ... }) ⇒ void
This method returns an undefined value.
Defines an optional method that is used to validate the parameter’s DSL/string value. Validation should raise appropriate exceptions, the return value of the given block is ignored. The easiest way to raise an appropriate exception is to call the method Util::Errors.fail with the message as an argument. To validate the munged value instead, just munge the value (‘munge(value)`).
242 243 244 |
# File 'lib/puppet/parameter.rb', line 242 def validate(&block) define_method(:unsafe_validate, &block) end |
Instance Method Details
#file ⇒ Integer
Returns the result of calling the same method on the associated resource.
311 312 313 |
# File 'lib/puppet/parameter.rb', line 311 def file resource.file end |
#isnamevar? ⇒ Object
287 |
# File 'lib/puppet/parameter.rb', line 287 proxymethods("required?", "isnamevar?") |
#line ⇒ Integer
Returns the result of calling the same method on the associated resource.
306 307 308 |
# File 'lib/puppet/parameter.rb', line 306 def line resource.line end |
#log(msg) ⇒ void
is loglevel a metaparameter? it is looked up with ‘resource`
This method returns an undefined value.
Writes the given ‘msg` to the log with the loglevel indicated by the associated resource’s ‘loglevel` parameter.
348 349 350 |
# File 'lib/puppet/parameter.rb', line 348 def log(msg) send_log(resource[:loglevel], msg) end |
#metaparam? ⇒ Boolean
Returns whether this parameter is a meta-parameter or not.
353 354 355 |
# File 'lib/puppet/parameter.rb', line 353 def self.class. end |
#munge(value) ⇒ Object
This method should not be overridden. Use the DSL method munge to define a munging method if required.
Munges the value to internal form. This implementation of ‘munge` provides exception handling around the specified munging of this parameter.
417 418 419 420 421 422 423 424 425 426 427 |
# File 'lib/puppet/parameter.rb', line 417 def munge(value) begin ret = unsafe_munge(value) rescue Puppet::Error => detail Puppet.debug "Reraising #{detail}" raise rescue => detail raise Puppet::DevError, "Munging failed for value #{value.inspect} in class #{self.name}: #{detail}", detail.backtrace end ret end |
#noop ⇒ Boolean
How is noop mode set for a parameter? Is this of value in DSL to inhibit a parameter?
Returns true if this parameter, the associated resource, or overall puppet mode is ‘noop`.
372 373 374 375 376 377 |
# File 'lib/puppet/parameter.rb', line 372 def noop @noop ||= false tmp = @noop || self.resource.noop || Puppet[:noop] || false #debug "noop is #{tmp}" tmp end |
#path ⇒ String
Returns a string representation of the resource’s containment path in the catalog.
301 302 303 |
# File 'lib/puppet/parameter.rb', line 301 def path @path ||= '/' + pathbuilder.join('/') end |
#pathbuilder ⇒ 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.
Returns an array of strings representing the containment heirarchy (types/classes) that make up the path to the resource from the root of the catalog. This is mostly used for logging purposes.
384 385 386 387 388 389 390 |
# File 'lib/puppet/parameter.rb', line 384 def pathbuilder if @resource return [@resource.pathbuilder, self.name] else return [self.name] end end |
#provider ⇒ Puppet::Provider
The original comment says = _“Retrieve the resource’s provider. Some types don’t have providers, in which case we return the resource object itself.”_ This does not seem to be true, the default implementation that sets this value may be Type.provider= which always gets either the name of a provider or an instance of one.
Returns the provider of the associated resource.
498 499 500 |
# File 'lib/puppet/parameter.rb', line 498 def provider @resource.provider end |
#remove ⇒ nil
Why - what is the intent/purpose of this?
Sets the associated resource to nil.
466 467 468 |
# File 'lib/puppet/parameter.rb', line 466 def remove @resource = nil end |
#required? ⇒ Object
287 |
# File 'lib/puppet/parameter.rb', line 287 proxymethods("required?", "isnamevar?") |
#tags ⇒ Array<Symbol>
The original comment says = _“The properties need to return tags so that logs correctly collect them.”_ what if anything of that is of interest to document. Should tags and their relationship to logs be described. This is a more general concept.
Returns an array of the associated resource’s symbolic tags (including the parameter itself). At a minimun, the array contains the name of the parameter. If the associated resource has tags, these tags are also included in the array.
510 511 512 513 514 515 516 517 518 |
# File 'lib/puppet/parameter.rb', line 510 def unless defined?(@tags) @tags = [] # This might not be true in testing @tags = @resource. if @resource.respond_to? :tags @tags << self.name.to_s end @tags end |
#to_s ⇒ String
Returns The name of the parameter in string form.
521 522 523 |
# File 'lib/puppet/parameter.rb', line 521 def to_s name.to_s end |
#unmunge(value) ⇒ Object
Unmunges the value by transforming it from internal form to DSL form. This is the default implementation of ‘unmunge` that simply returns the value without processing. The DSL method unmunge should be used to define an overriding method if required.
406 407 408 |
# File 'lib/puppet/parameter.rb', line 406 def unmunge(value) value end |
#unsafe_munge(value) ⇒ 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.
This is the default implementation of ‘munge` that simply produces the value (if it is valid). The DSL method munge should be used to define an overriding method if munging is required.
397 398 399 |
# File 'lib/puppet/parameter.rb', line 397 def unsafe_munge(value) self.class.value_collection.munge(value) end |
#unsafe_validate(value) ⇒ void
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.
This method returns an undefined value.
This is the default implementation of ‘validate` that may be overridden by the DSL method validate. If no valid values have been defined, the given value is accepted, else it is validated against the literal values (enumerator) and/or patterns defined by calling newvalues.
438 439 440 |
# File 'lib/puppet/parameter.rb', line 438 def unsafe_validate(value) self.class.value_collection.validate(value) end |
#validate(value) ⇒ void
Better description of when the various exceptions are raised.ArgumentError is rescued and changed into Puppet::Error.
This method returns an undefined value.
Performs validation of the given value against the rules defined by this parameter. A protected validation method that only ever raises useful exceptions.
450 451 452 453 454 455 456 457 458 459 460 |
# File 'lib/puppet/parameter.rb', line 450 def validate(value) begin unsafe_validate(value) rescue ArgumentError => detail self.fail Puppet::Error, detail.to_s, detail rescue Puppet::Error, TypeError raise rescue => detail raise Puppet::DevError, "Validate method failed for class #{self.name}: #{detail}", detail.backtrace end end |
#value ⇒ Object
Returns Gets the value of this parameter after performing any specified unmunging.
471 472 473 |
# File 'lib/puppet/parameter.rb', line 471 def value unmunge(@value) unless @value.nil? end |
#value=(value) ⇒ Object
This original comment _“All of the checking should possibly be late-binding (e.g., users might not exist when the value is assigned but might when it is asked for).”_ does not seem to be correct, the implementation calls both validate and munge on the given value, so no late binding.
Sets the given value as the value of this parameter. The given value is validated and then munged (if munging has been specified). The result is store as the value of this arameter.
486 487 488 489 490 |
# File 'lib/puppet/parameter.rb', line 486 def value=(value) validate(value) @value = munge(value) end |
#version ⇒ Integer
Returns the result of calling the same method on the associated resource.
316 317 318 |
# File 'lib/puppet/parameter.rb', line 316 def version resource.version end |