Class: Puppet::Pops::Lookup::HieraConfigV5 Private

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

RESERVED_OPTION_KEYS =

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.

['path', 'uri'].freeze
DEFAULT_CONFIG_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.

{
  KEY_VERSION => 5,
  KEY_DEFAULTS => {
    KEY_DATADIR => 'data',
    KEY_DATA_HASH => 'yaml_data'
  },
  KEY_HIERARCHY => [
    {
      KEY_NAME => 'Common',
      KEY_PATH => 'common.yaml',
    }
  ]
}.freeze

Constants inherited from HieraConfig

Puppet::Pops::Lookup::HieraConfig::ALL_FUNCTION_KEYS, Puppet::Pops::Lookup::HieraConfig::CONFIG_FILE_NAME, Puppet::Pops::Lookup::HieraConfig::FUNCTION_KEYS, Puppet::Pops::Lookup::HieraConfig::FUNCTION_PROVIDERS, Puppet::Pops::Lookup::HieraConfig::KEY_BACKEND, Puppet::Pops::Lookup::HieraConfig::KEY_DATADIR, Puppet::Pops::Lookup::HieraConfig::KEY_DATA_DIG, Puppet::Pops::Lookup::HieraConfig::KEY_DATA_HASH, Puppet::Pops::Lookup::HieraConfig::KEY_DEFAULTS, Puppet::Pops::Lookup::HieraConfig::KEY_GLOB, Puppet::Pops::Lookup::HieraConfig::KEY_GLOBS, Puppet::Pops::Lookup::HieraConfig::KEY_HIERARCHY, Puppet::Pops::Lookup::HieraConfig::KEY_LOGGER, Puppet::Pops::Lookup::HieraConfig::KEY_LOOKUP_KEY, Puppet::Pops::Lookup::HieraConfig::KEY_NAME, Puppet::Pops::Lookup::HieraConfig::KEY_OPTIONS, Puppet::Pops::Lookup::HieraConfig::KEY_PATH, Puppet::Pops::Lookup::HieraConfig::KEY_PATHS, Puppet::Pops::Lookup::HieraConfig::KEY_URI, Puppet::Pops::Lookup::HieraConfig::KEY_URIS, Puppet::Pops::Lookup::HieraConfig::KEY_V3_BACKEND, Puppet::Pops::Lookup::HieraConfig::KEY_V3_DATA_HASH, Puppet::Pops::Lookup::HieraConfig::KEY_V4_DATA_HASH, Puppet::Pops::Lookup::HieraConfig::KEY_VERSION, Puppet::Pops::Lookup::HieraConfig::LOCATION_KEYS

Constants included from Puppet::Pops::LabelProvider

Puppet::Pops::LabelProvider::A, Puppet::Pops::LabelProvider::AN, Puppet::Pops::LabelProvider::SKIPPED_CHARACTERS, Puppet::Pops::LabelProvider::VOWELS

Constants included from SubLookup

SubLookup::SPECIAL

Instance Attribute Summary

Attributes inherited from HieraConfig

#config_path

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from HieraConfig

config_exist?, #configured_data_providers, create, #create_hiera3_backend_provider, #initialize, #name, #scope_interpolations_stable?, symkeys_to_string, v4_function_config

Methods included from Puppet::Pops::LabelProvider

#a_an, #a_an_uc, #article, #combine_strings, #label, #plural_s, #the, #the_uc

Methods included from LocationResolver

#expand_globs, #expand_uris, #resolve_paths

Methods included from Interpolation

#interpolate

Methods included from SubLookup

#split_key, #sub_lookup

Constructor Details

This class inherits a constructor from Puppet::Pops::Lookup::HieraConfig

Class Method Details

.config_typeObject

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.



452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
# File 'lib/puppet/pops/lookup/hiera_config.rb', line 452

