Class: Puppet::DataProviders::HieraConfig

Inherits:
Object
  • Object
show all
Includes:
HieraInterpolate, Plugins::DataProviders
Defined in:
lib/puppet/data_providers/hiera_config.rb

Constant Summary collapse

DEFAULT_CONFIG =
{
  '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 HieraInterpolate

#interpolate

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.



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

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)



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

def config_path
  @config_path
end

#versionObject (readonly)



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

def version
  @version
end

Class Method Details

.config_typeObject



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

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

.symkeys_to_string(struct) ⇒ Object



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

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_data_providers(lookup_invocation) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/puppet/data_providers/hiera_config.rb', line 72

def create_data_providers(lookup_invocation)
  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)
    resolved_paths = provider_factory.resolve_paths(datadir, original_paths, paths, lookup_invocation)
    data_providers[name] = provider_factory.create(name, resolved_paths)
  end
  data_providers.values
end