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 =
Puppet::Pops::Lookup::LOOKUP_OPTIONS
LOOKUP_OPTIONS_PREFIX =
LOOKUP_OPTIONS + '.'
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.



16
17
18
19
20
# File 'lib/puppet/data_providers/lookup_adapter.rb', line 16

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

Class Method Details

.create_adapter(env) ⇒ Object



12
13
14
# File 'lib/puppet/data_providers/lookup_adapter.rb', line 12

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



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/puppet/data_providers/lookup_adapter.rb', line 33

def lookup(key, lookup_invocation, merge)
  # The 'lookup_options' key is reserved and not found as normal data
  if key == LOOKUP_OPTIONS || key.start_with?(LOOKUP_OPTIONS_PREFIX)
    lookup_invocation.with(:invalid_key, LOOKUP_OPTIONS) do
      throw :no_such_key
    end
  end

  lookup_invocation.top_key ||= key
  merge_explained = false
  if lookup_invocation.explain_options?
    catch(:no_such_key) do
      module_name = extract_module_name(key) unless key == Puppet::Pops::Lookup::GLOBAL
      lookup_invocation.module_name = module_name
      if lookup_invocation.only_explain_options?
        do_lookup(LOOKUP_OPTIONS, lookup_invocation, HASH)
        return nil
      end

      # Bypass cache and do a "normal" lookup of the lookup_options
      lookup_invocation.with(:meta, LOOKUP_OPTIONS) do
        key_options = do_lookup(LOOKUP_OPTIONS, lookup_invocation, HASH)[key]
        merge = key_options[MERGE] unless key_options.nil?
        merge_explained = true
      end
    end
  elsif merge.nil?
    # Used cached lookup_options
    merge = lookup_merge_options(key, lookup_invocation)
    lookup_invocation.report_merge_source('lookup_options') unless merge.nil?
  end

  if merge_explained
    # Merge lookup is explained in detail so we need to explain the data in a section
    # on the same level to avoid confusion
    lookup_invocation.with(:data, key) { do_lookup(key, lookup_invocation, merge) }
  else
    do_lookup(key, lookup_invocation, merge)
  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.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/puppet/data_providers/lookup_adapter.rb', line 75

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, :variables => lookup_invocation.scope, :merge => merge_strategy }))
    end
    lookup_invocation.report_not_found(name)
    throw :no_such_key
  end
rescue Puppet::DataBinding::LookupError => detail
  error = Puppet::Error.new("Lookup of key '#{lookup_invocation.top_key}' failed: #{detail.message}")
  error.set_backtrace(detail.backtrace)
  raise error
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.



92
93
94
# File 'lib/puppet/data_providers/lookup_adapter.rb', line 92

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.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/puppet/data_providers/lookup_adapter.rb', line 97

def lookup_in_module(name, lookup_invocation, merge_strategy)
  module_name = lookup_invocation.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



129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/puppet/data_providers/lookup_adapter.rb', line 129

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
  if !@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
  else
    options = @lookup_options[module_name]
  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



118
119
120
121
# File 'lib/puppet/data_providers/lookup_adapter.rb', line 118

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