lib/hiera/backend/mysql2_backend.rb in hiera-mysql-backend-0.0.3 vs lib/hiera/backend/mysql2_backend.rb in hiera-mysql-backend-0.0.4

- old
+ new

@@ -32,36 +32,41 @@ next unless File.exist?(sqlfile) data = @cache.read(sqlfile, Hash, {}) do |datafile| YAML.load(datafile) end + mysql_host = data.fetch(:dbconfig, {}).fetch(:host, nil) || Config[:mysql2][:host] + mysql_user = data.fetch(:dbconfig, {}).fetch(:user, nil) || Config[:mysql2][:user] + mysql_pass = data.fetch(:dbconfig, {}).fetch(:pass, nil) || Config[:mysql2][:pass] + mysql_database = data.fetch(:dbconfig, {}).fetch(:database, nil) || Config[:mysql2][:database] + + connection_hash = { + :host => mysql_host, + :username => mysql_user, + :password => mysql_pass, + :database => mysql_database, + :reconnect => true} + + Hiera.debug("data #{data.inspect}") next if data.empty? next unless data.include?(key) Hiera.debug("Found #{key} in #{source}") new_answer = Backend.parse_answer(data[key], scope) - results = query(new_answer) + results = query(connection_hash, new_answer) end - return results + return results end - def query(query) + def query(connection_hash, query) Hiera.debug("Executing SQL Query: #{query}") data=nil - mysql_host = Config[:mysql2][:host] - mysql_user = Config[:mysql2][:user] - mysql_pass = Config[:mysql2][:pass] - mysql_database = Config[:mysql2][:database] - client = Mysql2::Client.new(:host => mysql_host, - :username => mysql_user, - :password => mysql_pass, - :database => mysql_database, - :reconnect => true) + client = Mysql2::Client.new(connection_hash) begin data = client.query(query).to_a Hiera.debug("Mysql Query returned #{data.size} rows") rescue => e Hiera.debug e.message