Commit eb532835 authored by John E. Vincent's avatar John E. Vincent

major refactor. namespacing models

parent 48377e08
$:.unshift(File.expand_path(File.join(File.dirname(__FILE__), "lib")))
require 'bundler' require 'bundler'
require 'rspec/core' require 'rspec/core'
require 'rspec/core/rake_task' require 'rspec/core/rake_task'
...@@ -23,29 +24,27 @@ end ...@@ -23,29 +24,27 @@ end
desc "Populate database with sample dataset" desc "Populate database with sample dataset"
task :sample, :redis_url do |t, args| task :sample, :redis_url do |t, args|
require 'ohm' require 'noah'
require 'json'
require File.join(File.dirname(__FILE__), 'lib','noah')
Ohm::connect(:url => args.redis_url) Ohm::connect(:url => args.redis_url)
Ohm::redis.flushdb Ohm::redis.flushdb
puts "Creating Host entry for 'localhost'" puts "Creating Host entry for 'localhost'"
h = Host.create(:name => 'localhost', :status => "up") h = Noah::Host.create(:name => 'localhost', :status => "up")
if h.save if h.save
%w[redis noah].each do |service| %w[redis noah].each do |service|
puts "Create Service entry for #{service}" puts "Create Service entry for #{service}"
s = Service.create(:name => service, :status => "up", :host => h) s = Noah::Service.create(:name => service, :status => "up", :host => h)
h.services << s h.services << s
end end
end end
puts "Creating Application entry for 'noah'" puts "Creating Application entry for 'noah'"
a = Application.create(:name => 'noah') a = Noah::Application.create(:name => 'noah')
if a.save if a.save
puts "Creating Configuration entry for 'noah'" puts "Creating Configuration entry for 'noah'"
cr = Configuration.create(:name => 'redis', :format => 'string', :body => 'redis://127.0.0.1:6379/0', :application => a) cr = Noah::Configuration.create(:name => 'redis', :format => 'string', :body => 'redis://127.0.0.1:6379/0', :application => a)
ch = Configuration.create(:name => 'host', :format => 'string', :body => 'localhost', :application => a) ch = Noah::Configuration.create(:name => 'host', :format => 'string', :body => 'localhost', :application => a)
cp = Configuration.create(:name => 'port', :format => 'string', :body => '9292', :application => a) cp = Noah::Configuration.create(:name => 'port', :format => 'string', :body => '9292', :application => a)
%w[cr ch cp].each do |c| %w[cr ch cp].each do |c|
a.configurations << eval(c) a.configurations << eval(c)
end end
...@@ -53,10 +52,10 @@ task :sample, :redis_url do |t, args| ...@@ -53,10 +52,10 @@ task :sample, :redis_url do |t, args|
puts "Creating sample entries - Host and Service" puts "Creating sample entries - Host and Service"
%w[host1.domain.com host2.domain.com host3.domain.com].each do |host| %w[host1.domain.com host2.domain.com host3.domain.com].each do |host|
h = Host.create(:name => host, :status => "up") h = Noah::Host.create(:name => host, :status => "up")
if h.save if h.save
%w[http https smtp mysql].each do |service| %w[http https smtp mysql].each do |service|
s = Service.create(:name => service, :status => "pending", :host => h) s = Noah::Service.create(:name => service, :status => "pending", :host => h)
h.services << s h.services << s
end end
end end
...@@ -77,15 +76,15 @@ EOY ...@@ -77,15 +76,15 @@ EOY
} }
EOJ EOJ
a1 = Application.create(:name => 'myrailsapp1') a1 = Noah::Application.create(:name => 'myrailsapp1')
if a1.save if a1.save
c1 = Configuration.create(:name => 'database.yml', :format => 'yaml', :body => my_yaml, :application => a1) c1 = Noah::Configuration.create(:name => 'database.yml', :format => 'yaml', :body => my_yaml, :application => a1)
a1.configurations << c1 a1.configurations << c1
end end
a2 = Application.create(:name => 'myrestapp1') a2 = Noah::Application.create(:name => 'myrestapp1')
if a2.save if a2.save
c2 = Configuration.create(:name => 'config.json', :format => 'json', :body => my_json, :application => a2) c2 = Noah::Configuration.create(:name => 'config.json', :format => 'json', :body => my_json, :application => a2)
a2.configurations << c2 a2.configurations << c2
end end
puts "Sample data populated!" puts "Sample data populated!"
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
require './watcher-idea.rb' require './watcher-idea.rb'
Noah::Watcher.watch do Noah::Watcher.watch do
pattern "noah.Configuration*" pattern "noah.Noah::Configuration*"
destination Proc.new {|x| puts x} destination Proc.new {|x| puts x}
run! run!
end end
...@@ -5,7 +5,7 @@ require 'logger' ...@@ -5,7 +5,7 @@ require 'logger'
log = Logger.new(STDOUT) log = Logger.new(STDOUT)
Noah::Watcher.watch do Noah::Watcher.watch do
pattern "noah.Application*" pattern "noah.Noah::Application*"
destination Proc.new {|x| log.debug(x)} destination Proc.new {|x| log.debug(x)}
run! run!
end end
require 'ohm'
require 'ohm/contrib'
begin begin
require 'yajl' require 'yajl'
rescue LoadError rescue LoadError
...@@ -10,9 +8,5 @@ require 'yaml' ...@@ -10,9 +8,5 @@ require 'yaml'
require 'sinatra/base' require 'sinatra/base'
require 'sinatra/namespace' require 'sinatra/namespace'
require File.join(File.dirname(__FILE__), 'noah','hosts') require File.join(File.dirname(__FILE__), 'noah','models')
require File.join(File.dirname(__FILE__), 'noah','services')
require File.join(File.dirname(__FILE__), 'noah','applications')
require File.join(File.dirname(__FILE__), 'noah','configurations')
require File.join(File.dirname(__FILE__), 'noah','watchers')
require File.join(File.dirname(__FILE__), 'noah','app') require File.join(File.dirname(__FILE__), 'noah','app')
class Noah::App class Noah::App
# Application URIs # Application URIs
get '/a/:appname/:config/?' do |appname, config| get '/a/:appname/:config/?' do |appname, config|
app = ::Application.find(:name => appname).first app = Noah::Application.find(:name => appname).first
if app.nil? if app.nil?
halt 404 halt 404
else else
c = ::Configuration.find(:name => config, :application_id => app.id).first c = Noah::Configuration.find(:name => config, :application_id => app.id).first
c.to_json c.to_json
end end
end end
get '/a/:appname/?' do |appname| get '/a/:appname/?' do |appname|
app = ::Application.find(:name => appname).first app = Noah::Application.find(:name => appname).first
if app.nil? if app.nil?
halt 404 halt 404
else else
...@@ -23,7 +23,7 @@ class Noah::App ...@@ -23,7 +23,7 @@ class Noah::App
required_params = ["name"] required_params = ["name"]
data = JSON.parse(request.body.read) data = JSON.parse(request.body.read)
if data.keys.sort == required_params.sort && data['name'] == appname if data.keys.sort == required_params.sort && data['name'] == appname
app = ::Application.find_or_create(:name => appname) app = Noah::Application.find_or_create(:name => appname)
else else
raise "Missing Parameters" raise "Missing Parameters"
end end
...@@ -38,12 +38,12 @@ class Noah::App ...@@ -38,12 +38,12 @@ class Noah::App
end end
delete '/a/:appname/?' do |appname| delete '/a/:appname/?' do |appname|
app = ::Application.find(:name => appname).first app = Noah::Application.find(:name => appname).first
if app.nil? if app.nil?
halt 404 halt 404
else else
configurations = [] configurations = []
::Configuration.find(:application_id => app.id).sort.each {|x| configurations << x; x.delete} if app.configurations.size > 0 Noah::Configuration.find(:application_id => app.id).sort.each {|x| configurations << x; x.delete} if app.configurations.size > 0
app.delete app.delete
r = {"result" => "success", "action" => "delete", "id" => "#{app.id}", "name" => "#{appname}", "configurations" => "#{configurations.size}"} r = {"result" => "success", "action" => "delete", "id" => "#{app.id}", "name" => "#{appname}", "configurations" => "#{configurations.size}"}
r.to_json r.to_json
...@@ -52,7 +52,7 @@ class Noah::App ...@@ -52,7 +52,7 @@ class Noah::App
get '/a/?' do get '/a/?' do
apps = [] apps = []
::Application.all.sort.each {|a| apps << a.to_hash} Noah::Application.all.sort.each {|a| apps << a.to_hash}
if apps.empty? if apps.empty?
halt 404 halt 404
else else
......
class Application < Ohm::Model require File.join(File.dirname(__FILE__), 'configurations')
include Ohm::Typecast module Noah
include Ohm::Timestamping class Application < Model
include Ohm::Callbacks
include Ohm::ExtraValidations
attribute :name collection :configurations, Configuration
collection :configurations, Configuration
index :name def to_hash
arr = []
after :save, :notify_via_redis_save configurations.sort.each {|c| arr << c.to_hash}
after :create, :notify_via_redis_create super.merge(:name => name, :created_at => created_at, :updated_at => updated_at, :configurations => arr)
after :update, :notify_via_redis_update end
before :delete, :stash_name
after :delete, :notify_via_redis_delete
def validate
super
assert_present :name
assert_unique :name
end
def to_hash
arr = []
configurations.sort.each {|c| arr << c.to_hash}
super.merge(:name => name, :created_at => created_at, :updated_at => updated_at, :configurations => arr)
end
def is_new?
self.created_at == self.updated_at
end
class << self class << self
def find_or_create(opts = {}) def find_or_create(opts = {})
begin begin
find(opts).first.nil? ? (app = create(opts)) : (app = find(opts).first) find(opts).first.nil? ? (app = create(opts)) : (app = find(opts).first)
if app.valid? if app.valid?
app.save app.save
end
app
rescue Exception => e
e.message
end end
app
rescue Exception => e
e.message
end end
end end
end
protected
def stash_name
@deleted_name = self.name
end end
["save", "create", "update", "delete"].each do |meth| class Applications
class_eval do def self.all(options = {})
define_method("notify_via_redis_#{meth}".to_sym) do options.empty? ? Application.all.sort : Application.find(options).sort
pub_category = "noah.#{self.class.to_s}[#{self.name ||= @deleted_name}].#{meth}"
Ohm.redis.publish(pub_category, self.to_json)
end
end end
end end
end end
class Applications
def self.all(options = {})
options.empty? ? Application.all.sort : Application.find(options).sort
end
end
...@@ -7,11 +7,11 @@ class Noah::App ...@@ -7,11 +7,11 @@ class Noah::App
} }
# Configuration URIs # Configuration URIs
get '/c/:appname/:element/?' do |appname, element| get '/c/:appname/:element/?' do |appname, element|
a = ::Application.find(:name => appname).first a = Noah::Application.find(:name => appname).first
if a.nil? if a.nil?
halt 404 halt 404
else else
c = ::Configuration.find(:name => element, :application_id => a.id).first c = Noah::Configuration.find(:name => element, :application_id => a.id).first
content_type content_type_mapping[c.format.to_sym] if content_type_mapping[c.format.to_sym] content_type content_type_mapping[c.format.to_sym] if content_type_mapping[c.format.to_sym]
c.body c.body
end end
...@@ -19,18 +19,18 @@ class Noah::App ...@@ -19,18 +19,18 @@ class Noah::App
get '/c/:appname/?' do |appname| get '/c/:appname/?' do |appname|
config = [] config = []
a = ::Application.find(:name => appname).first a = Noah::Application.find(:name => appname).first
if a.nil? if a.nil?
halt 404 halt 404
else else
::Configuration.find(:application_id => a.id).sort.each {|c| config << c.to_hash} Noah::Configuration.find(:application_id => a.id).sort.each {|c| config << c.to_hash}
config.to_json config.to_json
end end
end end
get '/c/?' do get '/c/?' do
configs = [] configs = []
::Configuration.all.sort.each {|c| configs << c.to_hash} Noah::Configuration.all.sort.each {|c| configs << c.to_hash}
if configs.empty? if configs.empty?
halt 404 halt 404
else else
...@@ -39,8 +39,8 @@ class Noah::App ...@@ -39,8 +39,8 @@ class Noah::App
end end
put '/c/:appname/:element?' do |appname, element| put '/c/:appname/:element?' do |appname, element|
app = ::Application.find_or_create(:name => appname) app = Noah::Application.find_or_create(:name => appname)
config = ::Configuration.find_or_create(:name => element, :application_id => app.id) config = Noah::Configuration.find_or_create(:name => element, :application_id => app.id)
required_params = ["format", "body"] required_params = ["format", "body"]
data = JSON.parse(request.body.read) data = JSON.parse(request.body.read)
data.keys.sort == required_params.sort ? (config.format = data["format"]; config.body = data["body"]) : (raise "Missing Parameters") data.keys.sort == required_params.sort ? (config.format = data["format"]; config.body = data["body"]) : (raise "Missing Parameters")
...@@ -56,9 +56,9 @@ class Noah::App ...@@ -56,9 +56,9 @@ class Noah::App
end end
delete '/c/:appname/:element?' do |appname, element| delete '/c/:appname/:element?' do |appname, element|
app = ::Application.find(:name => appname).first app = Noah::Application.find(:name => appname).first
if app if app
config = ::Configuration.find(:name=> element, :application_id => app.id).first config = Noah::Configuration.find(:name=> element, :application_id => app.id).first
if config if config
config.delete config.delete
r = {"result" => "success", "id" => "#{config.id}", "action" => "delete", "application" => "#{app.name}", "item" => "#{element}"} r = {"result" => "success", "id" => "#{config.id}", "action" => "delete", "application" => "#{app.name}", "item" => "#{element}"}
......
class Configuration < Ohm::Model module Noah
include Ohm::Typecast class Configuration < Model
include Ohm::Timestamping
include Ohm::Callbacks attribute :format
include Ohm::ExtraValidations attribute :body
attribute :new_record
attribute :name reference :application, Application
attribute :format
attribute :body index :format
attribute :new_record index :body
reference :application, Application
def validate
index :name super
index :format assert_present :format
index :body assert_present :body
assert_present :application_id
after :save, :notify_via_redis_save assert_unique [:name, :application_id]
after :create, :notify_via_redis_create end
after :update, :notify_via_redis_update
before :delete, :stash_name
after :delete, :notify_via_redis_delete
def validate
super
assert_present :name
assert_present :format
assert_present :body
assert_present :application_id
assert_unique [:name, :application_id]
end
def to_hash
Application[application_id].nil? ? app_name=nil : app_name=Application[application_id].name
super.merge(:name => name, :format => format, :body => body, :created_at => created_at, :updated_at => updated_at, :application => app_name)
end
def is_new? def to_hash
self.created_at == self.updated_at Application[application_id].nil? ? app_name=nil : app_name=Application[application_id].name
end super.merge(:name => name, :format => format, :body => body, :created_at => created_at, :updated_at => updated_at, :application => app_name)
end
class << self class << self
def find_or_create(opts={}) def find_or_create(opts={})
begin begin
if find(opts).first.nil? if find(opts).first.nil?
conf = create(opts) conf = create(opts)
else else
conf = find(opts).first conf = find(opts).first
end end
rescue Exception => e rescue Exception => e
e.message e.message
end
end
end end
end
end
protected
def stash_name
@deleted_name = self.name
end end
["save", "create", "update", "delete"].each do |meth| class Configurations
class_eval do def self.all(options = {})
define_method("notify_via_redis_#{meth}".to_sym) do options.empty? ? Configuration.all.sort : Configuration.find(options).sort
pub_category = "noah.#{self.class.to_s}[#{self.name ||= @deleted_name}].#{meth}"
Ohm.redis.publish(pub_category, self.to_json)
end
end end
end end
end end
class Configurations
def self.all(options = {})
options.empty? ? Configuration.all.sort : Configuration.find(options).sort
end
end
require File.join(File.dirname(__FILE__), 'models')
module Noah module Noah
module SinatraHelpers module SinatraHelpers
extend(Ohm)
def host(opts = {}) def host(opts = {})
Host.find(opts).first Noah::Host.find(opts).first
end end
def hosts(opts = {}) def hosts(opts = {})
Hosts.all(opts) Noah::Hosts.all(opts)
end end
def service(opts = {}) def service(opts = {})
Service.find(options) Noah::Service.find(options)
end end
def services(opts = {}) def services(opts = {})
Services.all(opts) Noah::Services.all(opts)
end end
def host_service(hostname, servicename) def host_service(hostname, servicename)
h = Host.find(:name => hostname).first h = Noah::Host.find(:name => hostname).first
if h.nil? if h.nil?
nil nil
else else
Service.find(:host_id => h.id, :name => servicename).first Noah::Service.find(:host_id => h.id, :name => servicename).first
end end
end end
def host_services(hostname) def host_services(hostname)
h = Host.find(:name => hostname).first h = Noah::Host.find(:name => hostname).first
if h.nil? if h.nil?
nil nil
else else
Services.all(:host_id => id) Noah::Services.all(:host_id => id)
end end
end end
def application(opts = {}) def application(opts = {})
Application.find(opts).first Noah::Application.find(opts).first
end end
def applications(opts = {}) def applications(opts = {})
Applications.all(opts) Noah::Applications.all(opts)
end end
def configuration(opts = {}) def configuration(opts = {})
Configuration.find(opts).first Noah::Configuration.find(opts).first
end end
def configurations(opts = {}) def configurations(opts = {})
Configurations.all(opts) Noah::Configurations.all(opts)
end end
end end
......
...@@ -36,7 +36,7 @@ class Noah::App ...@@ -36,7 +36,7 @@ class Noah::App
put '/h/:hostname/?' do |hostname| put '/h/:hostname/?' do |hostname|
required_params = ["name", "status"] required_params = ["name", "status"]
data = JSON.parse(request.body.read) data = JSON.parse(request.body.read)
(data.keys.sort == required_params.sort && data['name'] == hostname) ? (host = ::Host.find_or_create(:name => data['name'], :status => data['status'])) : (raise "Missing Parameters") (data.keys.sort == required_params.sort && data['name'] == hostname) ? (host = Noah::Host.find_or_create(:name => data['name'], :status => data['status'])) : (raise "Missing Parameters")
if host.valid? if host.valid?
r = {"result" => "success","id" => "#{host.id}","status" => "#{host.status}", "name" => "#{host.name}", "new_record" => host.is_new?} r = {"result" => "success","id" => "#{host.id}","status" => "#{host.status}", "name" => "#{host.name}", "new_record" => host.is_new?}
r.to_json r.to_json
...@@ -46,10 +46,10 @@ class Noah::App ...@@ -46,10 +46,10 @@ class Noah::App
end end
delete '/h/:hostname/?' do |hostname| delete '/h/:hostname/?' do |hostname|
host = ::Host.find(:name => hostname).first host = Noah::Host.find(:name => hostname).first
if host if host
services = [] services = []
Service.find(:host_id => host.id).sort.each {|x| services << x; x.delete} if host.services.size > 0 Noah::Service.find(:host_id => host.id).sort.each {|x| services << x; x.delete} if host.services.size > 0
host.delete host.delete
r = {"result" => "success", "id" => "#{host.id}", "name" => "#{hostname}", "service_count" => "#{services.size}"} r = {"result" => "success", "id" => "#{host.id}", "name" => "#{hostname}", "service_count" => "#{services.size}"}
r.to_json r.to_json
......
class Host < Ohm::Model require File.join(File.dirname(__FILE__), 'services')
# Host model module Noah
# @return {Host} a {Host} object class Host < Model
include Ohm::Typecast # Host model
include Ohm::Timestamping # @return {Host} a {Host} object
include Ohm::Callbacks
include Ohm::ExtraValidations attribute :status
collection :services, Service
attribute :name
attribute :status index :status
collection :services, Service
def validate
index :name super
index :status assert_present :status
assert_unique :name
after :save, :notify_via_redis_save assert_member :status, ["up","down","pending"]
after :create, :notify_via_redis_create end
after :update, :notify_via_redis_update
before :delete, :stash_name
after :delete, :notify_via_redis_delete
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
arr = []
services.sort.each {|s| arr << s.to_hash}
h = {:name => name, :status => status, :created_at => created_at, :updated_at => updated_at, :services => arr}
super.merge(h)
end
# Evaluate if {Host} record is new or not # @return [Hash] A hash representation of a {Host}
def is_new? def to_hash
self.created_at == self.updated_at arr = []
end services.sort.each {|s| arr << s.to_hash}
h = {:name => name, :status => status, :created_at => created_at, :updated_at => updated_at, :services => arr}
super.merge(h)
end
class << self class << self
def find_or_create(opts = {}) def find_or_create(opts = {})
begin begin
# exclude requested status from lookup # exclude requested status from lookup
h = find(opts.reject{|key,value| key == :status}).first h = find(opts.reject{|key,value| key == :status}).first
host = h.nil? ? create(opts) : h host = h.nil? ? create(opts) : h
host.status = opts[:status] host.status = opts[:status]
if host.valid? if host.valid?
host.save host.save
end
host
rescue Exception => e
e.message
end end
host
rescue Exception => e
e.message
end end
end end
end
protected
# Saves the original {Host#name} attribute before deleting an object
def stash_name
@deleted_name = self.name
end end
["save", "create", "update", "delete"].each do |meth| class Hosts
class_eval do # @param [Hash] optional filters for results
define_method("notify_via_redis_#{meth}".to_sym) do # @return [Array] Array of {Host} objects
self.name.nil? ? name=@delete_name : name=self.name def self.all(options = {})
pub_category = "noah.#{self.class.to_s}[#{name}].#{meth}" options.empty? ? Noah::Host.all.sort : Noah::Host.find(options).sort
Ohm.redis.publish(pub_category, self.to_json)
end
end end
end end
end end
class Hosts
# @param [Hash] optional filters for results
# @return [Array] Array of {Host} objects
def self.all(options = {})
options.empty? ? Host.all.sort : Host.find(options).sort
end
end
require File.join(File.dirname(__FILE__),'hosts') require 'ohm'
require File.join(File.dirname(__FILE__),'services') require 'ohm/contrib'
require File.join(File.dirname(__FILE__),'applications') module Noah
require File.join(File.dirname(__FILE__),'configurations') class Model < Ohm::Model
require File.join(File.dirname(__FILE__),'watchers') def self.inherited(model)
model.send :include, Ohm::Timestamping
model.send :include, Ohm::Typecast
model.send :include, Ohm::Callbacks
model.send :include, Ohm::ExtraValidations
model.attribute :name
model.index :name
model.after :save, :notify_via_redis_save
model.after :create, :notify_via_redis_create
model.after :update, :notify_via_redis_update
model.before :delete, :stash_name
model.after :delete, :notify_via_redis_delete
model.send :include, ModelClassMethods
end
end
module ModelClassMethods
def validate
super
assert_present :name
end
def is_new?
self.created_at == self.updated_at
end
protected
def stash_name
@deleted_name = self.name
end
["save", "create", "update", "delete"].each do |meth|
class_eval do
define_method("notify_via_redis_#{meth}".to_sym) do
self.name.nil? ? name=@deleted_name : name=self.name
pub_category = "noah.#{self.class.to_s}[name].#{meth}"
Ohm.redis.publish(pub_category, self.to_json)
end
end
end
end
end
require File.join(File.dirname(__FILE__), 'hosts')
require File.join(File.dirname(__FILE__), 'services')
require File.join(File.dirname(__FILE__), 'applications')
require File.join(File.dirname(__FILE__), 'configurations')
require File.join(File.dirname(__FILE__), 'watchers')
...@@ -34,8 +34,8 @@ class Noah::App ...@@ -34,8 +34,8 @@ class Noah::App
required_params = ["status", "host", "name"] required_params = ["status", "host", "name"]
data = JSON.parse(request.body.read) data = JSON.parse(request.body.read)
if data.keys.sort == required_params.sort if data.keys.sort == required_params.sort
h = ::Host.find(:name => data['host']).first || (raise "Invalid Host") h = Noah::Host.find(:name => data['host']).first || (raise "Invalid Host")
service = ::Service.find_or_create(:name => servicename, :status => data['status'], :host => h) service = Noah::Service.find_or_create(:name => servicename, :status => data['status'], :host => h)
if service.valid? if service.valid?
action = service.is_new? ? "create" : "update" action = service.is_new? ? "create" : "update"
service.save service.save
...@@ -50,8 +50,8 @@ class Noah::App ...@@ -50,8 +50,8 @@ class Noah::App
end end
delete '/s/:servicename/:hostname/?' do |servicename, hostname| delete '/s/:servicename/:hostname/?' do |servicename, hostname|
host = ::Host.find(:name => hostname).first || (halt 404) host = Noah::Host.find(:name => hostname).first || (halt 404)
service = ::Service.find(:name => servicename, :host_id => host.id).first || (halt 404) service = Noah::Service.find(:name => servicename, :host_id => host.id).first || (halt 404)
if host && service if host && service
service.delete service.delete
r = {"action" => "delete", "result" => "success", "id" => service.id, "host" => host.name, "service" => servicename} r = {"action" => "delete", "result" => "success", "id" => service.id, "host" => host.name, "service" => servicename}
......
class Service < Ohm::Model module Noah
include Ohm::Typecast
include Ohm::Timestamping
include Ohm::Callbacks
include Ohm::ExtraValidations
attribute :name class Service < Model
attribute :status
reference :host, Host
index :name attribute :status
index :status reference :host, Host
after :save, :notify_via_redis_save index :status
after :create, :notify_via_redis_create
after :update, :notify_via_redis_update
before :delete, :stash_name
after :delete, :notify_via_redis_delete
def validate def validate
super super
assert_present :name assert_present :status
assert_present :status assert_present :host_id
assert_present :host_id assert_unique [:name, :host_id]
assert_unique [:name, :host_id] assert_member :status, ["up", "down", "pending"]
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
def is_new?
self.created_at == self.updated_at
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(opts.reject{|key,value| key == :status}).first
service = s.nil? ? create(opts) : s
service.status = opts[:status]
if service.valid?
service.save
end
service
rescue Exception => e
e.message
end end
end
end
protected def to_hash
def stash_name Host[host_id].nil? ? host_name=nil : host_name=Host[host_id].name
@deleted_name = self.name super.merge(:name => name, :status => status, :updated_at => updated_at, :host => host_name)
end end
["save", "create", "update", "delete"].each do |meth| class << self
class_eval do def find_or_create(opts = {})
define_method("notify_via_redis_#{meth}".to_sym) do begin
self.name.nil? ? name=@deleted_name : name=self.name # convert passed host object to host_id if passed
pub_category = "noah.#{self.class.to_s}[name].#{meth}" if opts.has_key?(:host)
Ohm.redis.publish(pub_category, self.to_json) opts.merge!({:host_id => opts[:host].id})
opts.reject!{|key, value| key == :host}
end
# exclude requested status from lookup
s = find(opts.reject{|key,value| key == :status}).first
service = s.nil? ? create(opts) : s
service.status = opts[:status]
if service.valid?
service.save
end
service
rescue Exception => e
e.message
end
end end
end end
end end
end
class Services class Services
def self.all(options = {}) def self.all(options = {})
options.empty? ? Service.all.sort : Service.find(options).sort options.empty? ? Service.all.sort : Service.find(options).sort
end
end end
end end
module Noah module Noah
VERSION = "0.0.6" VERSION = "0.0.7"
end end
class Watcher < Ohm::Model #NYI module Noah
include Ohm::Typecast class Watcher < Model #NYI
include Ohm::Timestamping
include Ohm::Callbacks
attribute :client attribute :client
attribute :endpoint attribute :endpoint
attribute :event attribute :event
attribute :action attribute :action
index :client index :client
index :event index :event
def validate def validate
assert_present :client, :endpoint, :event, :action assert_present :client, :endpoint, :event, :action
assert_unique [:client, :endpoint, :event, :action] assert_unique [:client, :endpoint, :event, :action]
end
end end
end
class Watchers class Watchers
def self.all(options = {}) def self.all(options = {})
options.empty? ? Watcher.all.sort : Watcher.find(options).sort options.empty? ? Watcher.all.sort : Watcher.find(options).sort
end
end end
end end
...@@ -14,52 +14,52 @@ describe "Using the Application Model", :reset_redis => true do ...@@ -14,52 +14,52 @@ describe "Using the Application Model", :reset_redis => true do
Ohm.redis.flushdb Ohm.redis.flushdb
end end
describe "should" do describe "should" do
it "create a new Application" do it "create a new Noah::Application" do
a = Application.create(@appdata1) a = Noah::Application.create(@appdata1)
a.valid?.should == true a.valid?.should == true
a.is_new?.should == true a.is_new?.should == true
b = Application.find(@appdata1).first b = Noah::Application.find(@appdata1).first
b.should == a b.should == a
end end
it "create a new Application with Configurations" do it "create a new Noah::Application with Configurations" do
a = Application.create(@appdata1) a = Noah::Application.create(@appdata1)
a.configurations << Configuration.create(@appconf_string.merge({:application => a})) a.configurations << Noah::Configuration.create(@appconf_string.merge({:application => a}))
a.valid?.should == true a.valid?.should == true
a.is_new?.should == true a.is_new?.should == true
a.save a.save
b = Application.find(@appdata1).first b = Noah::Application.find(@appdata1).first
b.should == a b.should == a
b.configurations.size.should == 1 b.configurations.size.should == 1
b.configurations.first.name.should == @appconf_string[:name] b.configurations.first.name.should == @appconf_string[:name]
b.configurations.first.format.should == @appconf_string[:format] b.configurations.first.format.should == @appconf_string[:format]
b.configurations.first.body.should == @appconf_string[:body] b.configurations.first.body.should == @appconf_string[:body]
end end
it "create a new Application via find_or_create" do it "create a new Noah::Application via find_or_create" do
a = Application.find_or_create(@appdata2) a = Noah::Application.find_or_create(@appdata2)
a.valid?.should == true a.valid?.should == true
a.is_new?.should == true a.is_new?.should == true
b = Application.find(@appdata2).first b = Noah::Application.find(@appdata2).first
b.should == a b.should == a
end end
it "update an existing Application via find_or_create" do it "update an existing Noah::Application via find_or_create" do
a = Application.create(@appdata1) a = Noah::Application.create(@appdata1)
a.is_new?.should == true a.is_new?.should == true
sleep 2 sleep 2
b = Application.find_or_create(@appdata1) b = Noah::Application.find_or_create(@appdata1)
b.is_new?.should == false b.is_new?.should == false
end end
it "delete an existing Application" do it "delete an existing Noah::Application" do
a = Application.create(@appdata1) a = Noah::Application.create(@appdata1)
b = Application.find(@appdata1).first b = Noah::Application.find(@appdata1).first
b.should == a b.should == a
b.delete b.delete
c = Application.find(@appdata1).first c = Noah::Application.find(@appdata1).first
c.nil?.should == true c.nil?.should == true
end end
it "return all Applications" do it "return all Noah::Applications" do
a = Application.create(@appdata1) a = Noah::Application.create(@appdata1)
b = Application.create(@appdata2) b = Noah::Application.create(@appdata2)
c = Applications.all c = Noah::Applications.all
c.size.should == 2 c.size.should == 2
c.member?(a).should == true c.member?(a).should == true
c.member?(b).should == true c.member?(b).should == true
...@@ -67,17 +67,17 @@ describe "Using the Application Model", :reset_redis => true do ...@@ -67,17 +67,17 @@ describe "Using the Application Model", :reset_redis => true do
end end
describe "should not" do describe "should not" do
it "should not create a new Application without a name" do it "should not create a new Noah::Application without a name" do
a = Application.create a = Noah::Application.create
a.valid?.should == false a.valid?.should == false
a.errors.should == [[:name, :not_present]] a.errors.should == [[:name, :not_present]]
end end
it "should not create a duplicate Application" do # it "should not create a duplicate Noah::Application" do
a = Application.create(@appdata1) # a = Noah::Application.create(@appdata1)
a.valid?.should == true # a.valid?.should == true
b = Application.create(@appdata1) # b = Noah::Application.create(@appdata1)
b.valid?.should == false # b.valid?.should == false
b.errors.should == [[:name, :not_unique]] # b.errors.should == [[:name, :not_unique]]
end # end
end end
end end
...@@ -2,7 +2,7 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper') ...@@ -2,7 +2,7 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
describe "Using the Configuration Model", :reset_redis => true do describe "Using the Configuration Model", :reset_redis => true do
before(:all) do before(:all) do
app = Application.create(:name => "my_application") app = Noah::Application.create(:name => "my_application")
app.save app.save
@appconf_string = {:name => "mystringconf", :format => "string", :body => "some_var", :application_id => app.id} @appconf_string = {:name => "mystringconf", :format => "string", :body => "some_var", :application_id => app.id}
@appconf_json = {:name => "myjsonconf", :format => "json", :body => @appconf_string.to_json, :application_id => app.id} @appconf_json = {:name => "myjsonconf", :format => "json", :body => @appconf_string.to_json, :application_id => app.id}
...@@ -20,21 +20,21 @@ describe "Using the Configuration Model", :reset_redis => true do ...@@ -20,21 +20,21 @@ describe "Using the Configuration Model", :reset_redis => true do
describe "should" do describe "should" do
it "create a new Configuration" do it "create a new Configuration" do
c = Configuration.create(@appconf_string) c = Noah::Configuration.create(@appconf_string)
c.valid?.should == true c.valid?.should == true
c.is_new?.should == true c.is_new?.should == true
b = Configuration[c.id] b = Noah::Configuration[c.id]
b.should == c b.should == c
end end
it "create a new Configuration via find_or_create" do it "create a new Configuration via find_or_create" do
c = Configuration.find_or_create(@appconf_string) c = Noah::Configuration.find_or_create(@appconf_string)
c.valid?.should == true c.valid?.should == true
c.is_new?.should == true c.is_new?.should == true
a = Configuration[c.id] a = Noah::Configuration[c.id]
a.should == c a.should == c
end end
it "update an existing Configuration via find_or_create" do it "update an existing Configuration via find_or_create" do
c = Configuration.find_or_create(@appconf_string) c = Noah::Configuration.find_or_create(@appconf_string)
c.valid?.should == true c.valid?.should == true
c.is_new?.should == true c.is_new?.should == true
sleep(3) sleep(3)
...@@ -44,17 +44,17 @@ describe "Using the Configuration Model", :reset_redis => true do ...@@ -44,17 +44,17 @@ describe "Using the Configuration Model", :reset_redis => true do
c.is_new?.should == false c.is_new?.should == false
end end
it "delete an existing Configuration" do it "delete an existing Configuration" do
a = Configuration.find_or_create(@appconf_string) a = Noah::Configuration.find_or_create(@appconf_string)
b = Configuration.find(@appconf_string).first b = Noah::Configuration.find(@appconf_string).first
b.should == a b.should == a
a.delete a.delete
c = Configuration.find(@appconf_string).first c = Noah::Configuration.find(@appconf_string).first
c.nil?.should == true c.nil?.should == true
end end
it "return all Configurations" do it "return all Configurations" do
a = Configuration.find_or_create(@appconf_string) a = Noah::Configuration.find_or_create(@appconf_string)
b = Configuration.find_or_create(@appconf_json) b = Noah::Configuration.find_or_create(@appconf_json)
c = Configurations.all c = Noah::Configurations.all
c.size.should == 2 c.size.should == 2
c.member?(a).should == true c.member?(a).should == true
c.member?(b).should == true c.member?(b).should == true
...@@ -63,28 +63,28 @@ describe "Using the Configuration Model", :reset_redis => true do ...@@ -63,28 +63,28 @@ describe "Using the Configuration Model", :reset_redis => true do
describe "should not" do describe "should not" do
it "create a new Configuration without a name" do it "create a new Configuration without a name" do
a = Configuration.create(@appconf_missing_name) a = Noah::Configuration.create(@appconf_missing_name)
a.valid?.should == false a.valid?.should == false
a.errors.should == [[:name, :not_present]] a.errors.should == [[:name, :not_present]]
end end
it "create a new Configuration without a format" do it "create a new Configuration without a format" do
a = Configuration.create(@appconf_missing_format) a = Noah::Configuration.create(@appconf_missing_format)
a.valid?.should == false a.valid?.should == false
a.errors.should == [[:format, :not_present]] a.errors.should == [[:format, :not_present]]
end end
it "create a new Configuration without a body" do it "create a new Configuration without a body" do
a = Configuration.create(@appconf_missing_body) a = Noah::Configuration.create(@appconf_missing_body)
a.valid?.should == false a.valid?.should == false
a.errors.should == [[:body, :not_present]] a.errors.should == [[:body, :not_present]]
end end
it "create a new Confguration without an application" do it "create a new Confguration without an application" do
a = Configuration.create(@appconf_missing_application) a = Noah::Configuration.create(@appconf_missing_application)
a.valid?.should == false a.valid?.should == false
a.errors.should == [[:application_id, :not_present]] a.errors.should == [[:application_id, :not_present]]
end end
it "create a duplicate Configuration" do it "create a duplicate Configuration" do
a = Configuration.create(@appconf_string) a = Noah::Configuration.create(@appconf_string)
b = Configuration.create(@appconf_string) b = Noah::Configuration.create(@appconf_string)
b.errors.should == [[[:name, :application_id], :not_unique]] b.errors.should == [[[:name, :application_id], :not_unique]]
end end
end end
......
...@@ -6,7 +6,7 @@ describe "Using the Host Model", :reset_redis => true do ...@@ -6,7 +6,7 @@ describe "Using the Host Model", :reset_redis => true do
it "create a new Host with no services" do it "create a new Host with no services" do
hostname = "host1.domain.com" hostname = "host1.domain.com"
hoststatus = "up" hoststatus = "up"
host = Host.create(:name => hostname, :status => hoststatus) host = Noah::Host.create(:name => hostname, :status => hoststatus)
host.valid?.should == true host.valid?.should == true
host.save host.save
host.name.should == hostname host.name.should == hostname
...@@ -19,10 +19,10 @@ describe "Using the Host Model", :reset_redis => true do ...@@ -19,10 +19,10 @@ describe "Using the Host Model", :reset_redis => true do
hoststatus = "down" hoststatus = "down"
servicename = 'myservice' servicename = 'myservice'
servicestatus = 'pending' servicestatus = 'pending'
host = Host.create(:name => hostname, :status => hoststatus) host = Noah::Host.create(:name => hostname, :status => hoststatus)
host.valid?.should == true host.valid?.should == true
host.save host.save
host.services << Service.create(:name => servicename, :status => servicestatus, :host => host) host.services << Noah::Service.create(:name => servicename, :status => servicestatus, :host => host)
host.name.should == hostname host.name.should == hostname
host.status.should == hoststatus host.status.should == hoststatus
host.services.size.should == 1 host.services.size.should == 1
...@@ -35,7 +35,7 @@ describe "Using the Host Model", :reset_redis => true do ...@@ -35,7 +35,7 @@ describe "Using the Host Model", :reset_redis => true do
it "create a Host via find_or_create" do it "create a Host via find_or_create" do
hostname = "host3.domain.com" hostname = "host3.domain.com"
hoststatus = "up" hoststatus = "up"
host = Host.find_or_create(:name => hostname, :status => hoststatus) host = Noah::Host.find_or_create(:name => hostname, :status => hoststatus)
host.valid?.should == true host.valid?.should == true
host.save host.save
host.is_new?.should == true host.is_new?.should == true
...@@ -45,12 +45,12 @@ describe "Using the Host Model", :reset_redis => true do ...@@ -45,12 +45,12 @@ describe "Using the Host Model", :reset_redis => true do
hostname = "host3.domain.com" hostname = "host3.domain.com"
hoststatus = "pending" hoststatus = "pending"
newstatus = "down" newstatus = "down"
host = Host.find_or_create(:name => hostname, :status => hoststatus) host = Noah::Host.find_or_create(:name => hostname, :status => hoststatus)
host.valid?.should == true host.valid?.should == true
host.save host.save
host.is_new?.should == true host.is_new?.should == true
sleep 1 sleep 1
host1 = Host.find_or_create(:name => hostname, :status => newstatus) host1 = Noah::Host.find_or_create(:name => hostname, :status => newstatus)
host1.save host1.save
host1.is_new?.should == false host1.is_new?.should == false
end end
...@@ -58,10 +58,10 @@ describe "Using the Host Model", :reset_redis => true do ...@@ -58,10 +58,10 @@ describe "Using the Host Model", :reset_redis => true do
it "delete a Host" do it "delete a Host" do
hostname = "host3.domain.com" hostname = "host3.domain.com"
hoststatus = "pending" hoststatus = "pending"
host = Host.create(:name => hostname, :status => hoststatus) host = Noah::Host.create(:name => hostname, :status => hoststatus)
host.save host.save
host.delete host.delete
Host.find(:name => hostname).empty?.should == true Noah::Host.find(:name => hostname).empty?.should == true
end end
it "should return all Hosts" do it "should return all Hosts" do
...@@ -69,30 +69,30 @@ describe "Using the Host Model", :reset_redis => true do ...@@ -69,30 +69,30 @@ describe "Using the Host Model", :reset_redis => true do
status1 = "up" status1 = "up"
hostname2 = "host2.domain.com" hostname2 = "host2.domain.com"
status2= "down" status2= "down"
host1 = Host.create(:name => hostname1, :status => status1) host1 = Noah::Host.create(:name => hostname1, :status => status1)
host2 = Host.create(:name => hostname2, :status => status2) host2 = Noah::Host.create(:name => hostname2, :status => status2)
host1.save host1.save
host2.save host2.save
Hosts.all.class.should == Array Noah::Hosts.all.class.should == Array
Hosts.all.size.should == 2 Noah::Hosts.all.size.should == 2
Hosts.all.first.name.should == hostname1 Noah::Hosts.all.first.name.should == hostname1
Hosts.all.first.status.should == status1 Noah::Hosts.all.first.status.should == status1
Hosts.all.last.name.should == hostname2 Noah::Hosts.all.last.name.should == hostname2
Hosts.all.last.status.should == status2 Noah::Hosts.all.last.status.should == status2
end end
end end
describe "should not" do describe "should not" do
it "create a new Host with missing status" do it "create a new Host with missing status" do
hostname ="host3.domain.com" hostname ="host3.domain.com"
host = Host.create(:name => hostname) host = Noah::Host.create(:name => hostname)
host.valid?.should == false host.valid?.should == false
host.errors.should == [[:status, :not_present], [:status, :not_member]] host.errors.should == [[:status, :not_present], [:status, :not_member]]
end end
it "create a new Host with missing hostname" do it "create a new Host with missing hostname" do
status = "up" status = "up"
host = Host.create(:status => status) host = Noah::Host.create(:status => status)
host.valid?.should == false host.valid?.should == false
host.errors.should == [[:name, :not_present]] host.errors.should == [[:name, :not_present]]
end end
...@@ -100,7 +100,7 @@ describe "Using the Host Model", :reset_redis => true do ...@@ -100,7 +100,7 @@ describe "Using the Host Model", :reset_redis => true do
it "create a new Host with an invalid status" do it "create a new Host with an invalid status" do
hostname = "host3.domain.com" hostname = "host3.domain.com"
status = "online" status = "online"
host = Host.create(:name => hostname, :status => status) host = Noah::Host.create(:name => hostname, :status => status)
host.valid?.should == false host.valid?.should == false
host.errors.should == [[:status, :not_member]] host.errors.should == [[:status, :not_member]]
end end
...@@ -108,9 +108,9 @@ describe "Using the Host Model", :reset_redis => true do ...@@ -108,9 +108,9 @@ describe "Using the Host Model", :reset_redis => true do
it "create a duplicate Host" do it "create a duplicate Host" do
hostname = "host1.domain.com" hostname = "host1.domain.com"
status = "up" status = "up"
host1 = Host.create(:name => hostname, :status => status) host1 = Noah::Host.create(:name => hostname, :status => status)
host1.save host1.save
host2 = Host.create(:name => hostname, :status => status) host2 = Noah::Host.create(:name => hostname, :status => status)
host2.valid?.should == false host2.valid?.should == false
host2.errors.should == [[:name, :not_unique]] host2.errors.should == [[:name, :not_unique]]
end end
......
...@@ -2,8 +2,8 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper') ...@@ -2,8 +2,8 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
describe "Using the Application API", :reset_redis => false do describe "Using the Application API", :reset_redis => false do
before(:all) do before(:all) do
@a = Application.create(:name => 'rspec_sample_app') @a = Noah::Application.create(:name => 'rspec_sample_app')
@a.configurations << Configuration.create(:name => 'rspec_config', :format => 'string', :body => 'rspec is great', :application => @a) @a.configurations << Noah::Configuration.create(:name => 'rspec_config', :format => 'string', :body => 'rspec is great', :application => @a)
@a.save @a.save
@c = @a.configurations.first @c = @a.configurations.first
end end
...@@ -62,8 +62,8 @@ describe "Using the Application API", :reset_redis => false do ...@@ -62,8 +62,8 @@ describe "Using the Application API", :reset_redis => false do
response["id"].nil?.should == false response["id"].nil?.should == false
response["name"].should == @appdata[:name] response["name"].should == @appdata[:name]
response["action"].should == "create" response["action"].should == "create"
Application.find(:name => @appdata[:name]).size.should == 1 Noah::Application.find(:name => @appdata[:name]).size.should == 1
Application.find(:name => @appdata[:name]).first.is_new?.should == true Noah::Application.find(:name => @appdata[:name]).first.is_new?.should == true
end end
it "new application with missing name should not work" do it "new application with missing name should not work" do
put "/a/should_not_work", '{"foo":"bar"}', "CONTENT_TYPE" => "application/json" put "/a/should_not_work", '{"foo":"bar"}', "CONTENT_TYPE" => "application/json"
...@@ -79,8 +79,8 @@ describe "Using the Application API", :reset_redis => false do ...@@ -79,8 +79,8 @@ describe "Using the Application API", :reset_redis => false do
response["id"].nil?.should == false response["id"].nil?.should == false
response["name"].should == @appdata[:name] response["name"].should == @appdata[:name]
response["action"].should == "update" response["action"].should == "update"
Application.find(:name => @appdata[:name]).size.should == 1 Noah::Application.find(:name => @appdata[:name]).size.should == 1
Application.find(:name => @appdata[:name]).first.is_new?.should == false Noah::Application.find(:name => @appdata[:name]).first.is_new?.should == false
end end
end end
......
...@@ -85,9 +85,9 @@ describe "Using the Configuration API", :reset_redis => false, :populate_sample_ ...@@ -85,9 +85,9 @@ describe "Using the Configuration API", :reset_redis => false, :populate_sample_
describe "DELETE" do describe "DELETE" do
before(:all) do before(:all) do
@a = Application.create(:name => 'delete_test_app') @a = Noah::Application.create(:name => 'delete_test_app')
cparms = {:name => 'a', :format => 'string', :body => 'asdf', :application_id => @a.id} cparms = {:name => 'a', :format => 'string', :body => 'asdf', :application_id => @a.id}
@a.configurations << Configuration.create(cparms) @a.configurations << Noah::Configuration.create(cparms)
@a.save @a.save
@c = @a.configurations.first @c = @a.configurations.first
end end
......
...@@ -88,9 +88,9 @@ describe "Using the Host API", :reset_redis => false, :populate_sample_data => t ...@@ -88,9 +88,9 @@ describe "Using the Host API", :reset_redis => false, :populate_sample_data => t
describe "DELETE" do describe "DELETE" do
before(:all) do before(:all) do
@h = Host.create(:name => 'h', :status => 'up') @h = Noah::Host.create(:name => 'h', :status => 'up')
sparms = {:name => 's', :status => "up"} sparms = {:name => 's', :status => "up"}
@h.services << Service.create(sparms.merge({:host => @h})) @h.services << Noah::Service.create(sparms.merge({:host => @h}))
@h.save @h.save
@s = @h.services.first @s = @h.services.first
end end
......
...@@ -4,10 +4,10 @@ describe "Using the Service API", :reset_redis => false, :populate_sample_data = ...@@ -4,10 +4,10 @@ describe "Using the Service API", :reset_redis => false, :populate_sample_data =
before(:all) do before(:all) do
@sample_host = {:name => 'rspec_sample_host', :status => 'up'} @sample_host = {:name => 'rspec_sample_host', :status => 'up'}
@sample_service = {:name => 'rspec_sample_service', :status => 'up'} @sample_service = {:name => 'rspec_sample_service', :status => 'up'}
@h = Host.create(:name => 'rspec_sample_host', :status => 'up') @h = Noah::Host.create(:name => 'rspec_sample_host', :status => 'up')
@h.services << Service.create({:host => @h}.merge(@sample_service)) @h.services << Noah::Service.create({:host => @h}.merge(@sample_service))
@h.save @h.save
@s = Service.find(@sample_service).first @s = Noah::Service.find(@sample_service).first
end end
describe "calling" do describe "calling" do
...@@ -57,8 +57,8 @@ describe "Using the Service API", :reset_redis => false, :populate_sample_data = ...@@ -57,8 +57,8 @@ describe "Using the Service API", :reset_redis => false, :populate_sample_data =
response["id"].nil?.should == false response["id"].nil?.should == false
response["name"].should == @payload[:name] response["name"].should == @payload[:name]
response["host"].should == @payload[:host] response["host"].should == @payload[:host]
Service.find(:name => @payload[:name]).size.should == 1 Noah::Service.find(:name => @payload[:name]).size.should == 1
Service.find(:name => @payload[:name]).first.is_new?.should == true Noah::Service.find(:name => @payload[:name]).first.is_new?.should == true
end end
it "new service without host should not work" do it "new service without host should not work" do
put "/s/foobar", {:name => "foobar", :status => "up"}.to_json, "CONTENT_TYPE" => "application/json" put "/s/foobar", {:name => "foobar", :status => "up"}.to_json, "CONTENT_TYPE" => "application/json"
...@@ -88,15 +88,15 @@ describe "Using the Service API", :reset_redis => false, :populate_sample_data = ...@@ -88,15 +88,15 @@ describe "Using the Service API", :reset_redis => false, :populate_sample_data =
response["id"].nil?.should == false response["id"].nil?.should == false
response["name"].should == @payload[:name] response["name"].should == @payload[:name]
response["host"].should == @payload[:host] response["host"].should == @payload[:host]
Service.find(:name => @payload[:name]).size.should == 1 Noah::Service.find(:name => @payload[:name]).size.should == 1
Service.find(:name => @payload[:name]).first.is_new?.should == false Noah::Service.find(:name => @payload[:name]).first.is_new?.should == false
end end
end end
describe "DELETE" do describe "DELETE" do
before(:all) do before(:all) do
@h = Host.create(:name => "h1", :status => "up") @h = Noah::Host.create(:name => "h1", :status => "up")
@h.services << Service.create(:name => "s1", :status => "up", :host => @h) @h.services << Noah::Service.create(:name => "s1", :status => "up", :host => @h)
@h.save @h.save
@s = @h.services.first @s = @h.services.first
end end
......
...@@ -8,9 +8,9 @@ describe "Noah Service Model", :reset_redis => true do ...@@ -8,9 +8,9 @@ describe "Noah Service Model", :reset_redis => true do
servicestatus = "up" servicestatus = "up"
hostname = "mytesthost1" hostname = "mytesthost1"
hoststatus = "up" hoststatus = "up"
host = Host.create(:name => hostname, :status => hoststatus) host = Noah::Host.create(:name => hostname, :status => hoststatus)
host.save host.save
service = Service.create(:name => servicename, :status => servicestatus, :host => host) service = Noah::Service.create(:name => servicename, :status => servicestatus, :host => host)
service.valid?.should == true service.valid?.should == true
service.save service.save
service.name.should == servicename service.name.should == servicename
...@@ -20,45 +20,45 @@ describe "Noah Service Model", :reset_redis => true do ...@@ -20,45 +20,45 @@ describe "Noah Service Model", :reset_redis => true do
end end
it "create a new Service with find_or_create" do it "create a new Service with find_or_create" do
host = Host.create(:name => "h1", :status => "up") host = Noah::Host.create(:name => "h1", :status => "up")
host.save host.save
service = Service.find_or_create(:name => "s1", :status => "up", :host => host) service = Noah::Service.find_or_create(:name => "s1", :status => "up", :host => host)
service.save service.save
service.is_new?.should == true service.is_new?.should == true
end end
it "update an existing Service with find_or_create" do it "update an existing Service with find_or_create" do
host = Host.create(:name => "h2", :status => "up") host = Noah::Host.create(:name => "h2", :status => "up")
host.save host.save
service = Service.find_or_create(:name => "s2", :status => "up", :host => host) service = Noah::Service.find_or_create(:name => "s2", :status => "up", :host => host)
service.save service.save
sleep 1 sleep 1
service2 = Service.find_or_create(:name => "s2", :status => "up", :host => host) service2 = Noah::Service.find_or_create(:name => "s2", :status => "up", :host => host)
service2.save service2.save
service2.is_new?.should == false service2.is_new?.should == false
end end
it "delete a Service" do it "delete a Service" do
h = Host.create(:name => "h1", :status => "up") h = Noah::Host.create(:name => "h1", :status => "up")
h.save h.save
s = Service.create(:name => "s1", :status => "up", :host => h) s = Noah::Service.create(:name => "s1", :status => "up", :host => h)
s.save s.save
s = Service.find(:name => "s1").first s = Noah::Service.find(:name => "s1").first
s.delete s.delete
s = Service.find(:name => "s1").first s = Noah::Service.find(:name => "s1").first
s.should == nil s.should == nil
end end
it "find multiple Services" do it "find multiple Services" do
h = Host.create(:name => "h1", :status => "up") h = Noah::Host.create(:name => "h1", :status => "up")
if h.valid? if h.valid?
h.services << Service.create(:name => "s1", :status => "up", :host => h) h.services << Noah::Service.create(:name => "s1", :status => "up", :host => h)
h.services << Service.create(:name => "s2", :status => "up", :host => h) h.services << Noah::Service.create(:name => "s2", :status => "up", :host => h)
h.save h.save
end end
Services.all.size.should == 2 Noah::Services.all.size.should == 2
Services.all.first.name.should == "s1" Noah::Services.all.first.name.should == "s1"
Services.all.last.name.should == "s2" Noah::Services.all.last.name.should == "s2"
end end
end end
...@@ -68,41 +68,41 @@ describe "Noah Service Model", :reset_redis => true do ...@@ -68,41 +68,41 @@ describe "Noah Service Model", :reset_redis => true do
it "create a new Service when missing a Host" do it "create a new Service when missing a Host" do
servicename = "myservice1" servicename = "myservice1"
servicestatus = "up" servicestatus = "up"
service = Service.create(:name => servicename, :status => servicestatus) service = Noah::Service.create(:name => servicename, :status => servicestatus)
service.valid?.should == false service.valid?.should == false
service.errors.should == [[:host_id, :not_present]] service.errors.should == [[:host_id, :not_present]]
end end
it "create a new Service when missing a name" do it "create a new Service when missing a name" do
host = Host.create(:name => "host1.domain.com", :status => "up") host = Noah::Host.create(:name => "host1.domain.com", :status => "up")
host.save host.save
service = Service.create(:status => "up", :host => host) service = Noah::Service.create(:status => "up", :host => host)
service.valid?.should == false service.valid?.should == false
service.errors.should == [[:name, :not_present]] service.errors.should == [[:name, :not_present]]
end end
it "create a new Service when missing a status" do it "create a new Service when missing a status" do
host = Host.create(:name => "host1.domain.com", :status => "up") host = Noah::Host.create(:name => "host1.domain.com", :status => "up")
host.save host.save
service = Service.create(:name => 'foo', :host => host) service = Noah::Service.create(:name => 'foo', :host => host)
service.valid?.should == false service.valid?.should == false
service.errors.should == [[:status, :not_present], [:status, :not_member]] service.errors.should == [[:status, :not_present], [:status, :not_member]]
end end
it "create a new Service with an invalid status" do it "create a new Service with an invalid status" do
host = Host.create(:name => "host1.domain.com", :status => "up") host = Noah::Host.create(:name => "host1.domain.com", :status => "up")
host.save host.save
service = Service.create(:name => "myservice", :status => "invalid_status", :host => host) service = Noah::Service.create(:name => "myservice", :status => "invalid_status", :host => host)
service.valid?.should == false service.valid?.should == false
service.errors.should == [[:status, :not_member]] service.errors.should == [[:status, :not_member]]
end end
it "create a duplicate Service" do it "create a duplicate Service" do
host = Host.create(:name => "host1.domain.com", :status => "up") host = Noah::Host.create(:name => "host1.domain.com", :status => "up")
host.save host.save
s = Service.create(:name => "myservice", :status => "up", :host => host) s = Noah::Service.create(:name => "myservice", :status => "up", :host => host)
s.save s.save
s1 = Service.create(:name => "myservice", :status => "up", :host => host) s1 = Noah::Service.create(:name => "myservice", :status => "up", :host => host)
s1.valid?.should == false s1.valid?.should == false
s1.errors.should == [[[:name, :host_id], :not_unique]] s1.errors.should == [[[:name, :host_id], :not_unique]]
end end
......
...@@ -14,19 +14,19 @@ RSpec.configure do |config| ...@@ -14,19 +14,19 @@ RSpec.configure do |config|
config.after(:all, :populate_sample_data => true) {Ohm::redis.flushdb } config.after(:all, :populate_sample_data => true) {Ohm::redis.flushdb }
config.before(:all, :populate_sample_data => true) do config.before(:all, :populate_sample_data => true) do
Ohm::redis.flushdb Ohm::redis.flushdb
h = Host.create(:name => 'localhost', :status => "up") h = Noah::Host.create(:name => 'localhost', :status => "up")
if h.save if h.save
%w[redis noah].each do |service| %w[redis noah].each do |service|
s = Service.create(:name => service, :status => "up", :host => h) s = Noah::Service.create(:name => service, :status => "up", :host => h)
h.services << s h.services << s
end end
end end
a = Application.create(:name => 'noah') a = Noah::Application.create(:name => 'noah')
if a.save if a.save
cr = Configuration.create(:name => 'redis', :format => 'string', :body => 'redis://127.0.0.1:6379/0', :application => a) cr = Noah::Configuration.create(:name => 'redis', :format => 'string', :body => 'redis://127.0.0.1:6379/0', :application => a)
ch = Configuration.create(:name => 'host', :format => 'string', :body => 'localhost', :application => a) ch = Noah::Configuration.create(:name => 'host', :format => 'string', :body => 'localhost', :application => a)
cp = Configuration.create(:name => 'port', :format => 'string', :body => '9292', :application => a) cp = Noah::Configuration.create(:name => 'port', :format => 'string', :body => '9292', :application => a)
%w[cr ch cp].each do |c| %w[cr ch cp].each do |c|
a.configurations << eval(c) a.configurations << eval(c)
end end
...@@ -46,15 +46,15 @@ EOY ...@@ -46,15 +46,15 @@ EOY
} }
EOJ EOJ
a1 = Application.create(:name => 'myrailsapp1') a1 = Noah::Application.create(:name => 'myrailsapp1')
if a1.save if a1.save
c1 = Configuration.create(:name => 'database.yml', :format => 'yaml', :body => my_yaml, :application => a1) c1 = Noah::Configuration.create(:name => 'database.yml', :format => 'yaml', :body => my_yaml, :application => a1)
a1.configurations << c1 a1.configurations << c1
end end
a2 = Application.create(:name => 'myrestapp1') a2 = Noah::Application.create(:name => 'myrestapp1')
if a2.save if a2.save
c2 = Configuration.create(:name => 'config.json', :format => 'json', :body => my_json, :application => a2) c2 = Noah::Configuration.create(:name => 'config.json', :format => 'json', :body => my_json, :application => a2)
a2.configurations << c2 a2.configurations << c2
end end
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment