midair77
2011-Oct-13 17:01 UTC
[Puppet Users] how to check if a hash/array is empty or not in puppet .pp/module (not in template)?
Dear all, I have a parameterized class and one of the args is a hash or an array and I would like to check if they are empty or not? I tried with class test($hash, $array){ if $hash.empty? == true { } if $array.empty? == true { } } but when I just puppet parser validate init.pp I would get this error: Could not parse for environment production: Syntax error at ''.''; expected ''}'' at. Please help as this is important to know how to do this type of check. Thank you in advance. -- 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.
Jacob Helwig
2011-Oct-13 17:51 UTC
Re: [Puppet Users] how to check if a hash/array is empty or not in puppet .pp/module (not in template)?
On Thu, 13 Oct 2011 10:01:49 -0700, midair77 wrote:> > Dear all, > > I have a parameterized class and one of the args is a hash or an array > and I would like to check if they are empty or not? > > I tried with > > class test($hash, $array){ > if $hash.empty? == true > { > } > if $array.empty? == true > { > } > > } > > but when I just > puppet parser validate init.pp > > I would get this error: Could not parse for environment production: > Syntax error at ''.''; expected ''}'' at. > > Please help as this is important to know how to do this type of check. > > Thank you in advance. >The cleanest way to do this would be to write a custom rvalue function that does the Ruby check for you. You can''t call ruby directly from the puppet language, so you need to use functions or inline templates. I definitely recommend going the function route, since it''s much easier to write tests for those, and your manifests end up looking much cleaner. http://docs.puppetlabs.com/guides/custom_functions.html -- Jacob Helwig
midair77
2011-Oct-13 18:41 UTC
[Puppet Users] Re: how to check if a hash/array is empty or not in puppet .pp/module (not in template)?
Thank you very much.. I am writing a function to do that. On Oct 13, 10:51 am, Jacob Helwig <ja...@puppetlabs.com> wrote:> On Thu, 13 Oct 2011 10:01:49 -0700, midair77 wrote: > > > Dear all, > > > I have a parameterized class and one of the args is a hash or an array > > and I would like to check if they are empty or not? > > > I tried with > > > class test($hash, $array){ > > if $hash.empty? == true > > { > > } > > if $array.empty? == true > > { > > } > > > } > > > but when I just > > puppet parser validate init.pp > > > I would get this error: Could not parse for environment production: > > Syntax error at ''.''; expected ''}'' at. > > > Please help as this is important to know how to do this type of check. > > > Thank you in advance. > > The cleanest way to do this would be to write a custom rvalue function > that does the Ruby check for you. You can''t call ruby directly from the > puppet language, so you need to use functions or inline templates. I > definitely recommend going the function route, since it''s much easier to > write tests for those, and your manifests end up looking much cleaner. > > http://docs.puppetlabs.com/guides/custom_functions.html > > -- > Jacob Helwig > > signature.asc > < 1KViewDownload-- 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.
Dan White
2011-Oct-13 19:17 UTC
Re: [Puppet Users] Re: how to check if a hash/array is empty or not in puppet .pp/module (not in template)?
Please share it back with the list when you get it working. Thanks. “Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us.” Bill Waterson (Calvin & Hobbes) ----- midair77 <midair77@gmail.com> wrote:> Thank you very much.. I am writing a function to do that. > > > On Oct 13, 10:51 am, Jacob Helwig <ja...@puppetlabs.com> wrote: > > On Thu, 13 Oct 2011 10:01:49 -0700, midair77 wrote: > > > > > Dear all, > > > > > I have a parameterized class and one of the args is a hash or an array > > > and I would like to check if they are empty or not? > > > > > I tried with > > > > > class test($hash, $array){ > > > if $hash.empty? == true > > > { > > > } > > > if $array.empty? == true > > > { > > > } > > > > > } > > > > > but when I just > > > puppet parser validate init.pp > > > > > I would get this error: Could not parse for environment production: > > > Syntax error at ''.''; expected ''}'' at. > > > > > Please help as this is important to know how to do this type of check. > > > > > Thank you in advance. > > > > The cleanest way to do this would be to write a custom rvalue function > > that does the Ruby check for you. You can''t call ruby directly from the > > puppet language, so you need to use functions or inline templates. I > > definitely recommend going the function route, since it''s much easier to > > write tests for those, and your manifests end up looking much cleaner. > > > > http://docs.puppetlabs.com/guides/custom_functions.html > > > > -- > > Jacob Helwig > > > > signature.asc > > < 1KViewDownload > > -- > 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. >-- 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.
Nan Liu
2011-Oct-13 23:52 UTC
Re: [Puppet Users] Re: how to check if a hash/array is empty or not in puppet .pp/module (not in template)?
On Thu, Oct 13, 2011 at 11:41 AM, midair77 <midair77@gmail.com> wrote:> Thank you very much.. I am writing a function to do that. > > > On Oct 13, 10:51 am, Jacob Helwig <ja...@puppetlabs.com> wrote: >> On Thu, 13 Oct 2011 10:01:49 -0700, midair77 wrote: >> >> > Dear all, >> >> > I have a parameterized class and one of the args is a hash or an array >> > and I would like to check if they are empty or not? >> >> > I tried with >> >> > class test($hash, $array){ >> > if $hash.empty? == true >> > { >> > } >> > if $array.empty? == true >> > { >> > } >> >> > } >> >> > but when I just >> > puppet parser validate init.pp >> >> > I would get this error: Could not parse for environment production: >> > Syntax error at ''.''; expected ''}'' at. >> >> > Please help as this is important to know how to do this type of check. >> >> > Thank you in advance.See empty function: https://github.com/puppetlabs/puppetlabs-stdlib/tree/master/lib/puppet/parser/functions Thanks, Nan -- 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.
Krzysztof Wilczynski
2011-Oct-14 11:33 UTC
[Puppet Users] Re: how to check if a hash/array is empty or not in puppet .pp/module (not in template)?
Hello, [...]> See empty function:https://github.com/puppetlabs/puppetlabs-stdlib/tree/master/lib/puppe...There is newer version soon to be merged with stdlib: https://github.com/kwilczynski/puppet-functions/blob/master/lib/puppet/parser/functions/empty.rb KW -- 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.
Krzysztof Wilczynski
2011-Oct-14 11:35 UTC
[Puppet Users] Re: how to check if a hash/array is empty or not in puppet .pp/module (not in template)?
Hello, Nan +1 [...]> See empty function:https://github.com/puppetlabs/puppetlabs-stdlib/tree/master/lib/puppe...[...] Here is a newer version soon to be merged with stdlib: https://github.com/kwilczynski/puppet-functions/blob/master/lib/puppet/parser/functions/empty.rb KW -- 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.
Krzysztof Wilczynski
2011-Oct-14 17:49 UTC
[Puppet Users] Re: how to check if a hash/array is empty or not in puppet .pp/module (not in template)?
Hello, Apologies for double-posting. I''ve got 500 first time and wasn''t sure whether it went through. KW -- 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.