def self.config_type
  return @@CONFIG_TYPE if class_variable_defined?(:@@CONFIG_TYPE_V5)
  tf = Types::TypeFactory
  nes_t = Types::PStringType::NON_EMPTY

  # Need alias here to avoid ridiculously long regexp burp in case of validation errors.
  uri_t = Pcore::TYPE_URI_ALIAS

  # The option name must start with a letter and end with a letter or digit. May contain underscore and dash.
  option_name_t = tf.pattern(/\A[A-Za-z](:?[0-9A-Za-z_-]*[0-9A-Za-z])?\z/)

  @@CONFIG_TYPE = tf.struct({
    KEY_VERSION => tf.range(5, 5),
    tf.optional(KEY_DEFAULTS) => tf.struct(
      {
        tf.optional(KEY_DATA_HASH) => nes_t,
        tf.optional(KEY_LOOKUP_KEY) => nes_t,
        tf.optional(KEY_DATA_DIG) => nes_t,
        tf.optional(KEY_DATADIR) => nes_t
      }),
    tf.optional(KEY_HIERARCHY) => tf.array_of(tf.struct(
      {
        KEY_NAME => nes_t,
        tf.optional(KEY_OPTIONS) => tf.hash_kv(option_name_t, tf.data),
        tf.optional(KEY_DATA_HASH) => nes_t,
        tf.optional(KEY_LOOKUP_KEY) => nes_t,
        tf.optional(KEY_V3_BACKEND) => nes_t,
        tf.optional(KEY_V4_DATA_HASH) => nes_t,
        tf.optional(KEY_DATA_DIG) => nes_t,
        tf.optional(KEY_PATH) => nes_t,
        tf.optional(KEY_PATHS) => tf.array_of(nes_t, tf.range(1, :default)),
        tf.optional(KEY_GLOB) => nes_t,
        tf.optional(KEY_GLOBS) => tf.array_of(nes_t, tf.range(1, :default)),
        tf.optional(KEY_URI) => uri_t,
        tf.optional(KEY_URIS) => tf.array_of(uri_t, tf.range(1, :default)),
        tf.optional(KEY_DATADIR) => nes_t
      }))
  })
end

Instance Method Details

#create_configured_data_providers(lookup_invocation, parent_data_provider) ⇒ 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.



492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
# File 'lib/puppet/pops/lookup/hiera_config.rb', line 492

def create_configured_data_providers(lookup_invocation, parent_data_provider)
  defaults = @config[KEY_DEFAULTS] || EMPTY_HASH
  datadir = defaults[KEY_DATADIR] || 'data'

  # Hashes enumerate their values in the order that the corresponding keys were inserted so it's safe to use
  # a hash for the data_providers.
  data_providers = {}
  @config[KEY_HIERARCHY].each do |he|
    name = he[KEY_NAME]
    raise Puppet::DataBinding::LookupError, "#{@config_path}: Name '#{name}' defined more than once" if data_providers.include?(name)
    function_kind = ALL_FUNCTION_KEYS.find { |key| he.include?(key) }
    if function_kind.nil?
      function_kind = FUNCTION_KEYS.find { |key| defaults.include?(key) }
      function_name = defaults[function_kind]
    else
      function_name = he[function_kind]
    end

    entry_datadir = @config_root + (he[KEY_DATADIR] || datadir)
    location_key = LOCATION_KEYS.find { |key| he.include?(key) }
    locations = case location_key
    when KEY_PATHS
      resolve_paths(entry_datadir, he[location_key], lookup_invocation, @config_path.nil?)
    when KEY_PATH
      resolve_paths(entry_datadir, [he[location_key]], lookup_invocation, @config_path.nil?)
    when KEY_GLOBS
      expand_globs(entry_datadir, he[location_key], lookup_invocation)
    when KEY_GLOB
      expand_globs(entry_datadir, [he[location_key]], lookup_invocation)
    when KEY_URIS
      expand_uris(he[location_key], lookup_invocation)
    when KEY_URI
      expand_uris([he[location_key]], lookup_invocation)
    else
      nil
    end
    next if @config_path.nil? && !locations.nil? && locations.empty? # Default config and no existing paths found
    options = he[KEY_OPTIONS]
    options = options.nil? ? EMPTY_HASH : interpolate(options, lookup_invocation, false)
    if(function_kind == KEY_V3_BACKEND)
      unless parent_data_provider.is_a?(GlobalDataProvider)
        # hiera3_backend is not allowed in environments and modules
        raise Puppet::DataBinding::LookupError, "#{@config_path}: '#{KEY_V3_BACKEND}' is only allowed in the global layer"
      end

      if function_name == 'json' || function_name == 'yaml' || function_name == 'hocon' &&  Puppet.features.hocon?
        # Disallow use of backends that have corresponding "data_hash" functions in version 5
        raise Puppet::DataBinding::LookupError, "#{@config_path}: Use \"#{KEY_DATA_HASH}: #{function_name}_data\" instead of \"#{KEY_V3_BACKEND}: #{function_name}\""
      end
      v3options = { :datadir => entry_datadir.to_s }
      options.each_pair { |k, v| v3options[k.to_sym] = v }
      data_providers[name] = create_hiera3_backend_provider(name, function_name, parent_data_provider, entry_datadir, locations, {
        :hierarchy =>
          locations.nil? ? [] : locations.map do |loc|
            path = loc.original_location
            path.end_with?(".#{function_name}") ? path[0..-(function_name.length + 2)] : path
          end,
        function_name.to_sym => v3options,
        :backends => [ function_name ],
        :logger => 'puppet'
      })
    else
      data_providers[name] = create_data_provider(name, parent_data_provider, function_kind, function_name, options, locations)
    end
  end
  data_providers.values
