Class: Puppet::Parameter

Inherits:
Object show all
Extended by:
Util, Util::Docs
Includes:
Util, Util::Cacher, Util::Errors, Util::LogPaths, Util::Logging, Util::MethodHelper
Defined in:
lib/puppet/parameter.rb

Direct Known Subclasses

Path, Property, Type::RelationshipMetaparam

Defined Under Namespace

Classes: Path, Value, ValueCollection

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes included from Util::Docs

#doc, #nodoc

Attributes included from Util::Cacher::Expirer

#timestamp

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util::Docs

desc, dochook, doctable, nodoc?, pad, scrub

Methods included from Util

activerecord_version, benchmark, chuser, classproxy, execfail, execpipe, execute, logmethods, memory, proxy, recmkdir, secure_open, symbolize, symbolizehash, symbolizehash!, synchronize_on, thinmark, threadlock, which, withumask

Methods included from Util::POSIX

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

Methods included from Util::Cacher

extended, included

Methods included from Util::Cacher::Expirer

#dependent_data_expired?, #expire

Methods included from Util::MethodHelper

#requiredopts, #set_options, #symbolize_options

Methods included from Util::Logging

#send_log

Methods included from Util::LogPaths

#path, #source_descriptors

Methods included from Util::Errors

#adderrorcontext, #error_context, #exceptwrap

Constructor Details

#initialize(options = {}) ⇒ Parameter

Basic parameter initialization.



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

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

.defaultObject (readonly)

Returns the value of attribute default.



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

def default
  @default
end

.metaparamObject

Returns the value of attribute metaparam.



21
22
23
# File 'lib/puppet/parameter.rb', line 21

def metaparam
  @metaparam
end

.mungerObject (readonly)

Returns the value of attribute munger.



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

def munger
  @munger
end

.nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

.required_featuresObject

Returns the value of attribute required_features.



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

def required_features
  @required_features
end

.validaterObject (readonly)

Returns the value of attribute validater.



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

def validater
  @validater
end

.value_collectionObject (readonly)

Returns the value of attribute value_collection.



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

def value_collection
  @value_collection
end

Instance Attribute Details

#parentObject

LAK 2007-05-09: Keep the @parent around for backward compatibility.



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

def parent
  @parent
end

#resourceObject

Returns the value of attribute resource.



139
140
141
# File 'lib/puppet/parameter.rb', line 139

def resource
  @resource
end

Class Method Details

.aliasvalue(name, other) ⇒ Object



121
122
123
# File 'lib/puppet/parameter.rb', line 121

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

.defaultto(value = nil, &block) ⇒ Object

Define the default value for a given parameter or parameter. This means that ‘nil’ is an invalid default value. This defines the ‘default’ instance method.



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/puppet/parameter.rb', line 26

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) ⇒ Object

Store documentation for this parameter.



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

def desc(str)
  @doc = str
end

.docObject

Return a documentation string. If there are valid values, then tack them onto the string.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/puppet/parameter.rb', line 40

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

.initvarsObject



64
65
66
# File 'lib/puppet/parameter.rb', line 64

def initvars
  @value_collection = ValueCollection.new
end

.isnamevarObject

Mark whether we’re the namevar.



86
87
88
89
# File 'lib/puppet/parameter.rb', line 86

def isnamevar
  @isnamevar = true
  @required = true
end

.isnamevar?Boolean

Is this parameter the namevar? Defaults to false.

Returns:

  • (Boolean)


92
93
94
# File 'lib/puppet/parameter.rb', line 92

def isnamevar?
  @isnamevar
end

.isrequiredObject

This parameter is required.



97
98
99
# File 'lib/puppet/parameter.rb', line 97

def isrequired
  @required = true
end

.munge(&block) ⇒ Object

This is how we munge the value. Basically, this is our opportunity to convert the value from one form into another.



70
71
72
73
74
75
76
# File 'lib/puppet/parameter.rb', line 70

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) ⇒ Object

Define a new value for our parameter.



117
118
119
# File 'lib/puppet/parameter.rb', line 117

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

.nodefaultObject



55
56
57
# File 'lib/puppet/parameter.rb', line 55

def nodefault
  undef_method :default if public_method_defined? :default
end

.proxymethods(*values) ⇒ Object

Just a simple method to proxy instance methods to class methods



