From c3b006a449bcf3f6d9afb4de31499c865748748a Mon Sep 17 00:00:00 2001 From: Marcus Stoegbauer Date: Sun, 20 Jul 2014 08:48:50 +0200 Subject: Added support for custom SQL databases. Works as expected for sqlite, except config option "file" got renamed to "database", added authentication options for other SQL adapters and an optional query argument to overwrite the generic query. config snipplet: sql: adapter: mysql2 user: sqluser password: sekrit host: mysql.local database: inventory table: devices query: SELECT `fqhn` AS `name`, lower(`vendor`) AS `model` FROM `devices` INNER JOIN `devtypes` ON (`devtypes`.`id` = `devices`.`model_id`) WHERE (`aktiv` = 'J') map: name: name model: model --- lib/oxidized/source/sql.rb | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/lib/oxidized/source/sql.rb b/lib/oxidized/source/sql.rb index c744e31..c65d335 100644 --- a/lib/oxidized/source/sql.rb +++ b/lib/oxidized/source/sql.rb @@ -23,18 +23,28 @@ class SQL < Source end end + def connect + #begin + #require @cfg.adapter + #rescue LoadError + #raise OxidizedError, "@{cfg.adapter} not found: install gem" + #end + Sequel.connect(:adapter => @cfg.adapter, + :host => @cfg.host, + :user => @cfg.user, + :password => @cfg.password, + :database => @cfg.database) + end + def load nodes = [] - db = case @cfg.adapter - when 'sqlite' - begin - require 'sqlite3' - rescue LoadError - raise OxidizedError, 'sqlite3 not found: sudo apt install libsqlite3-dev; sudo gem install sqlite3' - end - Sequel.sqlite @cfg.file + db = connect + if @cfg.query? + query = db[@cfg.table.to_sym].with_sql(@cfg.query) + else + query = db[@cfg.table.to_sym] end - db[@cfg.table.to_sym].each do |node| + query.each do |node| # map node parameters keys = {} @cfg.map.each { |key, sql_column| keys[key.to_sym] = node[sql_column.to_sym] } -- cgit v1.2.1