class Puppet::Pops::Lookup::HieraConfigV3

@api private

Constants

DEFAULT_CONFIG_HASH
KEY_BACKENDS
KEY_DEEP_MERGE_OPTIONS
KEY_MERGE_BEHAVIOR

Public Class Methods

config_type() click to toggle source
    # File lib/puppet/pops/lookup/hiera_config.rb
354 def self.config_type
355   return @@CONFIG_TYPE if class_variable_defined?(:@@CONFIG_TYPE)
356   tf = Types::TypeFactory
357   nes_t = Types::PStringType::NON_EMPTY
358 
359   # This is a hash, not a type. Contained backends are added prior to validation
360   @@CONFIG_TYPE = {
361     tf.optional(KEY_VERSION) => tf.range(3,3),
362     tf.optional(KEY_BACKENDS) => tf.variant(nes_t, tf.array_of(nes_t)),
363     tf.optional(KEY_LOGGER) => nes_t,
364     tf.optional(KEY_MERGE_BEHAVIOR) => tf.enum('deep', 'deeper', 'native'),
365     tf.optional(KEY_DEEP_MERGE_OPTIONS) => tf.hash_kv(nes_t, tf.variant(tf.string, tf.boolean)),
366     tf.optional(KEY_HIERARCHY) => tf.variant(nes_t, tf.array_of(nes_t))
367   }
368 end

Public Instance Methods

create_configured_data_providers(lookup_invocation, parent_data_provider, _) click to toggle source
    # File lib/puppet/pops/lookup/hiera_config.rb
370 def create_configured_data_providers(lookup_invocation, parent_data_provider, _)
371   scope = lookup_invocation.scope
372   unless scope.is_a?(Hiera::Scope)
373     lookup_invocation = Invocation.new(
374       Hiera::Scope.new(scope),
375       lookup_invocation.override_values,
376       lookup_invocation.default_values,
377       lookup_invocation.explainer)
378   end
379 
380   default_datadir = File.join(Puppet.settings[:codedir], 'environments', '%{::environment}', 'hieradata')
381   data_providers = {}
382 
383   [@config[KEY_BACKENDS]].flatten.each do |backend|
384     if data_providers.include?(backend)
385       first_line = find_line_matching(/[^\w]#{backend}(?:[^\w]|$)/)
386       line = find_line_matching(/[^\w]#{backend}(?:[^\w]|$)/, first_line + 1) if first_line
387       unless line
388         line = first_line
389         first_line = nil
390       end
391       fail(Issues::HIERA_BACKEND_MULTIPLY_DEFINED, { :name => backend, :first_line => first_line }, line)
392     end
393     original_paths = [@config[KEY_HIERARCHY]].flatten
394     backend_config = @config[backend]
395     if backend_config.nil?
396       backend_config = EMPTY_HASH
397     else
398       backend_config = interpolate(backend_config, lookup_invocation, false)
399     end
400     datadir = Pathname(backend_config[KEY_DATADIR] || interpolate(default_datadir, lookup_invocation, false))
401     ext = backend_config[KEY_EXTENSION]
402     if ext.nil?
403       ext = backend == 'hocon' ? '.conf' : ".#{backend}"
404     else
405       ext = ".#{ext}"
406     end
407     paths = resolve_paths(datadir, original_paths, lookup_invocation, @config_path.nil?, ext)
408     data_providers[backend] = case
409     when backend == 'json', backend == 'yaml'
410       create_data_provider(backend, parent_data_provider, KEY_V3_DATA_HASH, "#{backend}_data", { KEY_DATADIR => datadir }, paths)
411     when backend == 'hocon' && Puppet.features.hocon?
412       create_data_provider(backend, parent_data_provider, KEY_V3_DATA_HASH, 'hocon_data', { KEY_DATADIR => datadir }, paths)
413     when backend == 'eyaml' && Puppet.features.hiera_eyaml?
414       create_data_provider(backend, parent_data_provider, KEY_V3_LOOKUP_KEY, 'eyaml_lookup_key', backend_config.merge(KEY_DATADIR => datadir), paths)
415     else
416       create_hiera3_backend_provider(backend, backend, parent_data_provider, datadir, paths, @loaded_config)
417     end
418   end
419   data_providers.values
420 end
merge_strategy() click to toggle source
    # File lib/puppet/pops/lookup/hiera_config.rb
450 def merge_strategy
451   @merge_strategy ||= create_merge_strategy
452 end
validate_config(config, owner) click to toggle source
    # File lib/puppet/pops/lookup/hiera_config.rb
428 def validate_config(config, owner)
429   unless Puppet[:strict] == :off
430     Puppet.warn_once('deprecations', 'hiera.yaml',
431       _("%{config_path}: Use of 'hiera.yaml' version 3 is deprecated. It should be converted to version 5") % { config_path: @config_path }, config_path.to_s)
432   end
433   config[KEY_VERSION] ||= 3
434   config[KEY_BACKENDS] ||= DEFAULT_CONFIG_HASH[KEY_BACKENDS]
435   config[KEY_HIERARCHY] ||= DEFAULT_CONFIG_HASH[KEY_HIERARCHY]
436   config[KEY_MERGE_BEHAVIOR] ||= DEFAULT_CONFIG_HASH[KEY_MERGE_BEHAVIOR]
437   config[KEY_DEEP_MERGE_OPTIONS] ||= {}
438 
439   backends = [ config[KEY_BACKENDS] ].flatten
440 
441   # Create the final struct used for validation (backends are included as keys to arbitrary configs in the form of a hash)
442   tf = Types::TypeFactory
443   backend_elements = {}
444   backends.each { |backend| backend_elements[tf.optional(backend)] = tf.hash_kv(Types::PStringType::NON_EMPTY, tf.any) }
445   v3_struct = tf.struct(self.class.config_type.merge(backend_elements))
446 
447   Types::TypeAsserter.assert_instance_of(["The Lookup Configuration at '%s'", @config_path], v3_struct, config)
448 end
version() click to toggle source
    # File lib/puppet/pops/lookup/hiera_config.rb
454 def version
455   3
456 end

Private Instance Methods

create_merge_strategy() click to toggle source
    # File lib/puppet/pops/lookup/hiera_config.rb
460 def create_merge_strategy
461   key = @config[KEY_MERGE_BEHAVIOR]
462   case key
463   when nil, 'native'
464     MergeStrategy.strategy(nil)
465   when 'array'
466     MergeStrategy.strategy(:unique)
467   when 'deep', 'deeper'
468     merge = { 'strategy' => key == 'deep' ? 'reverse_deep' : 'unconstrained_deep' }
469     dm_options = @config[KEY_DEEP_MERGE_OPTIONS]
470     merge.merge!(dm_options) if dm_options
471     MergeStrategy.strategy(merge)
472   end
473 end