Puppet 4 functions - argument count not working?

267 views
Skip to first unread message

Martin Alfke

unread,
Aug 31, 2015, 11:57:28 AM8/31/15
to puppet-dev
Hi,

I am playing with Puppet 4 functions.
According to Henriks blog[1] one can provide information on number of arguments for a function using arg_count inside the dispatch.

The following code will throw an error:

# modules/utils/lib/puppet/functions/resolver.rb
require 'socket'
Puppet::Functions.create_function(:resolver) do
dispatch :hostname do
arg_count 0, 0
end
def hostname()
Socket.gethostname
end
end

Error: Evaluation Error: Error while evaluating a Function Call, undefined method `arg_count' for #<Puppet::Functions::DispatcherBuilder:0x00000004cbd280> at /etc/puppetlabs/code/environments/production/modules/functions/manifests/init.pp:2:15 on node puppetmaster.example.net

I had aloof on the functions.rb file in PC1 (/opt/puppetlabs/puppet/lib/ruby/vendor_ruby/puppet/functions.rb).
No arg_count function is available.

Is arg_count a feature which will be available with an upcoming version?

Any other way to provide information regarding amount of arguments?

Best,
Martin

[1] http://puppet-on-the-edge.blogspot.de/2015/01/the-puppet-4x-function-api.html

Thomas Hallgren

unread,
Sep 1, 2015, 3:59:47 AM9/1/15
to puppe...@googlegroups.com
Hi Martin,

The function API has evolved slightly since the blog entry that you're referring to was written. The arg_count was
removed since it was redundant and confusing after we introduced the 'optional_param' and 'repeated_param'. You can find
the documentation for Puppet 4.2 functions here:

https://docs.puppetlabs.com/references/4.2.latest/developer/Puppet/Functions.html

Regards,
Thomas

Martin Alfke

unread,
Sep 1, 2015, 7:27:18 AM9/1/15
to puppe...@googlegroups.com
Hi Thomas,
On 01 Sep 2015, at 09:59, Thomas Hallgren <thomas....@puppetlabs.com> wrote:

> Hi Martin,
>
> The function API has evolved slightly since the blog entry that you're referring to was written. The arg_count was removed since it was redundant and confusing after we introduced the 'optional_param' and 'repeated_param'. You can find the documentation for Puppet 4.2 functions here:
>
> https://docs.puppetlabs.com/references/4.2.latest/developer/Puppet/Functions.html

Many thanks for pointing to the new updated documentation.

My idea is to create a functions which has 3 use cases:
- no parameter: return hostname of puppet master
- ip as parameter: return DNS resolved name
- hostname/fqdn as parameter: return IP address from DNS

How to check for explizit “no parameter”?
Should I check for a dummy parameter set to type Undef?

best,
Martin
> --
> You received this message because you are subscribed to the Google Groups "Puppet Developers" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to puppet-dev+...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-dev/55E55AEF.7080008%40puppetlabs.com.
> For more options, visit https://groups.google.com/d/optout.

Thomas Hallgren

unread,
Sep 1, 2015, 8:28:41 AM9/1/15
to puppe...@googlegroups.com
On 2015-09-01 13:27, Martin Alfke wrote:
Hi Thomas,
On 01 Sep 2015, at 09:59, Thomas Hallgren <thomas....@puppetlabs.com> wrote:

Hi Martin,

The function API has evolved slightly since the blog entry that you're referring to was written. The arg_count was removed since it was redundant and confusing after we introduced the 'optional_param' and 'repeated_param'. You can find the documentation for Puppet 4.2 functions here:

https://docs.puppetlabs.com/references/4.2.latest/developer/Puppet/Functions.html
Many thanks for pointing to the new updated documentation.

My idea is to create a functions which has 3 use cases:
- no parameter: return hostname of puppet master
- ip as parameter: return DNS resolved name
- hostname/fqdn as parameter: return IP address from DNS

How to check for explizit “no parameter”?
Create two dispatch entries. One that require a parameter and one where no parameter is declared. The one without a parameter will be used when no argument is given. In this scenario, the check for ip or hostname must be done in the method targeted by the dispatch with one parameter.

Alternatively, you could use three dispatch entries. One without a parameter (as before) and then two with parameters using mutually exclusive regular expression types. One matching the IP and another matching hostname/fdqn. Something like this:

  dispatch :no_param do
  end

  dispatch :ip_param do
     param 'Regexp[/^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/]', :ip
  end

  dispatch :fqdn_param do
     param 'Regexp[/^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}$/]', fdqn
  end

  def no_param
     return puppet master hostname
  def

  def ip_param(ip)
     ...
  end

  def fqdn_param(ip)
     ...
  end

- thomas


- thomas

Thomas Hallgren

unread,
Sep 1, 2015, 8:33:45 AM9/1/15
to puppe...@googlegroups.com
A correction to my last reply. You must use Pattern, not Regexp (a Regexp is for regexp instances whereas a Pattern matches strings).

- thomas

Martin Alfke

unread,
Sep 2, 2015, 1:50:16 PM9/2/15
to puppe...@googlegroups.com
Hi Thomas,

many thanks for pointing me to the documentation with the updates.
Your proposed code is exactly what I am looking for.

I really like Puppet 4 Functions!!

Best,
Martin
> --
> You received this message because you are subscribed to the Google Groups "Puppet Developers" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to puppet-dev+...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-dev/55E59B25.7030604%40puppetlabs.com.
Reply all
Reply to author
Forward
0 new messages