aboutsummaryrefslogblamecommitdiff
path: root/modules/base_installation/lib/puppet/functions/ldap_data.rb
blob: ff8d7794ad964a0fd1fd869c0b504a1e3de4a9f8 (plain) (tree)













































                                                                                  
require 'json'

Puppet::Functions.create_function(:ldap_data) do
  dispatch :ldap_data do
    param 'Hash', :options
    param 'Puppet::LookupContext', :context
  end

  def ldap_data(options, context)
    begin
      require 'ldap'
      require 'puppet/util/ldap/connection'
    rescue
      context.not_found
      return
    end

    if !context.cache_has_key("ldap_lookup")
      begin
        conn = Puppet::Util::Ldap::Connection.instance
        conn.start
        connection = conn.connection
      rescue ::LDAP::ResultError => e
        raise Puppet::ParseError, ("ldapquery(): LDAP ResultError - #{e.message}")
      end

      host = Facter.value('ec2_metadata')["hostname"]
      base = Puppet[:ldapbase]
      scope  = ::LDAP::LDAP_SCOPE_SUBTREE
      filter = "(objectclass=*)"

      data = {}
      connection.search(base, scope, filter) do |entry|
        data_ = entry.to_hash
        jsons = data_["immaePuppetJson"] || []
        jsons.each do |json|
          data.merge!(JSON.parse(json))
        end
      end

      context.cache("ldap_lookup", data)
    end

    context.cached_value("ldap_lookup")
  end
end