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

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

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.

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.

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.

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

'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

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

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.



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.



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

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

Instance Method Details

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



163
164
165
# File 'lib/puppet/pops/lookup/lookup_adapter.rb', line 163

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.



174
175
176
# File 'lib/puppet/pops/lookup/lookup_adapter.rb', line 174

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.



157
158
159
160
# File 'lib/puppet/pops/lookup/lookup_adapter.rb', line 157

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.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 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_lookup(key, lookup_invocation, merge) }
    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.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/puppet/pops/lookup/lookup_adapter.rb', line 66

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



93
94
95
96
97
# File 'lib/puppet/pops/lookup/lookup_adapter.rb', line 93

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.



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

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



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/puppet/pops/lookup/lookup_adapter.rb', line 134

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
  return nil if options.nil?

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

  patterns = options[1]
  patterns.each_pair { |pattern, value| return value if pattern =~ rk } unless patterns.nil?
  nil
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`.



123
124
125
126
# File 'lib/puppet/pops/lookup/lookup_adapter.rb', line 123

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.



169
170
171
172
# File 'lib/puppet/pops/lookup/lookup_adapter.rb', line 169

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



180
181
182
183
# File 'lib/puppet/pops/lookup/lookup_adapter.rb', line 180

def set_global_only
  @global_only = true
  self
end