Class: Houcho::OuterRole

Inherits:
Element show all
Includes:
Attribute
Defined in:
lib/houcho/outerrole.rb,
lib/houcho/outerrole/save.rb,
lib/houcho/outerrole/yabitz.rb,
lib/houcho/outerrole/cloudforecast.rb

Defined Under Namespace

Modules: Save Classes: CloudForecast, Yabitz

Instance Method Summary collapse

Methods included from Attribute

#attr_id, #del_attr, #get_attr, #get_attr_json, #set_attr, #set_attr!

Methods inherited from Element

#attach, #detach, #detach_from_all, #id

Constructor Details

#initializeOuterRole

Returns a new instance of OuterRole.



10
11
12
13
# File 'lib/houcho/outerrole.rb', line 10

def initialize
  super("outerrole")
  @type_id = 1
end

Instance Method Details

#details(outer_role) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/houcho/outerrole.rb', line 30

def details(outer_role)
  outer_role = outer_role.is_a?(Array) ? outer_role : [outer_role]
  result = {}
  outer_role.each do |role|
    hosts = hostlist(role)
    if !hosts.empty?
      result[role] = {}
      result[role]["host"] = hosts
    end
  end

  result
end

#hostlist(outer_role) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/houcho/outerrole.rb', line 45

def hostlist(outer_role)
  outer_role = outer_role.is_a?(Array) ? outer_role : [outer_role]
  hosts = []

  outer_role.each do |role|
    id = id(role)
    hosts << @db.execute("
      SELECT host.name
      FROM host
      JOIN outerrole_host ORHOST
      ON host.id = ORHOST.host_id
      WHERE ORHOST.outerrole_id = ?
    ", id)
  end

  hosts.flatten.uniq.sort
end

#list(opt = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/houcho/outerrole.rb', line 16

def list(opt = {})
  sql = "SELECT T1.name FROM outerrole T1, role_outerrole T2"
  sql += " WHERE T1.data_source LIKE ?"
  ds = opt[:data_source]

  if opt[:role_id]
    sql += " AND T1.id = T2.outerrole_id AND T2.role_id = ?"
    return @db.execute(sql, ds ? ds : "%", opt[:role_id]).flatten
  else
    return @db.execute(sql, ds ? ds : "%").flatten
  end
end