Héctor Rivas Gándara
2010-Oct-01 12:10 UTC
[Puppet Users] How to check if a parameter is defined
Fast question: How can I check wether a parameter is defined or not. For instance a function that would fail (with a parser error) if the variable is not defined: check_defined($avar) or check_defined("$avar") -- Atentamente Héctor Rivas -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
Héctor Rivas Gándara
2010-Oct-01 12:30 UTC
[Puppet Users] Re: How to check if a parameter is defined
> How can I check wether a parameter is defined or not. For instance a > function that would fail (with a parser error) if the variable is not > defined: > > check_defined($avar) or check_defined("$avar")I answer myself, one way would be: if $avar == undef { fail("''avar'' not defined") } I think that assertions would be even better: assert($avar != undef) or assert($avar) I think that add assertions would be a good idea. They could check True/False conditions, if variables are defined or not, if objects are defined... Is there any proposal about this? -- Atentamente Héctor Rivas 2010/10/1 Héctor Rivas Gándara <keymon@gmail.com>:> Fast question: > > How can I check wether a parameter is defined or not. For instance a > function that would fail (with a parser error) if the variable is not > defined: > > check_defined($avar) or check_defined("$avar") > > -- > Atentamente > Héctor Rivas >-- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
Felix Frank
2010-Oct-01 13:20 UTC
Re: [Puppet Users] Re: How to check if a parameter is defined
On 10/01/2010 02:30 PM, Héctor Rivas Gándara wrote:>> How can I check wether a parameter is defined or not. For instance a >> function that would fail (with a parser error) if the variable is not >> defined: >> >> check_defined($avar) or check_defined("$avar") > > I answer myself, one way would be: > > if $avar == undef { fail("''avar'' not defined") } > > I think that assertions would be even better: > > assert($avar != undef) > > or > assert($avar) > > > I think that add assertions would be a good idea. They could check > True/False conditions, if variables are defined or not, if objects are > defined... Is there any proposal about this?I think the problem with such things is that they are dependent on the order puppet evaluates the catalogue in, which is undefined. See http://docs.puppetlabs.com/references/stable/function.html#defined which does what you propose wrt. resources. An assert would be if !defined(Resource[...]) { fail "message" } but should not be used due to the order limitation. Regards, Felix -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
Héctor Rivas Gándara
2010-Oct-01 17:47 UTC
Re: [Puppet Users] Re: How to check if a parameter is defined
>> I think that add assertions would be a good idea. They could check >> True/False conditions, if variables are defined or not, if objects are >> defined... Is there any proposal about this? > > I think the problem with such things is that they are dependent on the > order puppet evaluates the catalogue in, which is undefined. See > > http://docs.puppetlabs.com/references/stable/function.html#defined > > which does what you propose wrt. resources. An assert would be > > if !defined(Resource[...]) { fail "message" } > but should not be used due to the order limitation.Well, I suppose that the assert function that I propose is just "syntax sugar", since it can be always expressed as an ''if'' sentence. But it could be useful to check the parameters needed in a module, for instance. -- Atentamente Héctor Rivas -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.