Class: Puppet::DataProviders::HieraConfig Deprecated Private

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

Overview

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.

Deprecated.

TODO: API 5.0, remove this class

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 Pops::Lookup::SubLookup

Pops::Lookup::SubLookup::SPECIAL

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::LocationResolver

#expand_globs, #expand_mapped_paths, #expand_uris, #resolve_paths

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



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/puppet/data_providers/hiera_config.rb', line 67

def initialize(config_root)
  unless Puppet[:strict] == :off
    Puppet.warn_once(:deprecation, 'Puppet::DataProviders::HieraConfig',
      "Use of class Puppet::DataProviders::HieraConfig' is deprecated. Puppet::Pops::Lookup::HieraConfig should be used instead", config_path.to_s)
  end
  @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.



60
61
62
# File 'lib/puppet/data_providers/hiera_config.rb', line 60

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.



60
61
62
# File 'lib/puppet/data_providers/hiera_config.rb', line 60

def version
  @version
end

Class Method Details

.config_exist?(config_root) ⇒ 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)


38
39
40
41
# File 'lib/puppet/data_providers/hiera_config.rb', line 38

def self.config_exist?(config_root)
  config_path = config_root + 'hiera.yaml'
  config_path.exist?
end

.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.



23
24
25
# File 'lib/puppet/data_providers/hiera_config.rb', line 23

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.



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

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:



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/puppet/data_providers/hiera_config.rb', line 96

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, "#{@config_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)}
    datadir = @config_root + (he['datadir'] || default_datadir)
    provider_name = he['backend']
    data_providers[name] = case provider_name
    when 'json', 'yaml'
      Puppet::Pops::Lookup::DataHashFunctionProvider.new(name, parent_data_provider, "#{provider_name}_data", {},
        resolve_paths(datadir, original_paths, lookup_invocation, ".#{provider_name}"))
    else
      # TODO: Remove support for injected provider factories
      paths = original_paths.map { |path| interpolate(path, lookup_invocation, false)}
      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
      create_data_provider(parent_data_provider, provider_factory, name, provider_factory.resolve_paths(datadir, original_paths, paths, lookup_invocation))
    end
  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.



84
85
86
87
# File 'lib/puppet/data_providers/hiera_config.rb', line 84

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

#nameObject

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.



131
132
133
# File 'lib/puppet/data_providers/hiera_config.rb', line 131

def name
  "hiera version #{version}"
end