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_DEFAULT_HIERARCHY, Puppet::Pops::Lookup::HieraConfig::KEY_EXTENSION, 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_MAPPED_PATHS, 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_V3_LOOKUP_KEY, 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, #fail, #find_line_matching, #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_mapped_paths, #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.



541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
# File 'lib/puppet/pops/lookup/hiera_config.rb', line 541

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

  # Validated using Ruby URI implementation
  uri_t = Types::PStringType::NON_EMPTY

  # 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/)

  hierarchy_t = 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_MAPPED_PATHS) => tf.array_of(nes_t, tf.range(3, 3)),
      tf.optional(KEY_DATADIR) => nes_t
    }))

  @@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_OPTIONS) => tf.hash_kv(option_name_t, tf.data),
      }),
    tf.optional(KEY_HIERARCHY) => hierarchy_t,
    tf.optional(KEY_DEFAULT_HIERARCHY) => hierarchy_t
  })
end

Instance Method Details

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



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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
# File 'lib/puppet/pops/lookup/hiera_config.rb', line 586

def create_configured_data_providers(lookup_invocation, parent_data_provider, use_default_hierarchy)
  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 = {}

  if @config.include?(KEY_DEFAULT_HIERARCHY)
    unless parent_data_provider.is_a?(ModuleDataProvider)
      fail(Issues::HIERA_DEFAULT_HIERARCHY_NOT_IN_MODULE, EMPTY_HASH, find_line_matching(/\s+default_hierarchy:/))
    end
  elsif use_default_hierarchy
    return data_providers
  end

  @config[use_default_hierarchy ? KEY_DEFAULT_HIERARCHY : KEY_HIERARCHY].each do |he|
    name = he[KEY_NAME]
    if data_providers.include?(name)
      first_line = find_line_matching(/\s+name:\s+['"]?#{name}(?:[^\w]|$)/)
      line = find_line_matching(/\s+name:\s+['"]?#{name}(?:[^\w]|$)/, first_line + 1) if first_line
      unless line
        line = first_line
        first_line = nil
      end
      fail(Issues::HIERA_HIERARCHY_NAME_MULTIPLY_DEFINED, { :name => name, :first_line => first_line }, line)
    end
    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)
    entry_datadir = Pathname(interpolate(entry_datadir.to_s, lookup_invocation, false))
    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)
    when KEY_MAPPED_PATHS
      expand_mapped_paths(entry_datadir, 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] || defaults[KEY_OPTIONS]
    options = options.nil? ? EMPTY_HASH : interpolate(options, lookup_invocation, false)
    if(function_kind == KEY_V3_BACKEND)
      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

#has_default_hierarchy?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:

  • (Boolean)


665
666
667
# File 'lib/puppet/pops/lookup/hiera_config.rb', line 665

def has_default_hierarchy?
  @config.include?(KEY_DEFAULT_HIERARCHY)
end

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



685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
# File 'lib/puppet/pops/lookup/hiera_config.rb', line 685

def validate_config(config, owner)
  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 { |he| validate_hierarchy(he, defaults, owner) }

  if config.include?(KEY_DEFAULT_HIERARCHY)
    unless owner.is_a?(ModuleDataProvider)
      fail(Issues::HIERA_DEFAULT_HIERARCHY_NOT_IN_MODULE, EMPTY_HASH, find_line_matching(/(?:^|\s+)#{KEY_DEFAULT_HIERARCHY}:/))
    end
    config[KEY_DEFAULT_HIERARCHY].each { |he| validate_hierarchy(he, defaults, owner) }
  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.



740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
# File 'lib/puppet/pops/lookup/hiera_config.rb', line 740

def validate_defaults(defaults)
  case FUNCTION_KEYS.count { |key| defaults.include?(key) }
  when 0, 1
    # OK
  else
    fail(Issues::HIERA_MULTIPLE_DATA_PROVIDER_FUNCTIONS_IN_DEFAULT)
  end

  options = defaults[KEY_OPTIONS]
  unless options.nil?
    RESERVED_OPTION_KEYS.each do |key|
      fail(Issues::HIERA_DEFAULT_OPTION_RESERVED_BY_PUPPET, :key => key) if options.include?(key)
    end
  end
end

#validate_hierarchy(he, defaults, owner) ⇒ 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.



703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
# File 'lib/puppet/pops/lookup/hiera_config.rb', line 703

def validate_hierarchy(he, defaults, owner)
  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
      fail(Issues::HIERA_MISSING_DATA_PROVIDER_FUNCTION, :name => name)
    end
  when 1
    # OK
  else
    fail(Issues::HIERA_MULTIPLE_DATA_PROVIDER_FUNCTIONS, :name => name)
  end

  v3_backend = he[KEY_V3_BACKEND]
  unless v3_backend.nil?
    unless owner.is_a?(GlobalDataProvider)
      fail(Issues::HIERA_V3_BACKEND_NOT_GLOBAL, EMPTY_HASH, find_line_matching(/\s+#{KEY_V3_BACKEND}:/))
    end
    if v3_backend == 'json' || v3_backend == 'yaml' || v3_backend == 'hocon' &&  Puppet.features.hocon?
      # Disallow use of backends that have corresponding "data_hash" functions in version 5
      fail(Issues::HIERA_V3_BACKEND_REPLACED_BY_DATA_HASH, { :function_name => v3_backend },
        find_line_matching(/\s+#{KEY_V3_BACKEND}:\s*['"]?#{v3_backend}(?:[^\w]|$)/))
    end
  end

  if LOCATION_KEYS.count { |key| he.include?(key) } > 1
    fail(Issues::HIERA_MULTIPLE_LOCATION_SPECS, :name => name)
  end

  options = he[KEY_OPTIONS]
  unless options.nil?
    RESERVED_OPTION_KEYS.each do |key|
      fail(Issues::HIERA_OPTION_RESERVED_BY_PUPPET, :key => key, :name => name) if options.include?(key)
    end
  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.



756
757
758
# File 'lib/puppet/pops/lookup/hiera_config.rb', line 756

def version
  5
end