Israel Calvete
2013-Nov-27 16:33 UTC
[Puppet Users] Custom Functions and hash as parameter
Hi all, I try write a custom function My code: * $gluster = {* * ''10.241.5.6'' => ''/data/gv0/brick1'',* * ''10.241.5.7'' => ''/data/gv0/brick1'',* * }* * $a = glusterFunctions($gluster)* * notify{$a:}* My simple custom function: *require ''rubygems''* *module Puppet::Parser::Functions* *class GlusterFunctions* * def initialize(gluster)* * @gluster = gluster* * end* * def formatBricks()* * r = ''''* * @gluster.each do |k,v|* * r += "#{k}:#{v} "* * end* * return r[0..-2]* * end* *end* * newfunction(:glusterFunctions, :type => :rvalue) do |args|* * gluster = args # with one argument args isn''t an array* * g = GlusterFunctions.new(gluster)* * return g.formatBricks()* * end* *end* The notify: *notice: 10.241.5.7/data/gv0/brick110.241.5.6/data/gv0/brick1:* It''s seems like if hash paremeter is converted in a string Why? Any solutions? Thanks -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/83967dbd-47b6-4c7d-8c19-314c053322f0%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Henrik Lindberg
2013-Nov-27 22:13 UTC
Re: [Puppet Users] Custom Functions and hash as parameter
On 2013-27-11 17:33, Israel Calvete wrote:> Hi all, > > I try write a custom function > > My code: > > / $gluster = {/ > / ''10.241.5.6'' => ''/data/gv0/brick1'',/ > / ''10.241.5.7'' => ''/data/gv0/brick1'',/ > / }/ > / > / > / $a = glusterFunctions($gluster)/ > / > / > / notify{$a:}/ > > > My simple custom function: > > /require ''rubygems''/ > / > / > /module Puppet::Parser::Functions/ > / > / > /class GlusterFunctions/ > / > / > / def initialize(gluster)/ > / @gluster = gluster/ > / end/ > / > / > / def formatBricks()/ > / r = ''''/ > / @gluster.each do |k,v|/ > / r += "#{k}:#{v} "/ > / end/ > / return r[0..-2]/ > / end/ > /end/ > / > / > / newfunction(:glusterFunctions, :type => :rvalue) do |args|/ > / gluster = args *# with one argument args isn''t an arra*y/ > / > / > / g = GlusterFunctions.new(gluster)/ > / return g.formatBricks()/ > / end/ > /end/ > > The notify: > > /notice: 10.241.5.7/data/gv0/brick110.241.5.6/data/gv0/brick1:/ > > It''s seems like if hash paremeter is converted in a string > > Why? Any solutions?All functions get their arguments as an array. The first argument passed is found in args[0], the second in args[1], etc. You probably want to do this: gluster = args[0] unless gluster.is_a? Hash raise ArgumentError, "glusterFunctions(): first arg must be a hash" end Regards - henrik -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/l75qoo%24aqn%241%40ger.gmane.org. For more options, visit https://groups.google.com/groups/opt_out.
Israel Calvete
2013-Dec-01 15:44 UTC
Re: [Puppet Users] Custom Functions and hash as parameter
On Wednesday, November 27, 2013 11:13:52 PM UTC+1, Henrik Lindberg wrote:> > On 2013-27-11 17:33, Israel Calvete wrote: > > Hi all, > > > > I try write a custom function > > > > My code: > > > > / $gluster = {/ > > / ''10.241.5.6'' => ''/data/gv0/brick1'',/ > > / ''10.241.5.7'' => ''/data/gv0/brick1'',/ > > / }/ > > / > > / > > / $a = glusterFunctions($gluster)/ > > / > > / > > / notify{$a:}/ > > > > > > My simple custom function: > > > > /require ''rubygems''/ > > / > > / > > /module Puppet::Parser::Functions/ > > / > > / > > /class GlusterFunctions/ > > / > > / > > / def initialize(gluster)/ > > / @gluster = gluster/ > > / end/ > > / > > / > > / def formatBricks()/ > > / r = ''''/ > > / @gluster.each do |k,v|/ > > / r += "#{k}:#{v} "/ > > / end/ > > / return r[0..-2]/ > > / end/ > > /end/ > > / > > / > > / newfunction(:glusterFunctions, :type => :rvalue) do |args|/ > > / gluster = args *# with one argument args isn''t an arra*y/ > > / > > / > > / g = GlusterFunctions.new(gluster)/ > > / return g.formatBricks()/ > > / end/ > > /end/ > > > > The notify: > > > > /notice: 10.241.5.7/data/gv0/brick110.241.5.6/data/gv0/brick1:/ > > > > It''s seems like if hash paremeter is converted in a string > > > > Why? Any solutions? > > All functions get their arguments as an array. The first argument passed > is found in args[0], the second in args[1], etc. > > You probably want to do this: > > gluster = args[0] > unless gluster.is_a? Hash > raise ArgumentError, "glusterFunctions(): first arg must be a hash" > end > > > Regards > - henrik > >Somehow, the code was being cached in my pool of puppet masters. In my tests, with only one argument, this is valid gluster = args Thanks. -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/c598f02a-9eec-438b-93c2-149cd17d1b15%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Felix Frank
2013-Dec-01 22:39 UTC
Re: [Puppet Users] Custom Functions and hash as parameter
Hi, I don''t fully understand how the problems you mention are connected, but the observed "caching" behavior may be due to https://projects.puppetlabs.com/issues/17210. HTH, Felix On 12/01/2013 04:44 PM, Israel Calvete wrote:> Somehow, the code was being cached in my pool of puppet masters. > > In my tests, with only one argument, this is valid > > gluster = args-- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/529BBA96.8000806%40Alumni.TU-Berlin.de. For more options, visit https://groups.google.com/groups/opt_out.