Commit 990a48ac authored by John E. Vincent's avatar John E. Vincent

namespaces/helpers/pluralized models refactor

parent bf412201
source "http://rubygems.org" source "http://rubygems.org"
gem "sinatra", "1.1.2" gem "sinatra", "1.1.2"
gem "sinatra-reloader", "0.5.0" gem "sinatra-reloader", "0.5.0"
gem "sinatra-namespace", "0.6.1"
gem "ohm", "0.1.3" gem "ohm", "0.1.3"
gem "ohm-contrib", "0.1.0" gem "ohm-contrib", "0.1.0"
gem "haml", "3.0.25" gem "haml", "3.0.25"
...@@ -20,6 +20,8 @@ GEM ...@@ -20,6 +20,8 @@ GEM
monkey-lib (~> 0.5.0) monkey-lib (~> 0.5.0)
sinatra (~> 1.0) sinatra (~> 1.0)
sinatra-sugar (~> 0.5.0) sinatra-sugar (~> 0.5.0)
sinatra-namespace (0.6.1)
sinatra (~> 1.1)
sinatra-reloader (0.5.0) sinatra-reloader (0.5.0)
sinatra (~> 1.0) sinatra (~> 1.0)
sinatra-advanced-routes (~> 0.5.0) sinatra-advanced-routes (~> 0.5.0)
...@@ -36,4 +38,5 @@ DEPENDENCIES ...@@ -36,4 +38,5 @@ DEPENDENCIES
ohm (= 0.1.3) ohm (= 0.1.3)
ohm-contrib (= 0.1.0) ohm-contrib (= 0.1.0)
sinatra (= 1.1.2) sinatra (= 1.1.2)
sinatra-namespace (= 0.6.1)
sinatra-reloader (= 0.5.0) sinatra-reloader (= 0.5.0)
...@@ -5,69 +5,76 @@ require 'haml' ...@@ -5,69 +5,76 @@ require 'haml'
require File.join(File.dirname(__FILE__), 'config/db') require File.join(File.dirname(__FILE__), 'config/db')
require File.join(File.dirname(__FILE__), 'models') require File.join(File.dirname(__FILE__), 'models')
require File.join(File.dirname(__FILE__), 'helpers')
# Read operations get '/' do
## Hosts before do
# Specifc Host/Specific Service content_type "text/html"
get '/h/:hostname/s/:servicename/?' do end
host = Host.find(:name => params[:hostname]).first
service = Service.find(:name => params[:servicename], :host_id => host.id).first
"#{service.to_json}"
end
# Specific Host/All Services haml :index, :format => :html5
get '/h/:hostname/s/?' do
host = Host.find(:name => params[:hostname]).first
services = []
Service.find(:host_id => host.id).sort.each {|s| services << s.to_hash}
"#{services.to_json}"
end end
# Specific Host before do
get '/h/:hostname/?' do content_type "application/json"
host = Host.find(:name => "#{params[:hostname]}").first
"#{host.to_json}"
end end
# All Hosts namespace "/h" do
get '/h/?' do
hosts = []
Host.all.sort.each {|h| hosts << h.to_hash}
"#{hosts.to_json}"
end
## Services get '/:hostname/s/:servicename/?' do |hostname, servicename|
get '/s/:servicename/h/:hostname/?' do host_service(hostname, servicename).to_json
h = Host.find(:name => params[:hostname]).first.id end
s = Service.find(:name => params[:servicename], :host_id => h).first
"#{s.to_json}" get '/:hostname/s/?' do |hostname|
end hs = host_services(hostname)
hs.map! {|x| x.to_hash}
hs.to_json
end
get '/:hostname/?' do |hostname|
host(:name => hostname).to_json
end
get '/?' do
hosts.map {|h| h.to_hash}
hosts.to_json
end
get '/s/:servicename/?' do
services = []
Service.find(:name => "#{params[:servicename]}").sort.each {|s| services << s.to_hash}
"#{services.to_json}"
end end
get '/s/?' do namespace "/s" do
services = []
Service.all.sort.each {|s| services << s.to_hash} get '/:servicename/h/:hostname/?' do |servicename, hostname|
"#{services.to_json}" host_service(hostname, servicename).to_json
end
get '/:servicename/?' do |servicename|
s = services(:name => servicename)
s.map {|x| x.to_hash}
s.to_json
end
get '/?' do
services.map {|s| s.to_hash}
services.to_json
end
end end
# Applications namespace "/a" do
get '/a/:appname/:config/?' do
get '/:appname/:config/?' do
app = Application.find(:name => params[:appname]).first app = Application.find(:name => params[:appname]).first
c = Configuration.find(:name => params[:config], :application_id => app.id).first c = Configuration.find(:name => params[:config], :application_id => app.id).first
"#{c.to_json}" "#{c.to_json}"
end end
get '/a/:appname/?' do get '/:appname/?' do
app = Application.find(:name => params[:appname]).first app = Application.find(:name => params[:appname]).first
"#{app.to_json}" "#{app.to_json}"
end end
put '/a/:appname/?' do put '/:appname/?' do
begin begin
app = Application.find(:name => params[:appname]).first ||= Application.create(:name => params[:appname]) app = Application.find(:name => params[:appname]).first ||= Application.create(:name => params[:appname])
data = JSON.parse(request.body.read) data = JSON.parse(request.body.read)
...@@ -75,55 +82,52 @@ put '/a/:appname/?' do ...@@ -75,55 +82,52 @@ put '/a/:appname/?' do
if app.valid? if app.valid?
app.save app.save
status 200 status 200
content_type 'application/json'
r = {"result" => "success","id" => "#{app.id}"} r = {"result" => "success","id" => "#{app.id}"}
r.to_json r.to_json
else else
status 500 status 500
content_type 'application/json'
r = {"result" => "failure","error" => "#{app.errors}"} r = {"result" => "failure","error" => "#{app.errors}"}
r.to_json r.to_json
end end
rescue Exception => e rescue Exception => e
status 500 status 500
content_type 'application/json'
r = {"result" => "failure","error" => "#{e.message}"} r = {"result" => "failure","error" => "#{e.message}"}
r.to_json r.to_json
end end
end end
delete '/a/:appname/?' do delete '/:appname/?' do
begin begin
app = Application.find(:name => params[:appname]).first app = Application.find(:name => params[:appname]).first
if app if app
Configuration.find(:application_id => app.id).sort.each {|x| x.delete} if app.configurations.size > 0 Configuration.find(:application_id => app.id).sort.each {|x| x.delete} if app.configurations.size > 0
app.delete app.delete
status 200 status 200
content_type 'application/json'
r = {"result" => "success", "id" => "#{app.id}"} r = {"result" => "success", "id" => "#{app.id}"}
r.to_json r.to_json
else else
status 404 status 404
content_type 'application/json'
r = {"result" => "failure","error" => "Application not found"} r = {"result" => "failure","error" => "Application not found"}
r.to_json r.to_json
end end
rescue Exception => e rescue Exception => e
status 500 status 500
content_type 'application/json'
r = {"result" => "failure", "error" => "#{e.message}"} r = {"result" => "failure", "error" => "#{e.message}"}
r.to_json r.to_json
end end
end end
get '/a/?' do get '/?' do
apps = [] apps = []
Application.all.sort.each {|a| apps << a.to_hash} Application.all.sort.each {|a| apps << a.to_hash}
"#{apps.to_json}" "#{apps.to_json}"
end
end end
## Configs namespace '/c' do
get '/c/:appname/:element/?' do
get '/:appname/:element/?' do
a = Application.find(:name => params[:appname]).first a = Application.find(:name => params[:appname]).first
c = Configuration.find(:name => params[:element], :application_id => a.id).first c = Configuration.find(:name => params[:element], :application_id => a.id).first
case c.format case c.format
...@@ -137,23 +141,21 @@ get '/c/:appname/:element/?' do ...@@ -137,23 +141,21 @@ get '/c/:appname/:element/?' do
content_type 'text/plain' content_type 'text/plain'
end end
"#{c.body}" "#{c.body}"
end end
get '/c/:appname/?' do get '/:appname/?' do
config = [] config = []
a = Application.find(:name => params[:appname]).first a = Application.find(:name => params[:appname]).first
Configuration.find(:application_id => a.id).sort.each {|c| config << c.to_hash} Configuration.find(:application_id => a.id).sort.each {|c| config << c.to_hash}
"#{config.to_json}" "#{config.to_json}"
end end
get '/c/?' do get '/?' do
configs = [] configs = []
Configuration.all.sort.each {|c| configs << c.to_hash} Configuration.all.sort.each {|c| configs << c.to_hash}
"#{configs.to_json}" "#{configs.to_json}"
end end
get '/' do
haml :index, :format => :html5
end end
# A route for adding a new host # A route for adding a new host
......
require 'rubygems' require 'rubygems'
require 'sinatra' require 'sinatra'
require 'sinatra/namespace'
require File.join(File.dirname(__FILE__), 'app') require File.join(File.dirname(__FILE__), 'app')
set :env, :development set :env, :development
set :root, File.dirname(__FILE__) set :root, File.dirname(__FILE__)
set :server, %[thin mongrel webrick] set :server, %[thin mongrel webrick]
set :logging, true if development?
set :raise_errors, true if development?
disable :run disable :run
set :raise_errors, true
run Sinatra::Application run Sinatra::Application
helpers do
def host(opts = {})
Host.find(opts).first
end
def hosts(opts = {})
Hosts.all(opts)
end
def service(opts = {})
Service.find(options)
end
def services(opts = {})
Services.all(opts)
end
def host_service(hostname, servicename)
id = Host.find(:name => hostname).first.id
Service.find(:host_id => id, :name => servicename).first
end
def host_services(hostname)
id = Host.find(:name => hostname).first.id
Services.all(:host_id => id)
end
def application(opts = {})
Application.find(opts).first
end
def applications(opts = {})
Applications.all(opts)
end
def configuration(opts = {})
Configuration.find(opts).first
end
def configurations(opts = {})
Configurations.all(opts)
end
end
...@@ -22,7 +22,16 @@ class Host < Ohm::Model ...@@ -22,7 +22,16 @@ class Host < Ohm::Model
services.sort.each {|s| arr << s.to_hash} services.sort.each {|s| arr << s.to_hash}
super.merge(:name => name, :state => state, :updated_at => updated_at, :services => arr) super.merge(:name => name, :state => state, :updated_at => updated_at, :services => arr)
end end
end
class Hosts
def self.all(options = {})
if options.empty?
Host.all.sort
else
Host.find(options).sort
end
end
end end
class Service < Ohm::Model class Service < Ohm::Model
...@@ -47,6 +56,16 @@ class Service < Ohm::Model ...@@ -47,6 +56,16 @@ class Service < Ohm::Model
end end
end end
class Services
def self.all(options = {})
if options.empty?
Service.all.sort
else
Service.find(options).sort
end
end
end
class Configuration < Ohm::Model class Configuration < Ohm::Model
include Ohm::Typecast include Ohm::Typecast
include Ohm::Timestamping include Ohm::Timestamping
...@@ -111,3 +130,28 @@ class Watcher < Ohm::Model #NYI ...@@ -111,3 +130,28 @@ class Watcher < Ohm::Model #NYI
assert_unique [:client, :endpoint, :event, :action] assert_unique [:client, :endpoint, :event, :action]
end end
end end
# Some pluralized helper objects
class Hosts
def self.all(options = {})
options.empty? ? Host.all.sort : Host.find(options).sort
end
end
class Services
def self.all(options = {})
options.empty? ? Service.all.sort : Service.find(options).sort
end
end
class Applications
def self.all(options = {})
options.empty? ? Application.all.sort : Application.find(options).sort
end
end
class Configurations
def self.all(options = {})
options.empty? ? Configuration.all.sort : Configuration.find(options).sort
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