Class: Puppet::Pops::Lookup::LookupAdapter Private

Inherits:
DataAdapter show all
Defined in:
lib/puppet/pops/lookup/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.

API:

  • private

Constant Summary collapse

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.

API:

  • private

LOOKUP_OPTIONS + '.'
LOOKUP_OPTIONS_PATTERN_START =

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.

API:

  • private

'^'.freeze
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.

API:

  • private

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

API:

  • private

'merge'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from DataAdapter

#[], #[]=, #include?

Methods inherited from Adaptable::Adapter

adapt, adapt_new, associate_adapter, clear, get, instance_var_name, self_attr_name, type_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.

API:

  • private



24
25
26
27
28
# File 'lib/puppet/pops/lookup/lookup_adapter.rb', line 24

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.

API:

  • private



20
21
22
# File 'lib/puppet/pops/lookup/lookup_adapter.rb', line 20

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

Instance Method Details

#extract_lookup_options_for_key(key, options) ⇒ 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.

API:

  • private



189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/puppet/pops/lookup/lookup_adapter.rb', line 189

def extract_lookup_options_for_key(key, options)
  return nil if options.nil?

  rk = key.root_key
  key_opts = options[0]
  unless key_opts.nil?
    key_opt = key_opts[rk]
    return key_opt unless key_opt.nil?
  end

  patterns = options[1]
  patterns.each_pair { |pattern, value| return value if pattern =~ rk } unless patterns.nil?
  nil
end

#global_hiera_config_pathPathname

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 the full path of the hiera.yaml config file.

Returns:

  • the full path of the hiera.yaml config file

API:

  • private



212
213
214
# File 'lib/puppet/pops/lookup/lookup_adapter.rb', line 212

def global_hiera_config_path
  @global_hiera_config_path ||= Pathname.new(Puppet.settings[:hiera_config])
end

#global_only?Boolean

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:

API:

  • private



223
224
225
# File 'lib/puppet/pops/lookup/lookup_adapter.rb', line 223

def global_only?
  instance_variable_defined?(:@global_only) ? @global_only : false
end

#has_environment_data_provider?(lookup_invocation) ⇒ Boolean

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 true if an environment data provider version 5 is configured.

Parameters:

  • the lookup invocation

Returns:

  • true if an environment data provider version 5 is configured

API:

  • private



206
207
208
209
# File 'lib/puppet/pops/lookup/lookup_adapter.rb', line 206

def has_environment_data_provider?(lookup_invocation)
  ep = env_provider(lookup_invocation)
  ep.nil? ? false : ep.config(lookup_invocation).version >= 5
end

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

Parameters:

  • The key to lookup

  • the lookup invocation

  • Merge strategy, merge strategy name, strategy and options hash, or nil (implies “first found”)

Returns:

  • the found object

API:

  • private



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
# File 'lib/puppet/pops/lookup/lookup_adapter.rb', line 41

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

  key = LookupKey.new(key)
  lookup_invocation.lookup(key, key.module_name) do
    merge_explained = false
    if lookup_invocation.only_explain_options?
      catch(:no_such_key) { do_lookup(LookupKey::LOOKUP_OPTIONS, lookup_invocation, HASH) }
      nil
    else
      if merge.nil?
        # Used cached lookup_options
        merge = lookup_merge_options(key, lookup_invocation)
        lookup_invocation.report_merge_source(LOOKUP_OPTIONS) unless merge.nil?
      end
      lookup_invocation.with(:data, key.to_s) do
        catch(:no_such_key) { return do_lookup(key, lookup_invocation, merge) }
        throw :no_such_key if lookup_invocation.global_only?
        key.dig(lookup_invocation, lookup_default_in_module(key, lookup_invocation))
      end
    end
  end
end

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

API:

  • private



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/puppet/pops/lookup/lookup_adapter.rb', line 122

def lookup_default_in_module(key, lookup_invocation)
  module_name = lookup_invocation.module_name

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

  provider = module_provider(lookup_invocation, module_name)
  throw :no_such_key if provider.nil? || !provider.config(lookup_invocation).has_default_hierarchy?

  lookup_invocation.with(:scope, "Searching default_hierarchy of module \"#{module_name}\"") do
    merge_strategy = nil
    if merge_strategy.nil?
      @module_default_lookup_options ||= {}
      options = @module_default_lookup_options.fetch(module_name) do |k|
        meta_invocation = Invocation.new(lookup_invocation.scope)
        meta_invocation.lookup(LookupKey::LOOKUP_OPTIONS, k) do
          opts = nil
          lookup_invocation.with(:scope, "Searching for \"#{LookupKey::LOOKUP_OPTIONS}\"") do
            catch(:no_such_key) do
            opts = compile_patterns(
              validate_lookup_options(
                provider.key_lookup_in_default(LookupKey::LOOKUP_OPTIONS, meta_invocation, MergeStrategy.strategy(HASH)), k))
            end
          end
          @module_default_lookup_options[k] = opts
        end
      end
      lookup_options = extract_lookup_options_for_key(key, options)
      merge_strategy = lookup_options[MERGE] unless lookup_options.nil?
    end

    lookup_invocation.with(:scope, "Searching for \"#{key}\"") do
      provider.key_lookup_in_default(key, lookup_invocation, merge_strategy)
    end
  end
