lib/hiera/backend/mysql_json_backend.rb in hiera-mysql-json-backend-1.0.1 vs lib/hiera/backend/mysql_json_backend.rb in hiera-mysql-json-backend-1.1.0
- old
+ new
@@ -16,48 +16,68 @@
end
end
@cache = cache || Filecache.new
- Hiera.debug("Hiera mysql_json initialized")
+ Hiera.debug('Hiera mysql_json initialized')
end
+ def should_lookup?(constraints, scope)
+ return true unless constraints.is_a?(Hash)
+ should_lookup = false
+ constraints.each do |item, matchers|
+ next unless scope.exist?(item.to_s)
+ if scope[item.to_s] =~ compile_regexes(matchers)
+ should_lookup = true
+ break
+ end
+ end
+ should_lookup
+ end
+
+ def compile_regexes(regexes)
+ res = regexes.map { |re| Regexp.compile(re) }
+ Regexp.union(res)
+ end
+
def lookup(key, scope, order_override, resolution_type)
# default answer is set to nil otherwise the lookup ends up returning
# an Array of nils and causing a Puppet::Parser::AST::Resource failed with error ArgumentError
# for any other lookup because their default value is overwriten by [nil,nil,nil,nil]
# so hiera('myvalue', 'test1') returns [nil,nil,nil,nil]
results = nil
- Hiera.debug("looking up #{key} in MySQL2 Backend")
+ Hiera.debug("looking up #{key} in mysql_json Backend")
Hiera.debug("resolution type is #{resolution_type}")
+ return nil unless should_lookup?(Config[:mysql_json][:only_for], scope)
+
Backend.datasources(scope, order_override) do |source|
Hiera.debug("Looking for data source #{source}")
sqlfile = Backend.datafile(:mysql_json, scope, source, "sql") || next
next unless File.exist?(sqlfile)
data = @cache.read(sqlfile, Hash, {}) do |datafile|
YAML.load(datafile)
end
mysql_config = data.fetch(:dbconfig, {})
- mysql_host = mysql_config.fetch(:host, nil) || Config[:mysql2][:host] || 'localhost'
- mysql_user = mysql_config.fetch(:user, nil) || Config[:mysql2][:user]
- mysql_pass = mysql_config.fetch(:pass, nil) || Config[:mysql2][:pass]
- mysql_port = mysql_config.fetch(:port, nil) || Config[:mysql2][:port] || '3306'
- mysql_database = mysql_config.fetch(:database, nil) || Config[:mysql2][:database]
+ mysql_host = mysql_config.fetch(:host, nil) || Config[:mysql_json][:host] || 'localhost'
+ mysql_user = mysql_config.fetch(:user, nil) || Config[:mysql_json][:user]
+ mysql_pass = mysql_config.fetch(:pass, nil) || Config[:mysql_json][:pass]
+ mysql_port = mysql_config.fetch(:port, nil) || Config[:mysql_json][:port] || '3306'
+ mysql_database = mysql_config.fetch(:database, nil) || Config[:mysql_json][:database]
connection_hash = {
- :host => mysql_host,
- :username => mysql_user,
- :password => mysql_pass,
- :database => mysql_database,
- :port => mysql_port,
- :reconnect => true}
+ host: mysql_host,
+ username: mysql_user,
+ password: mysql_pass,
+ database: mysql_database,
+ port: mysql_port,
+ reconnect: true
+ }
-
Hiera.debug("data #{data.inspect}")
next if data.empty?
next unless data.include?(key)
Hiera.debug("Found #{key} in #{source}")
@@ -68,10 +88,11 @@
next if sql_results.length != 1
begin
new_answer = JSON.parse(sql_results[0]['value'])
rescue
+ raise Exception, "JSON parse error for key '#{key}'." unless Config[:mysql_json][:ignore_json_parse_errors]
Hiera.debug("Miserable failure while looking for #{key}.")
next
end
case resolution_type.is_a?(Hash) ? :hash : resolution_type
@@ -86,41 +107,40 @@
else
results = new_answer
break
end
end
- return results
+ results
end
-
def query(connection_hash, query)
Hiera.debug("Executing SQL Query: #{query}")
- data=[]
- mysql_host=connection_hash[:host]
- mysql_user=connection_hash[:username]
- mysql_pass=connection_hash[:password]
- mysql_database=connection_hash[:database]
- mysql_port=connection_hash[:port]
+ data = []
+ mysql_host = connection_hash[:host]
+ mysql_user = connection_hash[:username]
+ mysql_pass = connection_hash[:password]
+ mysql_database = connection_hash[:database]
+ mysql_port = connection_hash[:port]
if defined?(JRUBY_VERSION)
Jdbc::MySQL.load_driver
url = "jdbc:mysql://#{mysql_host}:#{mysql_port}/#{mysql_database}"
props = java.util.Properties.new
props.set_property :user, mysql_user
props.set_property :password, mysql_pass
- conn = com.mysql.jdbc.Driver.new.connect(url,props)
+ conn = com.mysql.jdbc.Driver.new.connect(url, props)
stmt = conn.create_statement
res = stmt.execute_query(query)
md = res.getMetaData
numcols = md.getColumnCount
Hiera.debug("Mysql Query returned #{numcols} rows")
- while ( res.next ) do
+ while res.next
if numcols < 2
Hiera.debug("Mysql value : #{res.getString(1)}")
data << res.getString(1)
else
row = {}
@@ -142,11 +162,10 @@
ensure
client.close
end
end
- return data
-
+ data
end
end
end
end