Class: Oxidized::Source::SQL

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

Instance Method Summary collapse

Instance Method Details

#load(node_want = nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/oxidized/source/sql.rb', line 27

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:



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/oxidized/source/sql.rb', line 10

def setup
  if @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 #{Oxidized::Config.configfile}"
  end

  # map.name is mandatory
  return if @cfg.map.has_key?('name')

  raise InvalidConfig, "map/name is a mandatory source attribute, edit #{Oxidized::Config.configfile}"
end