Class: Oxidized::SQL

Inherits:
Source show all
Defined in:
lib/oxidized/source/sql.rb

Instance Method Summary collapse

Methods inherited from Source

#map_group, #map_model, #node_var_interpolate

Instance Method Details

#load(node_want = nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/oxidized/source/sql.rb', line 21

def load(node_want = nil)
  nodes = []
  db = connect
  query = db[@cfg.table.to_sym]
  query = query.with_sql(@cfg.query) if @cfg.query?

  query = query.where(@cfg.map.name.to_sym => node_want) if node_want

  query.each do |node|
    # map node parameters
    keys = {}
    @cfg.map.each { |key, sql_column| keys[key.to_sym] = node_var_interpolate node[sql_column.to_sym] }
    keys[:model] = map_model keys[:model] if keys.has_key? :model
    keys[:group] = map_group keys[:group] if keys.has_key? :group

    # map node specific vars
    vars = {}
    @cfg.vars_map.each do |key, sql_column|
      vars[key.to_sym] = node_var_interpolate node[sql_column.to_sym]
    end
    keys[:vars] = vars unless vars.empty?

    nodes << keys
  end
  db.disconnect
  nodes
end

#setupObject

Raises:



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/oxidized/source/sql.rb', line 9

def setup
  return unless @cfg.empty?

  Oxidized.asetus.user.source.sql.adapter   = 'sqlite'
  Oxidized.asetus.user.source.sql.database  = File.join(Config::ROOT, 'sqlite.db')
  Oxidized.asetus.user.source.sql.table     = 'devices'
  Oxidized.asetus.user.source.sql.map.name  = 'name'
  Oxidized.asetus.user.source.sql.map.model = 'rancid'
  Oxidized.asetus.save :user
  raise NoConfig, 'no source sql config, edit ~/.config/oxidized/config'
end