Class: Puppet::Parameter

Inherits:
Object show all
Extended by:
Util, Util::Docs
Includes:
Util, Util::Errors, Util::LogPaths, 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.

See Also:

Direct Known Subclasses

Property

Defined Under Namespace

Classes: Value, ValueCollection

Constant Summary

Class Attribute Summary (collapse)

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Util

exit_on_fail, exit_on_fail, which, which

Constructor Details

- (Parameter) initialize(options = {})

Note:

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.

Parameters:

  • options (Hash{Symbol => Object]) (defaults to: {})

    Options, where resource is required

Options Hash (options):

  • :resource (Puppet::Resource)

    The resource this parameter holds a value for. Required.

Raises:



329
330
331
332
333
334
335
336
337
338
339
# File 'lib/puppet/parameter.rb', line 329

def initialize(options = {})
  options = symbolize_options(options)
  if resource = options[:resource]
    self.resource = resource
    options.delete(:resource)
  else
    raise Puppet::DevError, "No resource set for #{self.class.name}"
  end

  set_options(options)
end

Class Attribute Details

+ (Object) munger (readonly) private

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.

TODO:

The term “munger” only appears in this location in the Puppet code base. There is munge and unmunge and they seem to work perfectly fine without this attribute declaration.

Unused?



46
47
48
# File 'lib/puppet/parameter.rb', line 46

def munger
  @munger
end

+ (Array<Symbol>) required_features  DSL + (Array<Symbol>) required_features=(*args)  DSL

The names of the provider features required for this parameter to work. the returned names are always all lower case symbols.

Overloads:

  • + (Array<Symbol>) required_features

    Returns the required provider features as an array of lower case symbols

  • + (Array<Symbol>) required_features=(*args)

    Sets the required_provider_features_ from one or more values, or array. The given arguments are flattened, and internalized.

    Parameters:

    • *args (Symbol)

      one or more names of required provider features

Returns:

  • (Array<Symbol>)

    The names of the provider features required for this parameter to work. the returned names are always all lower case symbols.

DSL:

  • type



74
75
76
# File 'lib/puppet/parameter.rb', line 74

def required_features
  @required_features
end

+ (Object) validater (readonly) private

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.

TODO:

The term “validater” only appears in this location in the Puppet code base. There is validate which seems to works fine without this attribute declaration.

Unused?



39
40
41
# File 'lib/puppet/parameter.rb', line 39

def validater
  @validater
end

+ (Puppet::Parameter::ValueCollection) value_collection (readonly) private

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.

The set of valid values (or an empty set that accepts any value).

Returns:



79
80
81
# File 'lib/puppet/parameter.rb', line 79

def value_collection
  @value_collection
end

Instance Attribute Details

- (Puppet::Parameter) parent  private

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.

A reference to the parameter’s parent kept for backwards compatibility.

Returns:

  • (Puppet::Parameter)

    A reference to the parameter’s parent kept for backwards compatibility.



304
305
306
# File 'lib/puppet/parameter.rb', line 304

def parent
  @parent
end

Class Method Details

+ (Object) aliasvalue(name, other)  DSL

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).

DSL:

  • type



274
275
276
# File 'lib/puppet/parameter.rb', line 274

def aliasvalue(name, other)
  @value_collection.aliasvalue(name, other)
end

+ defaultto(value)  DSL + defaultto({|| ... })  DSL

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.

Overloads:

  • + defaultto(value)

    Defines the default value with a literal value

    Parameters:

    • value (Object)

      the literal value to use as the default value

  • + defaultto({|| ... })

    Defines that the default value is produced by the given block. The given block should produce the default value.

Raises:

See Also:

DSL:

  • type



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/puppet/parameter.rb', line 99

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

+ (String) desc(str)  DSL

Sets the documentation for this parameter.

Parameters:

  • str (String)

    The documentation string to set

Returns:

  • (String)

    the given str parameter

See Also:

DSL:

  • type



154
155
156
# File 'lib/puppet/parameter.rb', line 154

def desc(str)
  @doc = str
end

+ (String) doc

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.

Returns:

  • (String)

    Returns a documentation string.



117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/puppet/parameter.rb', line 117

def doc
  @doc ||= ""

  unless defined?(@addeddocvals)
    @doc += value_collection.doc

    if f = self.required_features
      @doc += "  Requires features #{f.flatten.collect { |f| f.to_s }.join(" ")}."
    end
    @addeddocvals = true
  end

  @doc
end

+ initvars  private

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).



163
164
165
# File 'lib/puppet/parameter.rb', line 163

def initvars
  @value_collection = ValueCollection.new
end

+ isnamevar  DSL

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.

DSL:

  • type



202
203
204
205
# File 'lib/puppet/parameter.rb', line 202

