bsw@1071: -- generate a ready-to-use list of the configures LDAP servers bsw@1071: -- -------------------------------------------------------------------------- bsw@1071: -- result is in order of preference, including a round robin style random bsw@1071: -- selection of the preference of hosts with the same preference bsw@1071: -- bsw@1071: -- returns: bsw@1071: -- hostlist: flattened list of LDAP servers in order of preference bsw@1071: bsw@1071: function ldap.get_hosts() bsw@1071: bsw@1071: math.randomseed(os.time()) bsw@1071: bsw@1071: local hostlist = {} bsw@1071: bsw@1071: -- iterate through all entries on base level bsw@1071: for i, host in ipairs (config.ldap.hosts) do bsw@1071: bsw@1071: -- a single host entry bsw@1071: if host.uri then bsw@1071: hostlist[#hostlist+1] = host bsw@1071: bsw@1071: -- or a list of host entries bsw@1071: else bsw@1071: local subhostlist = {} bsw@1071: bsw@1071: -- sort list of host entries by random bsw@1071: for j, subhost in ipairs(host) do bsw@1071: subhost.priority = math.random() bsw@1071: subhostlist[#subhostlist+1] = subhost bsw@1071: end bsw@1071: table.sort(subhostlist, function (a,b) bsw@1071: return a.priority < b.priority bsw@1071: end) bsw@1071: bsw@1071: -- and add them to the main list bsw@1071: for i, subhost in ipairs(subhostlist) do bsw@1071: hostlist[#hostlist+1] = subhost bsw@1071: end bsw@1071: bsw@1071: end bsw@1071: bsw@1071: end bsw@1071: bsw@1071: return hostlist bsw@1071: bsw@1071: end