end

#validate_config(config) ⇒ 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.



576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
# File 'lib/puppet/pops/lookup/hiera_config.rb', line 576

def validate_config(config)
  config[KEY_DEFAULTS] ||= DEFAULT_CONFIG_HASH[KEY_DEFAULTS]
  config[KEY_HIERARCHY] ||= DEFAULT_CONFIG_HASH[KEY_HIERARCHY]

  Types::TypeAsserter.assert_instance_of(["The Lookup Configuration at '%s'", @config_path], self.class.config_type, config)
  defaults = config[KEY_DEFAULTS]
  validate_defaults(defaults) unless defaults.nil?
  config[KEY_HIERARCHY].each do |he|
    name = he[KEY_NAME]
    case ALL_FUNCTION_KEYS.count { |key| he.include?(key) }
    when 0
      if defaults.nil? || FUNCTION_KEYS.count { |key| defaults.include?(key) } == 0
        raise Puppet::DataBinding::LookupError,
          "#{@config_path}: One of #{combine_strings(FUNCTION_KEYS)} must defined in hierarchy '#{name}'"
      end
    when 1
      # OK
    when 0
      raise Puppet::DataBinding::LookupError,
        "#{@config_path}: One of #{combine_strings(FUNCTION_KEYS)} must defined in hierarchy '#{name}'"
    else
      raise Puppet::DataBinding::LookupError,
        "#{@config_path}: Only one of #{combine_strings(FUNCTION_KEYS)} can be defined in hierarchy '#{name}'"
    end

    if LOCATION_KEYS.count { |key| he.include?(key) } > 1
      raise Puppet::DataBinding::LookupError,
        "#{@config_path}: Only one of #{combine_strings(LOCATION_KEYS)} can be defined in hierarchy '#{name}'"
    end

    options = he[KEY_OPTIONS]
    unless options.nil?
      RESERVED_OPTION_KEYS.each do |key|
        if options.include?(key)
          raise Puppet::DataBinding::LookupError, "#{@config_path}: Option key '#{key}' used in hierarchy '#{name}' is reserved by Puppet"
        end
      end
    end
  end
  config
end

#validate_defaults(defaults) ⇒ 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.



618
619
620
621
622
623
624
625
626
# File 'lib/puppet/pops/lookup/hiera_config.rb', line 618

def validate_defaults(defaults)
  case FUNCTION_KEYS.count { |key| defaults.include?(key) }
  when 0, 1
    # OK
  else
    raise Puppet::DataBinding::LookupError,
      "#{@config_path}: Only one of #{combine_strings(FUNCTION_KEYS)} can be defined in defaults"
  end
end

#versionObject

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.



628
629
630
# File 'lib/puppet/pops/lookup/hiera_config.rb', line 628

def version
  5
end