Sha256: 0faa423987259c9918364a86670cd564f503482a2d8b635d37b835c4c6148466

Contents?: true

Size: 2 KB

Versions: 110

Compression:

Stored size: 2 KB

Contents

require 'hiera'
require 'hiera/scope'
require '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")
    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 = {}

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

    config[:logger] = 'puppet'
    config
  end

  def hiera_config_file
    config_file = nil

    if Puppet.settings[:hiera_config].is_a?(String)
      expanded_config_file = File.expand_path(Puppet.settings[:hiera_config])
      if Puppet::FileSystem.exist?(expanded_config_file)
        config_file = expanded_config_file
      end
    elsif Puppet.settings[:confdir].is_a?(String)
      expanded_config_file = File.expand_path(File.join(Puppet.settings[:confdir], '/hiera.yaml'))
      if Puppet::FileSystem.exist?(expanded_config_file)
        config_file = expanded_config_file
      end
    end

    config_file
  end
end

Version data entries

110 entries across 110 versions & 2 rubygems

Version Path
puppet-retrospec-0.12.2 vendor/gems/puppet-3.7.3/lib/hiera_puppet.rb
puppet-4.4.2 lib/hiera_puppet.rb
puppet-4.4.2-x86-mingw32 lib/hiera_puppet.rb
puppet-4.4.2-x64-mingw32 lib/hiera_puppet.rb
puppet-4.4.2-universal-darwin lib/hiera_puppet.rb
puppet-3.8.7 lib/hiera_puppet.rb
puppet-3.8.7-x86-mingw32 lib/hiera_puppet.rb
puppet-3.8.7-x64-mingw32 lib/hiera_puppet.rb
puppet-4.4.1 lib/hiera_puppet.rb
puppet-4.4.1-x86-mingw32 lib/hiera_puppet.rb
puppet-4.4.1-x64-mingw32 lib/hiera_puppet.rb
puppet-4.4.1-universal-darwin lib/hiera_puppet.rb
puppet-4.4.0 lib/hiera_puppet.rb
puppet-4.4.0-x86-mingw32 lib/hiera_puppet.rb
puppet-4.4.0-x64-mingw32 lib/hiera_puppet.rb
puppet-4.4.0-universal-darwin lib/hiera_puppet.rb
puppet-3.8.6 lib/hiera_puppet.rb
puppet-3.8.6-x86-mingw32 lib/hiera_puppet.rb
puppet-retrospec-0.12.1 vendor/gems/puppet-3.7.3/lib/hiera_puppet.rb
puppet-3.8.6-x64-mingw32 lib/hiera_puppet.rb