1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
module Noah
class Service < Model
include Taggable
include Linkable
attribute :name
attribute :status
reference :host, Host
index :name
index :status
def validate
super
assert_present :name
assert_present :status
assert_present :host_id
assert_unique [:name, :host_id]
assert_member :status, ["up", "down", "pending"]
end
def to_hash
Host[host_id].nil? ? host_name=nil : host_name=Host[host_id].name
super.merge(:name => name, :status => status, :updated_at => updated_at, :host => host_name)
end
class << self
def find_or_create(opts = {})
begin
# convert passed host object to host_id if passed
if opts.has_key?(:host)
opts.merge!({:host_id => opts[:host].id})
opts.reject!{|key, value| key == :host}
end
# exclude requested status from lookup
s = find(:name => opts[:name], :host_id => opts[:host_id]).first
s.nil? ? service=new(opts) : service=s
service.status = opts[:status]
if service.valid?
service.save
end
service
rescue Exception => e
e.message
end
end
end
end
class Services
def self.all(options = {})
service_hash = Hash.new
options.empty? ? services=Service.all.sort : services=Service.find(options).sort
services.each do |svc|
service_hash["#{svc.name}"] = Hash.new unless service_hash.has_key?(svc.name)
host_name = Noah::Host[svc.host_id].name
service_hash["#{svc.name}"]["#{host_name}"] = Hash.new
service_hash["#{svc.name}"]["#{host_name}"].merge!({:id => svc.id, :status => svc.status, :tags => svc.to_hash[:tags], :links => svc.to_hash[:links], :created_at => svc.created_at, :updated_at => svc.updated_at})
end
service_hash
end
end
end