Sha256: b8ba6247e820120466d212b512c575bf778e98d3a14c4c7db91d9fa06dd8edc9

Contents?: true

Size: 1.76 KB

Versions: 120

Compression:

Stored size: 1.76 KB

Contents

require 'hiera'
require 'hiera/scope'
require_relative 'puppet'

module HieraPuppet
  module_function

  def lookup(key, default, scope, override, resolution_type)
    scope = Hiera::Scope.new(scope)

    answer = hiera.lookup(key, default, scope, override, resolution_type)

    if answer.nil?
      raise Puppet::ParseError, _("Could not find data item %{key} in any Hiera data file and no default supplied") % { key: key }
    end

    answer
  end

  def parse_args(args)
    # Functions called from Puppet manifests like this:
    #
    #   hiera("foo", "bar")
    #
    # Are invoked internally after combining the positional arguments into a
    # single array:
    #
    #   func = function_hiera
    #   func(["foo", "bar"])
    #
    # Functions called from templates preserve the positional arguments:
    #
    #   scope.function_hiera("foo", "bar")
    #
    # Deal with Puppet's special calling mechanism here.
    if args[0].is_a?(Array)
      args = args[0]
    end

    if args.empty?
      raise Puppet::ParseError, _("Please supply a parameter to perform a Hiera lookup")
    end

    key      = args[0]
    default  = args[1]
    override = args[2]

    return [key, default, override]
  end

  private
  module_function

  def hiera
    @hiera ||= Hiera.new(:config => hiera_config)
  end

  def hiera_config
    config = {}

    config_file = hiera_config_file
    if config_file
      config = Hiera::Config.load(config_file)
    end

    config[:logger] = 'puppet'
    config
  end

  def hiera_config_file
    hiera_config = Puppet.settings[:hiera_config]
    if Puppet::FileSystem.exist?(hiera_config)
      hiera_config
    else
      Puppet.warning _("Config file %{hiera_config} not found, using Hiera defaults") % { hiera_config: hiera_config }
      nil
    end
  end
end

Version data entries

120 entries across 120 versions & 1 rubygems

Version Path
puppet-7.11.0 lib/hiera_puppet.rb
puppet-7.11.0-x86-mingw32 lib/hiera_puppet.rb
puppet-7.11.0-x64-mingw32 lib/hiera_puppet.rb
puppet-7.11.0-universal-darwin lib/hiera_puppet.rb
puppet-7.10.0 lib/hiera_puppet.rb
puppet-7.10.0-x86-mingw32 lib/hiera_puppet.rb
puppet-7.10.0-x64-mingw32 lib/hiera_puppet.rb
puppet-7.10.0-universal-darwin lib/hiera_puppet.rb
puppet-7.9.0 lib/hiera_puppet.rb
puppet-7.9.0-x86-mingw32 lib/hiera_puppet.rb
puppet-7.9.0-x64-mingw32 lib/hiera_puppet.rb
puppet-7.9.0-universal-darwin lib/hiera_puppet.rb
puppet-7.8.0 lib/hiera_puppet.rb
puppet-7.8.0-x86-mingw32 lib/hiera_puppet.rb
puppet-7.8.0-x64-mingw32 lib/hiera_puppet.rb
puppet-7.8.0-universal-darwin lib/hiera_puppet.rb
puppet-7.7.0 lib/hiera_puppet.rb
puppet-7.7.0-x86-mingw32 lib/hiera_puppet.rb
puppet-7.7.0-x64-mingw32 lib/hiera_puppet.rb
puppet-7.7.0-universal-darwin lib/hiera_puppet.rb