Module: Puppet::DataProviders::DataFunctionSupport
- Included in:
- FunctionEnvDataProvider, FunctionModuleDataProvider
- Defined in:
- lib/puppet/data_providers/data_function_support.rb
Instance Method Summary collapse
-
#data(key, scope) {|data| ... } ⇒ Hash
Gets the data from the compiler, or initializes it from a function call if not present in the compiler.
- #initialize_data_from_function(name, key, scope) ⇒ Object
Instance Method Details
#data(key, scope) {|data| ... } ⇒ Hash
Gets the data from the compiler, or initializes it from a function call if not present in the compiler. This means, that the function providing the data is called once per compilation, and the data is cached for as long as the compiler lives (which is for one catalog production). This makes it possible to return data that is tailored for the request. The class including this module must implement ‘loader(scope)` to return the apropriate loader.
If a block is given, it will be called to validate the data hash when it is retrieved from a function call. The block must return the validated data or raise a Error to indicate that the data is invalid. The block is not called when the data is found in the compiler or in the cache.
19 20 21 22 23 |
# File 'lib/puppet/data_providers/data_function_support.rb', line 19 def data(key, scope, &block) compiler = scope.compiler adapter = Puppet::DataProviders::DataAdapter.get(compiler) || Puppet::DataProviders::DataAdapter.adapt(compiler) adapter.data[key] ||= initialize_data_from_function("#{key}::data", key, scope, &block) end |
#initialize_data_from_function(name, key, scope) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/puppet/data_providers/data_function_support.rb', line 25 def initialize_data_from_function(name, key, scope) Puppet::Util::Profiler.profile("Called #{name}", [ :functions, name ]) do loader = loader(key, scope) if loader && func = loader.load(:function, name) # function found, call without arguments, must return a Hash # TODO: Validate the function - to ensure it does not contain unwanted side effects # That can only be done if the function is a puppet function # result = func.call(scope) unless result.is_a?(Hash) raise Puppet::Error.new("Expected '#{name}' function to return a Hash, got #{result.class}") end # validate result if block given result = yield(result) if block_given? else raise Puppet::Error.new("Data from 'function' cannot find the required '#{name}' function") end result end end |