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