127
128
129
130
131
132
133
# File 'lib/puppet/parameter.rb', line 127

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

.required?Boolean

Is this parameter required? Defaults to false.

Returns:

  • (Boolean)


107
108
109
# File 'lib/puppet/parameter.rb', line 107

def required?
  @required
end

.unmunge(&block) ⇒ Object

Does the parameter supports reverse munge? This will be called when something wants to access the parameter in a canonical form different to what the storage form is.



81
82
83
# File 'lib/puppet/parameter.rb', line 81

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

.validate(&block) ⇒ Object

Verify that we got a good value



112
113
114
# File 'lib/puppet/parameter.rb', line 112

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

Instance Method Details

#devfail(msg) ⇒ Object



149
150
151
# File 'lib/puppet/parameter.rb', line 149

def devfail(msg)
  self.fail(Puppet::DevError, msg)
end

#expirerObject



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

def expirer
  resource.catalog
end

#fail(*args) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/puppet/parameter.rb', line 157

def fail(*args)
  type = nil
  if args[0].is_a?(Class)
    type = args.shift
  else
    type = Puppet::Error
  end

  error = type.new(args.join(" "))

  error.line = @resource.line if @resource and @resource.line

  error.file = @resource.file if @resource and @resource.file

  raise error
end

#log(msg) ⇒ Object



187
188
189
# File 'lib/puppet/parameter.rb', line 187

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

#metaparam?Boolean

Is this parameter a metaparam?

Returns:

  • (Boolean)


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

def metaparam?
  self.class.metaparam
end

#munge(value) ⇒ Object

A wrapper around our munging that makes sure we raise useful exceptions.



234
235
236
237
238
239
240
241
242
243
244
# File 'lib/puppet/parameter.rb', line 234

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

#nameObject

each parameter class must define the name method, and parameter instances do not change that name this implicitly means that a given object can only have one parameter instance of a given parameter class



200
201
202
# File 'lib/puppet/parameter.rb', line 200

def name
  self.class.name
end

#noopObject

for testing whether we should actually do anything



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

def noop
  @noop ||= false
  tmp = @noop || self.resource.noop || Puppet[:noop] || false
  #debug "noop is #{tmp}"
  tmp
end

#pathbuilderObject

return the full path to us, for logging and rollback; not currently used



214
215
216
217
218
219
220
# File 'lib/puppet/parameter.rb', line 214

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

#providerObject

Retrieve the resource’s provider. Some types don’t have providers, in which case we return the resource object itself.



284
285
286
# File 'lib/puppet/parameter.rb', line 284

def provider
  @resource.provider
end

#removeObject



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

def remove
  @resource = nil
end

#tagsObject

The properties need to return tags so that logs correctly collect them.



289
290
291
292
293
294
295
296
297
# File 'lib/puppet/parameter.rb', line 289

def tags
  unless defined?(@tags)
    @tags = []
    # This might not be true in testing
    @tags = @resource.tags if @resource.respond_to? :tags
    @tags << self.name.to_s
  end
  @tags
end

#to_sObject



299
300
301
# File 'lib/puppet/parameter.rb', line 299

def to_s
  name.to_s
end

#unmunge(value) ⇒ Object

no unmunge by default



229
230
231
# File 'lib/puppet/parameter.rb', line 229

def unmunge(value)
  value
end

#unsafe_munge(value) ⇒ Object

If the specified value is allowed, then munge appropriately. If the developer uses a ‘munge’ hook, this method will get overridden.



224
225
226
# File 'lib/puppet/parameter.rb', line 224

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

#unsafe_validate(value) ⇒ Object

Verify that the passed value is valid. If the developer uses a ‘validate’ hook, this method will get overridden.



248
249
250
# File 'lib/puppet/parameter.rb', line 248

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

#validate(value) ⇒ Object

A protected validation method that only ever raises useful exceptions.



253
254
255
256
257
258
259
260
261
262
263
# File 'lib/puppet/parameter.rb', line 253

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

#valueObject



269
270
271
# File 'lib/puppet/parameter.rb', line 269

def value
  unmunge(@value) unless @value.nil?
end

#value=(value) ⇒ Object

Store the value provided. 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).



276
277
278
279
280
# File 'lib/puppet/parameter.rb', line 276

def value=(value)
  validate(value)

  @value = munge(value)
end