michalskalski
michalskalski

Reputation: 63

Is it possible to use custom function in custom type definition?

I write module which will define new types. Inside newproperty definition I want to use custom function (also provided in this module) which will munge passed value:

Function

#lib/puppet/parser/functions/my_custom_function.rb
module Puppet::Parser::Functions
  newfunction(:my_custom_function, :type => :rvalue) do |args|
    ...
  end
end

Type

#lib/puppet/type/new_type.rb
Puppet::Type.newtype(:new_type) do
  newparam(:name) do
    munge do |value|
      my_custom_function(value)
    end
  end
end

but I get undefined local variable or method when try use function in type like above.

I also don't have access to stdlib functions inside custom type, but these functions are available in manifest file.

Does someone can provide example how to execute custom function inside type definition especially in munge block?

Upvotes: 1

Views: 767

Answers (2)

Chris Pitman
Chris Pitman

Reputation: 13104

You need to extract the code from your custom function into a separate location and then call that shared code from both your custom function and from your type/provider. You do not need to pull the code into a separate gem to do this, it is fairly easy to keep the code local to your module.

Put your own Ruby classes in the directory lib/puppet/util/ of your module. You should then be able to require 'puppet/util/my_class' from both your custom function and your type/provider. You can see an example of how I've done this in my module jboss-puppet_admin.

Upvotes: 1

Felix Frank
Felix Frank

Reputation: 8223

Custom functions are parser functions, for use in your manifests only.

The type code is used by the agent only, which will not load parser functions while initializing resources.

You will have to duplicate your munging code. If this is not feasible, you may have to implement it in a custom Ruby library, and use that from both within your custom function and your type. The library will need to be installed on both masters and agents in this case.

Upvotes: 1

Related Questions