Class: Puppet::DataProviders::LookupAdapter Private

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

Overview

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

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

Constant Summary collapse

LOOKUP_OPTIONS =

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

Puppet::Pops::Lookup::LOOKUP_OPTIONS
LOOKUP_OPTIONS_PREFIX =

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

LOOKUP_OPTIONS + '.'
HASH =

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

'hash'.freeze
MERGE =

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

'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(compiler) ⇒ LookupAdapter

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.

Returns a new instance of LookupAdapter.



18
19
20
21
22
# File 'lib/puppet/data_providers/lookup_adapter.rb', line 18

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

Class Method Details

.create_adapter(compiler) ⇒ 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.



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

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

Instance Method Details

#lookup(key, lookup_invocation, merge) ⇒ 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.

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.



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
73
74
# File 'lib/puppet/data_providers/lookup_adapter.rb', line 35

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.



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

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 => environment, :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.



94
95
96
# File 'lib/puppet/data_providers/lookup_adapter.rb', line 94

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.



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

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 environment.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, ...

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.

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



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

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, ...

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.

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



120
121
122
123
# File 'lib/puppet/data_providers/lookup_adapter.rb', line 120

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