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

namespaces/helpers/pluralized models refactor

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