]> git.immae.eu Git - perso/Immae/Projets/Puppet.git/blame - modules/base_installation/lib/puppet/functions/ldap_data.rb
Order ldap configuration by priority
[perso/Immae/Projets/Puppet.git] / modules / base_installation / lib / puppet / functions / ldap_data.rb
CommitLineData
e8493916
IB
1require 'json'
2
3Puppet::Functions.create_function(:ldap_data) do
4 dispatch :ldap_data do
5 param 'Hash', :options
6 param 'Puppet::LookupContext', :context
7 end
8
9 def ldap_data(options, context)
10 begin
11 require 'ldap'
12 require 'puppet/util/ldap/connection'
a55a5e4a 13 rescue LoadError
e8493916
IB
14 context.not_found
15 return
16 end
17
18 if !context.cache_has_key("ldap_lookup")
19 begin
20 conn = Puppet::Util::Ldap::Connection.instance
21 conn.start
22 connection = conn.connection
23 rescue ::LDAP::ResultError => e
24 raise Puppet::ParseError, ("ldapquery(): LDAP ResultError - #{e.message}")
25 end
26
27 host = Facter.value('ec2_metadata')["hostname"]
28 base = Puppet[:ldapbase]
29 scope = ::LDAP::LDAP_SCOPE_SUBTREE
30 filter = "(objectclass=*)"
31
32 data = {}
435b97f5
IB
33 data_array = connection.search2(base, scope, filter, attrs=["immaePuppetJson", "dn"]).map do |entry|
34 [entry["dn"].first, entry["immaePuppetJson"] || []]
35 end.sort_by do |dn, json|
36 if dn == "ou=roles,ou=hosts,dc=immae,dc=eu"
37 [0, dn]
38 elsif dn.end_with?("ou=roles,ou=hosts,dc=immae,dc=eu")
39 [1, dn]
40 else
41 [2, dn]
42 end
43 end.to_h
44
45 data_array.each do |dn, jsons|
e8493916
IB
46 jsons.each do |json|
47 data.merge!(JSON.parse(json))
48 end
49 end
50
51 context.cache("ldap_lookup", data)
52 end
53
54 context.cached_value("ldap_lookup")
55 end
56end