def isnamevar
  @isnamevar = true
  @required = true
end

+ (Boolean) isnamevar?

Returns whether this parameter is the namevar or not.

Returns:

  • (Boolean)

    Returns whether this parameter is the namevar or not.



210
211
212
# File 'lib/puppet/parameter.rb', line 210

def isnamevar?
  @isnamevar
end

+ isrequired  DSL

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.

DSL:

  • type



220
221
222
# File 'lib/puppet/parameter.rb', line 220

def isrequired
  @required = true
end

+ (Object) munge({|| ... })  DSL

Note:

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.

DSL:

  • type



175
176
177
178
179
180
181
# File 'lib/puppet/parameter.rb', line 175

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)  DSL

Note:

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.

Parameters:

  • names (Symbol, Regexp)

    The set of valid literal values and/or patterns for the parameter.

DSL:

  • type



263
264
265
# File 'lib/puppet/parameter.rb', line 263

def newvalues(*names)
  @value_collection.newvalues(*names)
end

+ nodefault  DSL

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).

See Also:

DSL:

  • type



143
144
145
# File 'lib/puppet/parameter.rb', line 143

def nodefault
  undef_method :default if public_method_defined? :default
end

+ (Object) proxymethods(*values)  private

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.



282
283
284
285
286
287
288
# File 'lib/puppet/parameter.rb', line 282

def self.proxymethods(*values)
  values.each { |val|
    define_method(val) do
      self.class.send(val)
    end
  }
end

+ (Boolean) required?

Returns whether this parameter is required or not. A parameter is required if a call has been made to the DSL method isrequired.

Returns:

  • (Boolean)

    Returns whether this parameter is required or not.



239
240
241
# File 'lib/puppet/parameter.rb', line 239

def required?
  @required
end

+ (Object) unmunge({|| ... })  DSL

Note:

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.

See Also:

DSL:

  • type



191
192
193
# File 'lib/puppet/parameter.rb', line 191

def unmunge(&block)
  define_method(:unmunge, &block)
end

+ validate({|| ... })  DSL

This method returns an undefined value.

Defines an optional method that is used to validate the parameter’s value. Validation should raise appropriate exceptions, the return value of the given block is ignored.

DSL:

  • type



250
251
252
# File 'lib/puppet/parameter.rb', line 250

def validate(&block)
  define_method(:unsafe_validate, &block)
end

Instance Method Details

- (Boolean) isnamevar?

Returns whether this parameter is the namevar or not.

Returns:

  • (Boolean)

    Returns whether this parameter is the namevar or not.



295
# File 'lib/puppet/parameter.rb', line 295

proxymethods("required?", "isnamevar?")

- log(msg)

TODO:

is loglevel a metaparameter? it is looked up with resource[:loglevel]

This method returns an undefined value.

Writes the given msg to the log with the loglevel indicated by the associated resource’s loglevel parameter.



346
347
348
# File 'lib/puppet/parameter.rb', line 346

def log(msg)
  send_log(resource[:loglevel], msg)
end

- (Object) pathbuilder  private

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.

TODO:

Original comment = return the full path to us, for logging and rollback; not currently used This is difficult to figure out (if it is used or not as calls are certainly made to “pathbuilder” method is several places, not just sure if it is this implementation or not.



382
383
384
385
386
387
388
# File 'lib/puppet/parameter.rb', line 382

def pathbuilder
  if @resource
    return [@resource.pathbuilder, self.name]
  else
    return [self.name]
  end
end

- (Boolean) required?

Returns whether this parameter is required or not. A parameter is required if a call has been made to the DSL method isrequired.

Returns:

  • (Boolean)

    Returns whether this parameter is required or not.



295
# File 'lib/puppet/parameter.rb', line 295

proxymethods("required?", "isnamevar?")

- (Object) unsafe_munge(value)  private

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.



395
396
397
# File 'lib/puppet/parameter.rb', line 395

def unsafe_munge(value)
  self.class.value_collection.munge(value)
end

- unsafe_validate(value)  private

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.

Parameters:

  • value (Object)

    the value to check for validity

Raises:

  • (ArgumentError)

    if the value is not valid



436
437
438
# File 'lib/puppet/parameter.rb', line 436

def unsafe_validate(value)
  self.class.value_collection.validate(value)
end

- validate(value)

TODO:

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.

Raises:



448
449
450
451
452
453
454
455
456
457
458
# File 'lib/puppet/parameter.rb', line 448

def validate(value)
  begin
    unsafe_validate(value)
  rescue ArgumentError => detail
    fail detail.to_s
  rescue Puppet::Error, TypeError
    raise
  rescue => detail
    raise Puppet::DevError, "Validate method failed for class #{self.name}: #{detail}", detail.backtrace
  end
end