end

#lookup_global(key, 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.

API:

  • private



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/puppet/pops/lookup/lookup_adapter.rb', line 70

def lookup_global(key, lookup_invocation, merge_strategy)
  terminus = Puppet[:data_binding_terminus]
  case terminus
  when :hiera, 'hiera'
    provider = global_provider(lookup_invocation)
    throw :no_such_key if provider.nil?
    provider.key_lookup(key, lookup_invocation, merge_strategy)
  when :none, 'none', '', nil
    # If global lookup is disabled, immediately report as not found
    lookup_invocation.report_not_found(key)
    throw :no_such_key
  else
    lookup_invocation.with(:global, terminus) do
      catch(:no_such_key) do
        return lookup_invocation.report_found(key, Puppet::DataBinding.indirection.find(key.root_key,
          {:environment => environment, :variables => lookup_invocation.scope, :merge => merge_strategy}))
      end
      lookup_invocation.report_not_found(key)
      throw :no_such_key
    end
  end
rescue Puppet::DataBinding::LookupError => detail
  raise detail unless detail.issue_code.nil?
  error = Puppet::Error.new(_("Lookup of key '%{key}' failed: %{detail}") % { key: lookup_invocation.top_key, detail: detail.message })
  error.set_backtrace(detail.backtrace)
  raise error
end

#lookup_in_environment(key, 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.

API:

  • private



98
99
100
101
102
# File 'lib/puppet/pops/lookup/lookup_adapter.rb', line 98

def lookup_in_environment(key, lookup_invocation, merge_strategy)
  provider = env_provider(lookup_invocation)
  throw :no_such_key if provider.nil?
  provider.key_lookup(key, lookup_invocation, merge_strategy)
end

#lookup_in_module(key, 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.

API:

  • private



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/puppet/pops/lookup/lookup_adapter.rb', line 104

def lookup_in_module(key, lookup_invocation, merge_strategy)
  module_name = lookup_invocation.module_name

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

  provider = module_provider(lookup_invocation, module_name)
  if provider.nil?
    if environment.module(module_name).nil?
      lookup_invocation.report_module_not_found(module_name)
    else
      lookup_invocation.report_module_provider_not_found(module_name)
    end
    throw :no_such_key
  end
  provider.key_lookup(key, lookup_invocation, merge_strategy)
end

#lookup_lookup_options(key, 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.

Parameters:

  • The key for which we want lookup options

  • the lookup invocation

Returns:

  • The found lookup options or nil

API:

  • private



176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/puppet/pops/lookup/lookup_adapter.rb', line 176

def lookup_lookup_options(key, lookup_invocation)
  module_name = key.module_name

  # Retrieve the options for the module. We use nil as a key in case we have no module
  if !@lookup_options.include?(module_name)
    options = retrieve_lookup_options(module_name, lookup_invocation, MergeStrategy.strategy(HASH))
    @lookup_options[module_name] = options
  else
    options = @lookup_options[module_name]
  end
  extract_lookup_options_for_key(key, options)
end

#lookup_merge_options(key, 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.

Parameters:

  • The key for which we want merge options

  • the lookup invocation

Returns:

  • The found merge options or nil

API:

  • private



165
166
167
168
# File 'lib/puppet/pops/lookup/lookup_adapter.rb', line 165

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

#set_global_hiera_config_path(path) ⇒ 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 self.

Parameters:

  • the absolute path name of the global hiera.yaml file.

Returns:

  • self

API:

  • private



218
219
220
221
# File 'lib/puppet/pops/lookup/lookup_adapter.rb', line 218

def set_global_hiera_config_path(path)
  @global_hiera_config_path = Pathname.new(path)
  self
end

#set_global_onlyLookupAdapter

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.

Instructs the lookup framework to only perform lookups in the global layer

Returns:

  • self

API:

  • private



229
230
231
232
# File 'lib/puppet/pops/lookup/lookup_adapter.rb', line 229

def set_global_only
  @global_only = true
  self
end