Class: Puppet::DataProviders::HieraConfig Private

Inherits:
Object
  • Object
show all
Includes:
Plugins::DataProviders, Pops::Lookup::Interpolation
Defined in:
lib/puppet/data_providers/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

DEFAULT_CONFIG =

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.

{
  'version' => 4,
  'datadir' => 'data',
  'hierarchy' => [
    {
      'name' => 'common',
      'backend' => 'yaml'
    }
  ]
}.freeze

Constants included from Plugins::DataProviders

Plugins::DataProviders::ENV_DATA_PROVIDERS_KEY, Plugins::DataProviders::ENV_DATA_PROVIDERS_TYPE, Plugins::DataProviders::MODULE_DATA_PROVIDERS_KEY, Plugins::DataProviders::MODULE_DATA_PROVIDERS_TYPE, Plugins::DataProviders::PATH_BASED_DATA_PROVIDER_FACTORIES_KEY, Plugins::DataProviders::PATH_BASED_DATA_PROVIDER_FACTORIES_TYPE, Plugins::DataProviders::PER_MODULE_DATA_PROVIDER_KEY, Plugins::DataProviders::PER_MODULE_DATA_PROVIDER_TYPE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Pops::Lookup::Interpolation

#interpolate

Methods included from Pops::Lookup::SubLookup

#split_key, #sub_lookup

Constructor Details

#initialize(config_root) ⇒ HieraConfig

Creates a new HieraConfig from the given config_root. This is where the ‘hiera.yaml’ is expected to be found and is also the base location used when resolving relative paths.

Parameters:

  • config_root (Pathname)

    Absolute path to the configuration root



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/puppet/data_providers/hiera_config.rb', line 60

def initialize(config_root)
  @config_root = config_root
  @config_path = config_root + 'hiera.yaml'
  if @config_path.exist?
    @config = validate_config(HieraConfig.symkeys_to_string(YAML.load_file(@config_path)))
    @config['hierarchy'] ||= DEFAULT_CONFIG['hierarchy']
    @config['datadir'] ||= DEFAULT_CONFIG['datadir']
  else
    @config = DEFAULT_CONFIG
  end
  @version = @config['version']
end

Instance Attribute Details

#config_pathObject (readonly)

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.



53
54
55
# File 'lib/puppet/data_providers/hiera_config.rb', line 53

def config_path
  @config_path
end

#versionObject (readonly)

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.



53
54
55
# File 'lib/puppet/data_providers/hiera_config.rb', line 53

def version
  @version
end

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.



21
22
23
# File 'lib/puppet/data_providers/hiera_config.rb', line 21

def self.config_type
  @@CONFIG_TYPE ||= create_config_type
end

.symkeys_to_string(struct) ⇒ 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.



25
26
27
28
29
30
31
32
33
34
# File 'lib/puppet/data_providers/hiera_config.rb', line 25

def self.symkeys_to_string(struct)
  case(struct)
  when Hash
    Hash[struct.map { |k,v| [k.to_s, symkeys_to_string(v)] }]
  when Array
    struct.map { |v| symkeys_to_string(v) }
  else
    struct
  end
end

Instance Method Details

#create_configured_data_providers(lookup_invocation, parent_data_provider) ⇒ Array[DataProvider]

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.

Creates the data providers for this config

Parameters:

Returns:



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/puppet/data_providers/hiera_config.rb', line 85

def create_configured_data_providers(lookup_invocation, parent_data_provider)
  injector = Puppet.lookup(:injector)
  service_type = Registry.hash_of_path_based_data_provider_factories
  default_datadir = @config['datadir']

  # 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['hierarchy'].each do |he|
    name = he['name']
    raise Puppet::DataBinding::LookupError, "#{path}: Name '#{name}' defined more than once" if data_providers.include?(name)
    original_paths = he['paths']
    if original_paths.nil?
      single_path = he['path']
      single_path = name if single_path.nil?
      original_paths = [single_path]
    end
    paths = original_paths.map { |path| interpolate(path, lookup_invocation, false)}
    provider_name = he['backend']
    provider_factory = injector.lookup(nil, service_type, PATH_BASED_DATA_PROVIDER_FACTORIES_KEY)[provider_name]
    raise Puppet::DataBinding::LookupError, "#{@config_path}: No data provider is registered for backend '#{provider_name}' " unless provider_factory

    datadir = @config_root + (he['datadir'] || default_datadir)
    data_providers[name] = create_data_provider(parent_data_provider, provider_factory, name,
      provider_factory.resolve_paths(datadir, original_paths, paths, lookup_invocation))
  end
  data_providers.values
end

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



73
74
75
76
# File 'lib/puppet/data_providers/hiera_config.rb', line 73

def create_data_providers(lookup_invocation)
  Puppet.deprecation_warning('Method HieraConfig#create_data_providers is deprecated. Use create_configured_data_providers instead')
  create_configured_data_providers(lookup_invocation, nil)
end