Rewrite for testability

Testing the methods inside of the function is a complete pain.  This
moves the code out to use the PuppetX pattern and adds some basic unit
tests that validate the logic.
This commit is contained in:
Zach Leslie
2015-05-19 14:42:18 -07:00
parent f0e5e6e8a0
commit cb15ae8e70
9 changed files with 243 additions and 66 deletions

View File

@@ -1,75 +1,19 @@
require 'puppet_x/ldapquery'
begin
require 'net/ldap'
rescue
Puppet.warn("Missing net/ldap gem for ldapquery() function")
Puppet.warn("Missing net/ldap gem required for ldapquery() function")
end
Puppet::Parser::Functions.newfunction(:ldapquery, :type => :rvalue) do |args|
Puppet::Parser::Functions.newfunction(:ldapquery,
:type => :rvalue) do |args|
Puppet.debug(args.size)
filter = args[0]
attributes = args[1]
host = Puppet[:ldapserver]
port = Puppet[:ldapport]
base = Puppet[:ldapbase]
user = Puppet[:ldapuser]
password = Puppet[:ldappassword]
tls = Puppet[:ldaptls]
ca_file = "#{Puppet[:confdir]}/ldap_ca.pem"
conf = {
:host => host,
:port => port,
}
if user != '' and password != ''
conf[:auth] = {
:method => :simple,
:username => user,
:password => password,
}
if args.size > 2
raise Puppet::ParseError, "Too many arguments received in ldapquery()"
end
if tls
conf[:encryption] = {
:method => :simple_tls,
:tls_options => { :ca_file => ca_file }
}
end
filter, attributes = args
Puppet.debug(conf)
Puppet.debug("Searching ldap base #{base} using #{filter} for #{attributes}")
ldap = Net::LDAP.new(conf)
filter = Net::LDAP::Filter.construct(filter)
data = []
ldap.search( :base => base, :filter => filter, :attributes => attributes) do |entry|
entry_data = {}
entry.each do |attribute, values|
attr = attribute.to_s
if values.is_a? Array and values.size > 1
entry_data[attr] = []
values.each do |v|
entry_data[attr] << v
end
elsif values.is_a? Array and values.size == 1
entry_data[attr] = values[0]
else
entry_data[attr] = values
end
end
data << entry_data
end
return data
return PuppetX::LDAPquery.new(filter, attributes).results
end

127
lib/puppet_x/ldapquery.rb Normal file
View File

@@ -0,0 +1,127 @@
# Class: PuppetX::LDAPquery
#
module PuppetX
class LDAPquery
attr_reader :content
def initialize(
filter,
attributes=[],
base=Puppet[:ldapbase]
)
@filter = filter
@attributes = attributes
@base = base
end
def get_config
# Load the configuration variables from Puppet
required_vars = [
:ldapserver,
:ldapport,
]
required_vars.each {|r|
unless Puppet[r]
raise Puppet::ParseError, "Missing required setting '#{r.to_s}' in puppet.conf"
end
}
host = Puppet[:ldapserver]
port = Puppet[:ldapport]
if Puppet[:ldapuser] and Puppet[:ldappassword]
user = Puppet[:ldapuser]
password = Puppet[:ldappassword]
end
tls = Puppet[:ldaptls]
ca_file = "#{Puppet[:confdir]}/ldap_ca.pem"
conf = {
:host => host,
:port => port,
}
if user != '' and password != ''
conf[:auth] = {
:method => :simple,
:username => user,
:password => password,
}
end
if tls
conf[:encryption] = {
:method => :simple_tls,
:tls_options => { :ca_file => ca_file }
}
end
Puppet.debug(conf)
return conf
end
def get_entries()
# Query the LDAP server for attributes using the filter
#
# Returns: An array of Net::LDAP::Entry objects
ldapfilter = @filter
attributes = @attributes
base = @base
conf = self.get_config()
Puppet.debug("Searching ldap base #{base} using #{@filter} for #{@attributes}")
ldap = Net::LDAP.new(conf)
ldapfilter = Net::LDAP::Filter.construct(@filter)
entries = []
begin
ldap.search(:base => base,
:filter => ldapfilter,
:attributes => attributes,
:time => 10) do |entry|
entries << entry
end
Puppet.debug(entries)
return entries
rescue
return []
end
end
def parse_entries
data = []
entries = get_entries()
entries.each do |entry|
entry_data = {}
entry.each do |attribute, values|
attr = attribute.to_s
if values.is_a? Array and values.size > 1
entry_data[attr] = []
values.each do |v|
entry_data[attr] << v.chomp
end
elsif values.is_a? Array and values.size == 1
entry_data[attr] = values[0].chomp
else
entry_data[attr] = values.chomp
end
end
data << entry_data
end
return data
end
def results
parse_entries
end
end
end