Class: Puppet::Parameter

Inherits:
Object
  • Object
show all
Includes:
Util::Errors
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

#initialize(options = {}) ⇒ Parameter

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.

Options Hash (options):

  • :resource (Puppet::Resource)

    The resource this parameter holds a value for. Required.

Raises:

  • (Puppet::DevError)

    If resource is not specified in the options hash.



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

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

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

Overloads:

  • .required_featuresArray<Symbol>

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

  • .required_features=(*args) ⇒ Array<Symbol>

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

DSL:

  • type



59
60
61
# File 'lib/puppet/parameter.rb', line 59

def required_features
  @required_features
end

.value_collectionPuppet::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

#parentPuppet::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

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

DSL:

  • type



266
267
268
# File 'lib/puppet/parameter.rb', line 266

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

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

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

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

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

Raises:

  • (Puppet::DevError)

    if value is nil, and no block is given.

See Also:

  • default

DSL:

  • type



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.

See Also:

DSL:

  • type



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

def desc(str)
  @doc = str
end

.docString

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 f = self.required_features
      @doc << "\n\nRequires features #{f.flatten.collect { |f| f.to_s }.join(" ")}."
    end
    @addeddocvals = true
  end

  @doc
end

.initvars

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

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



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

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



208
209
210
# File 'lib/puppet/parameter.rb', line 208

def isrequired
  @required = true
end

.munge({|| ... }) ⇒ Object

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



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)

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.

DSL:

  • type



255
256
257
# File 'lib/puppet/parameter.rb', line 255

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

.nodefault

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



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

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



179
180
181
# File 'lib/puppet/parameter.rb', line 179

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

.validate({|| ... })

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

DSL:

  • type



242
243
244
# File 'lib/puppet/parameter.rb', line 242

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

Instance Method Details

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



348
349
350
# File 'lib/puppet/parameter.rb', line 348

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

#pathbuilderObject

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

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

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.

Raises:

  • (ArgumentError)

    if the value is not valid



438
439
440
# File 'lib/puppet/parameter.rb', line 438

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:

  • (ArgumentError, TypeError, Puppet::DevError, Puppet::Error)

    under various conditions



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