Class: Puppet::DataProviders::LookupAdapter

Inherits:
DataAdapter show all
Defined in:
lib/puppet/data_providers/lookup_adapter.rb

Overview

A LookupAdapter is a specialized DataAdapter that uses its hash to store module providers. It also remembers the environment that it is attached to and maintains a cache of _lookup options_ retrieved from its data providers.

Constant Summary collapse

LOOKUP_OPTIONS =
'lookup_options'.freeze
HASH =
'hash'.freeze
MERGE =
'merge'.freeze

Constants included from Plugins::DataProviders

Plugins::DataProviders::ENV_DATA_PROVIDERS_KEY, Plugins::DataProviders::ENV_DATA_PROVIDERS_TYPE, Plugins::DataProviders::MODULE_DATA_PROVIDERS_KEY, Plugins::DataProviders::MODULE_DATA_PROVIDERS_TYPE, Plugins::DataProviders::PATH_BASED_DATA_PROVIDER_FACTORIES_KEY, Plugins::DataProviders::PATH_BASED_DATA_PROVIDER_FACTORIES_TYPE, Plugins::DataProviders::PER_MODULE_DATA_PROVIDER_KEY, Plugins::DataProviders::PER_MODULE_DATA_PROVIDER_TYPE

Constants inherited from Pops::Adaptable::Adapter

Pops::Adaptable::Adapter::DOUBLE_COLON, Pops::Adaptable::Adapter::USCORE

Instance Attribute Summary

Attributes inherited from DataAdapter

#data

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from DataAdapter

#[], #[]=, #has_name?

Methods inherited from Pops::Adaptable::Adapter

adapt, adapt_new, associate_adapter, clear, get, instance_var_name, self_attr_name

Constructor Details

#initialize(env) ⇒ LookupAdapter

Returns a new instance of LookupAdapter.



14
15
16
17
18
# File 'lib/puppet/data_providers/lookup_adapter.rb', line 14

def initialize(env)
  super()
  @env = env
  @lookup_options = {}
end

Class Method Details

.create_adapter(env) ⇒ Object



10
11
12
# File 'lib/puppet/data_providers/lookup_adapter.rb', line 10

def self.create_adapter(env)
  new(env)
end

Instance Method Details

#lookup(key, lookup_invocation, merge) ⇒ Object

Performs a lookup using global, environment, and module data providers. Merge the result using the given merge strategy. If the merge strategy is nil, then an attempt is made to find merge options in the ‘lookup_options` hash for an entry associated with the key. If no options are found, the no merge is performed and the first found entry is returned.

Parameters:

Returns:

  • (Object)

    The found value



31
32
33
34
35
36
37
38
39
# File 'lib/puppet/data_providers/lookup_adapter.rb', line 31

def lookup(key, lookup_invocation, merge)
  merge = lookup_merge_options(key, lookup_invocation) if merge.nil?
  merge_strategy = Puppet::Pops::MergeStrategy.strategy(merge)
  lookup_invocation.with(:merge, merge_strategy) do
    result = merge_strategy.merge_lookup([:lookup_global, :lookup_in_environment, :lookup_in_module]) { |m| send(m, key, lookup_invocation, merge_strategy) }
    lookup_invocation.report_result(result)
    result
  end
end

#lookup_global(name, lookup_invocation, merge_strategy) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/puppet/data_providers/lookup_adapter.rb', line 42

def lookup_global(name, lookup_invocation, merge_strategy)
  terminus = Puppet[:data_binding_terminus]
  lookup_invocation.with(:global, terminus) do
    catch(:no_such_key) do
      return lookup_invocation.report_found(name, Puppet::DataBinding.indirection.find(name,
          { :environment => @env.to_s, :variables => lookup_invocation.scope, :merge => merge_strategy }))
    end
    lookup_invocation.report_not_found(name)
    throw :no_such_key
  end
rescue Puppet::DataBinding::LookupError => e
  raise Puppet::Error.new("Error from DataBinding '#{terminus}' while looking up '#{name}': #{e.message}", e)
end

#lookup_in_environment(name, lookup_invocation, merge_strategy) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



57
58
59
# File 'lib/puppet/data_providers/lookup_adapter.rb', line 57

def lookup_in_environment(name, lookup_invocation, merge_strategy)
  env_provider.lookup(name, lookup_invocation, merge_strategy)
end

#lookup_in_module(name, lookup_invocation, merge_strategy) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/puppet/data_providers/lookup_adapter.rb', line 62

def lookup_in_module(name, lookup_invocation, merge_strategy)
  module_name = extract_module_name(name)

  # Do not attempt to do a lookup in a module unless the name is qualified.
  throw :no_such_key if module_name.nil?

  lookup_invocation.with(:module, module_name) do
    if @env.module(module_name).nil?
      lookup_invocation.report_module_not_found
      throw :no_such_key
    end
    module_provider(module_name).lookup(name, lookup_invocation, merge_strategy)
  end
end

#lookup_lookup_options(name, lookup_invocation) ⇒ String, ...

Retrieve the lookup options that match the given ‘name`.

Parameters:

Returns:

  • (String, Hash, nil)

    The found lookup options or nil



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/puppet/data_providers/lookup_adapter.rb', line 94

def lookup_lookup_options(name, lookup_invocation)
  module_name = extract_module_name(name)

  # Retrieve the options for the module. We use nil as a key in case we have none
  options = @lookup_options[module_name]
  if options.nil? && !@lookup_options.include?(module_name)
    options = retrieve_lookup_options(module_name, lookup_invocation, Puppet::Pops::MergeStrategy.strategy(HASH))
    raise Puppet::DataBinding::LookupError.new("value of #{LOOKUP_OPTIONS} must be a hash") unless options.nil? || options.is_a?(Hash)
    @lookup_options[module_name] = options
  end
  options.nil? ? nil : options[name]
end

#lookup_merge_options(name, lookup_invocation) ⇒ String, ...

Retrieve the merge options that match the given ‘name`.

Parameters:

Returns:

  • (String, Hash, nil)

    The found merge options or nil



83
84
85
86
# File 'lib/puppet/data_providers/lookup_adapter.rb', line 83

def lookup_merge_options(name, lookup_invocation)
  lookup_options = lookup_lookup_options(name, lookup_invocation)
  lookup_options.nil? ? nil : lookup_options[MERGE]
end