-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hiera does not return Array of first elements - only "first first" element #4
Comments
I have implemented a fix which works for me at least: # Class Mysql_backend
# Description: MySQL back end to Hiera.
# Author: Craig Dunn <[email protected]>
#
class Hiera
module Backend
class Mysql_backend
def initialize
begin
require 'mysql'
rescue LoadError
require 'rubygems'
require 'mysql'
end
Hiera.debug("mysql_backend initialized")
end
def lookup(key, scope, order_override, resolution_type)
Hiera.debug("mysql_backend invoked lookup")
Hiera.debug("resolution type is #{resolution_type}")
answer = nil
# Parse the mysql query from the config, we also pass in key
# to extra_data so this can be interpreted into the query
# string
#
queries = [ Config[:mysql][:query] ].flatten
queries.map! { |q| Backend.parse_string(q, scope, {"key" => key}) }
queries.each do |mysql_query|
results = query(mysql_query)
unless results.empty?
case resolution_type
when :array
answer ||= []
results.each do |ritem|
answer << Backend.parse_answer(ritem, scope)
end
else
#return single element if only one result
if results.size == 1
answer = Backend.parse_answer(results[0], scope)
else
#return all results as array - this is required to get all rows for a single query
answer ||= []
results.each do |ritem|
answer << Backend.parse_answer(ritem, scope)
end
end
#break for resolution_type which is not an array / stop on first find for priority type
break
end
end
end
answer
end
def query (sql)
Hiera.debug("Executing SQL Query: #{sql}")
data=[]
mysql_host=Config[:mysql][:host]
mysql_user=Config[:mysql][:user]
mysql_pass=Config[:mysql][:pass]
mysql_database=Config[:mysql][:database]
dbh = Mysql.new(mysql_host, mysql_user, mysql_pass, mysql_database)
dbh.reconnect = true
res = dbh.query(sql)
Hiera.debug("Mysql Query returned #{res.num_rows} rows")
# Currently we'll just return the first element of each row, a future
# enhancement would be to make this easily support hashes so you can do
# select foo,bar from table
#
if res.num_fields < 2
res.each do |row|
Hiera.debug("Mysql value : #{row[0]}")
data << row[0]
end
else
res.each_hash do |row|
data << row
end
end
Hiera.debug("Returning Data : #{data}")
return data
end
end
end
end |
You should commit this, it works for me at least. |
use hiera_array(key) |
Hi Craig, Thank you for writing hiera-mysql. Hi Christof, Thank you for your updated version. It works for me unless I only want one entry in an array. I am testing with NTP 'https://forge.puppetlabs.com/puppetlabs/ntp'. hiera -d ntp::serverspuppet agent -tSee the entry for servers in my common.yaml. And my MySQL test table. ntp::servers:
mysql> select * from data; In my hiera.yaml I have: :backends:
:mysql: Every test is perfect unless I only have one entry in the DB. The MySQL Puppet run fails as it gets a string when it is expecting an array! Yet the yaml tests all work even when there is only one ntp server. At the moment I have a version of hiera-mysql that works for my requirements. The following are a suggestion to give some ideas. For the moment I am steadily improving my knowledge on Puppet, Hiera & Ruby. I do not know if it would be possible use a table like the one below. Where 'type' = 'string'|'array'|'hash'. Then automatically add 'type' to the queries, Query the DB then extract the 'type' from the results to determine how to return the data. SELECT type,val FROM data WHERE var='%{key}' CREATE TABLE Please get in touch if I can help… |
When calling hiera-mysql only the the first entry from a single select is returned.
My Debug output is:
For the last line I would expect:
172.18.0.19 172.18.0.28
My Create Table Statement is:
And my hiera.yaml:
The text was updated successfully, but these errors were encountered: