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
require File.join(File.dirname(__FILE__), 'services')
module Noah
class Host < Model
# Host model
# @return {Host} a {Host} object
include Taggable
include Linkable
attribute :name
attribute :status
collection :services, Service
index :name
index :status
index :services
def validate
super
assert_present :name
assert_present :status
assert_unique :name
assert_member :status, ["up","down","pending"]
end
# @return [Hash] A hash representation of a {Host}
def to_hash
services_hash = Hash.new
services.sort.each do |svc|
services_hash["#{svc.name}"] = svc.status
end
h = {:name => name, :status => status, :created_at => created_at, :updated_at => updated_at, :services => services_hash}
super.merge(h)
end
class << self
def find_or_create(opts = {})
begin
h = find(:name => opts[:name]).first
#h = find(opts.reject{|key,value| key == :status}).first
h.nil? ? host=new(opts) : host=h
host.status = opts[:status]
if host.valid?
host.save
end
host
rescue Exception => e
e.message
end
end
end
end
class Hosts
# @param [Hash] optional filters for results
# @return [Array] Array of {Host} objects
def self.all(options = {})
host_hash = Hash.new
options.empty? ? hosts=Noah::Host.all.sort : hosts=Noah::Host.find(options).sort
hosts.each {|x| host_hash["#{x.name}"] = x.to_hash.reject {|k,v| k == :name } }
host_hash
end
end
end