Access @resouce in custom type

Visto 36 veces
Saltar al primer mensaje no leído

ZJE

no leída,
31 jul 2012, 17:05:2831/7/12
a puppet...@googlegroups.com
Is it possible to access @resource variables inside a type?

I would like to make some decisions on parameters based on other parameters that may have already been set.

For example,
---
  newparam(:param1) do
    Puppet.debug "Found drivesperarray parameter"
    desc "parameter 1"
    validate do |value|
      if resource[:otherparam] then
        #dosomething
      else
        resource[:param1] = 0
      end
    end
    Puppet.debug "Parameter 1 is: #{@resource[:param1]}"
  end
---

But I keep getting messages like "undefined method `[]' for nil:NilClass"

Anyone have experience with this? I've tried searching around for example without much luck...

Thanks!

ZJE

no leída,
31 jul 2012, 17:20:1431/7/12
a puppet...@googlegroups.com


Actually, it turns out that setting  "resource[:param1] = 0" invokes the validate loop and the whole thing blows up eventually throws "stack level too deep" (glad that it didn't let it keep going)

Nick Lewis

no leída,
31 jul 2012, 19:54:1431/7/12
a puppet...@googlegroups.com
It sounds like what you actually want is a munge block, which is used to change the value of the parameter.

munge do |value|
  if resource[:otherparam] then
    #dosomething
  else
    0
  end
end

validate should be used only to raise an exception if the value is invalid. Puppet will call validate and then munge. Also, parameters are set in the order they're defined in the type/<type>.rb file, and validated/munged before moving on to the next parameter. So a parameter can only depend on the values of parameters that come *before* it.
 
Thanks!

ZJE

no leída,
1 ago 2012, 8:46:091/8/12
a puppet...@googlegroups.com


Hi Nick,

You're exactly right - even though I had read about munge, I completely forgot it and must've missed it a million times in the documentation.

Thanks so much!
Responder a todos
Responder al autor
Reenviar
0 mensajes nuevos