Custom Functions and hash as parameter

164 views
Skip to first unread message

Israel Calvete

unread,
Nov 27, 2013, 11:33:51 AM11/27/13
to puppet...@googlegroups.com
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:


It's seems like if hash paremeter is converted in a string

Why? Any solutions?


Thanks

Henrik Lindberg

unread,
Nov 27, 2013, 5:13:52 PM11/27/13
to puppet...@googlegroups.com
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

Israel Calvete

unread,
Dec 1, 2013, 10:44:36 AM12/1/13
to puppet...@googlegroups.com
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.

Felix Frank

unread,
Dec 1, 2013, 5:39:18 PM12/1/13
to puppet...@googlegroups.com
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
Reply all
Reply to author
Forward
0 new messages