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

cleanup/DRY/exception handling changes

parent 54564437
...@@ -19,88 +19,65 @@ before do ...@@ -19,88 +19,65 @@ before do
content_type "application/json" content_type "application/json"
end end
not_found do
erb :'404'
end
error do
erb :'500'
end
namespace "/h" do namespace "/h" do
get '/:hostname/:servicename/?' do |hostname, servicename| get '/:hostname/:servicename/?' do |hostname, servicename|
host_service(hostname, servicename).to_json h = host_service(hostname, servicename)
if h.nil?
halt 404
else
h.to_json
end
end end
get '/:hostname/?' do |hostname| get '/:hostname/?' do |hostname|
begin h = host(:name => hostname)
h = host(:name => hostname) if h.nil?
if h.nil? halt 404
status 404 else
r = {"result" => "failure","error" => "Host not found"} h.to_json
r.to_json
else
status 200
h.to_json
end
rescue Exception => e
status 500
r = {"result" => "failure","error" => "#{e.message}"}
r.to_json
end end
end end
get '/?' do get '/?' do
begin hosts.map {|h| h.to_hash}
hosts.map {|h| h.to_hash} if hosts.size == 0
if hosts.size == 0 halt 404
status 200 else
r = {"result" => "success", "response" => "No hosts found"} hosts.to_json
r.to_json
else
status 200
hosts.to_json
end
rescue Exception => e
status 500
r = {"result" => "failure","error" => "#{e.message}"}
r.to_json
end end
end end
put '/?' do put '/?' do
begin 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 ? (host = Host.find_or_create(:name => data['name'], :status => data['status'])) : (raise "Missing Parameters")
data.keys.sort == required_params.sort ? (host = 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}"}
status 200
r = {"result" => "success","id" => "#{host.id}","status" => "#{host.status}", "name" => "#{host.name}"}
r.to_json
else
status 500
r = {"result" => "failure","error" => "#{app.errors}"}
r.to_json
end
rescue Exception => e
status 500
r = {"result" => "failure","error" => "#{e.message}"}
r.to_json r.to_json
else
raise host.errors
end end
end end
delete '/:hostname/?' do delete '/:hostname/?' do |hostname|
begin host = Host.find(:name => hostname).first
host = Host.find(:name => params[: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
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" => "#{host.name}", "service_count" => "#{services.size}"}
status 200
r = {"result" => "success", "id" => "#{host.id}", "name" => "#{host.name}", "service_count" => "#{services.size}"}
r.to_json
else
status 404
r = {"result" => "failure","error" => "Host not found"}
r.to_json
end
rescue Exception => e
status 500
r = {"result" => "failure", "error" => "#{e.message}"}
r.to_json r.to_json
else
halt 404
end end
end end
...@@ -109,102 +86,89 @@ end ...@@ -109,102 +86,89 @@ end
namespace "/s" do namespace "/s" do
get '/:servicename/:hostname/?' do |servicename, hostname| get '/:servicename/:hostname/?' do |servicename, hostname|
begin hs = host_service(hostname, servicename)
hs = host_service(hostname, servicename) if hs.nil?
status 200 halt 404
else
hs.to_json hs.to_json
rescue Exception => e
status 500
r = {"result" => "failure", "error" => "#{e.message}"}
r.to_json
end end
end end
get '/:servicename/?' do |servicename| get '/:servicename/?' do |servicename|
begin s = services(:name => servicename)
s = services(:name => servicename) s.map {|x| x.to_hash}
s.map {|x| x.to_hash} if s.empty?
status 200 halt 404
else
s.to_json s.to_json
rescue Exception => e
status 500
r = {"result" => "failure", "error" => "#{e.message}"}
r.to_json
end end
end end
get '/?' do get '/?' do
services.map {|s| s.to_hash} if services.empty?
services.to_json halt 404
else
services.map {|s| s.to_hash}
services.to_json
end
end end
put '/?' do
# NYI
end
delete '/:servicename/?' do |servicename|
#NYI
end
end end
namespace "/a" do namespace "/a" do
get '/:appname/:config/?' do get '/:appname/:config/?' do |appname, config|
app = Application.find(:name => params[:appname]).first app = Application.find(:name => appname).first
c = Configuration.find(:name => params[:config], :application_id => app.id).first if app.nil?
"#{c.to_json}" halt 404
else
c = Configuration.find(:name => config, :application_id => app.id).first
c.to_json
end
end end
get '/:appname/?' do get '/:appname/?' do |appname|
begin app = Application.find(:name => appname).first
app = Application.find(:name => params[:appname]).first if app.nil?
if app.nil? halt 404
status 404 else
r = {"result" => "failure","error" => "Application not found"} app.to_json
r.to_json
else
status 200
"#{app.to_json}"
end
rescue Exception => e
status 500
r = {"result" => "failure","error" => "#{e.message}"}
r.to_json
end end
end end
put '/:appname/?' do put '/:appname/?' do |appname|
begin app = Application.find(:name => appname).first ||= Application.create(:name => 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) app.name = data['name']
app.name = data['name'] if app.valid?
if app.valid? if app.save
app.save
status 200
r = {"result" => "success","id" => "#{app.id}"} r = {"result" => "success","id" => "#{app.id}"}
r.to_json r.to_json
else else
status 500 raise app.errors
r = {"result" => "failure","error" => "#{app.errors}"} end
r.to_json else
end raise app.errors
rescue Exception => e
status 500
r = {"result" => "failure","error" => "#{e.message}"}
r.to_json
end end
end end
delete '/:appname/?' do delete '/:appname/?' do |appname|
begin app = Application.find(:name => appname).first
app = Application.find(:name => params[:appname]).first if app.nil?
if app.nil? halt 404
status 404 else
r = {"result" => "failure","error" => "Application not found"} configurations = []
r.to_json Configuration.find(:application_id => app.id).sort.each {|x| configurations << x; x.delete} if app.configurations.size > 0
else app.delete
Configuration.find(:application_id => app.id).sort.each {|x| x.delete} if app.configurations.size > 0 r = {"result" => "success", "action" => "delete", "id" => "#{app.id}", "name" => "#{app.name}", "configurations" => "#{configurations.size}"}
app.delete
status 200
r = {"result" => "success", "id" => "#{app.id}"}
r.to_json
end
rescue Exception => e
status 500
r = {"result" => "failure", "error" => "#{e.message}"}
r.to_json r.to_json
end end
end end
...@@ -212,7 +176,11 @@ namespace "/a" do ...@@ -212,7 +176,11 @@ namespace "/a" do
get '/?' 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}" if apps.empty?
halt 404
else
apps.to_json
end
end end
end end
...@@ -227,71 +195,66 @@ namespace '/c' do ...@@ -227,71 +195,66 @@ namespace '/c' do
:xml => "text/xml" :xml => "text/xml"
} }
get '/:appname/:element/?' do get '/:appname/:element/?' do |appname, element|
a = Application.find(:name => params[:appname]).first a = Application.find(:name => appname).first
c = Configuration.find(:name => params[:element], :application_id => a.id).first if a.nil?
content_type content_type_mapping[c.format.to_sym] if content_type_mapping[c.format.to_sym] halt 404
"#{c.body}" else
c = 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]
c.body
end
end end
get '/:appname/?' do get '/:appname/?' do |appname|
config = [] config = []
a = Application.find(:name => params[:appname]).first a = Application.find(:name => appname).first
Configuration.find(:application_id => a.id).sort.each {|c| config << c.to_hash} if a.nil?
"#{config.to_json}" halt 404
else
Configuration.find(:application_id => a.id).sort.each {|c| config << c.to_hash}
config.to_json
end
end end
get '/?' 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}" if configs.empty?
halt 404
else
configs.to_json
end
end end
put '/:appname/:element?' do |appname, element| put '/:appname/:element?' do |appname, element|
begin app = Application.find_or_create(:name => appname)
app = Application.find_or_create(:name => appname) config = Configuration.find_or_create(:name => element, :application_id => app.id)
config = 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") if config.valid?
if config.valid? config.save
config.save r = {"result" => "success","id" => "#{config.id}"}
status 200
r = {"result" => "success","id" => "#{config.id}"}
r.to_json
else
status 500
r = {"result" => "failure","error" => "#{config.errors}"}
r.to_json
end
rescue Exception => e
status 500
r = {"result" => "failure","error" => "#{e.message}"}
r.to_json r.to_json
else
raise config.errors
end end
end end
delete '/:appname/:element?' do |appname, element| delete '/:appname/:element?' do |appname, element|
begin app = Application.find(:name => appname).first
app = Application.find(:name => appname).first if app
if app config = Configuration.find(:name=> element, :application_id => app.id).first
config = Configuration.find(:name=> element, :application_id => app.id).first if config
if config config.delete
config.delete r = {"result" => "success", "id" => "#{config.id}", "item" => "#{appname}/#{element}"}
status 200 r.to_json
r = {"result" => "success", "id" => "#{config.id}", "item" => "#{appname}/#{element}"}
r.to_json
else
r = {"result" => "failure","error" => "Configuration element not found"}
halt 404, r.to_json
end
else else
r = {"result" => "failure","error" => "Application not found"} halt 404
halt 404, r.to_json
end end
rescue Exception => e else
r = {"result" => "failure", "error" => "#{e.message}"} halt 404
halt 500, r.to_json
end end
end end
......
...@@ -7,8 +7,9 @@ require File.join(File.dirname(__FILE__), 'app') ...@@ -7,8 +7,9 @@ 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 :logging, true
set :raise_errors, true if development? set :raise_errors, false
set :show_exceptions, false
disable :run disable :run
run Sinatra::Application run Sinatra::Application
...@@ -16,13 +16,21 @@ helpers do ...@@ -16,13 +16,21 @@ helpers do
end end
def host_service(hostname, servicename) def host_service(hostname, servicename)
id = Host.find(:name => hostname).first.id h = Host.find(:name => hostname).first
Service.find(:host_id => id, :name => servicename).first if h.nil?
nil
else
Service.find(:host_id => h.id, :name => servicename).first
end
end end
def host_services(hostname) def host_services(hostname)
id = Host.find(:name => hostname).first.id h = Host.find(:name => hostname).first
Services.all(:host_id => id) if h.nil?
nil
else
Services.all(:host_id => id)
end
end end
def application(opts = {}) def application(opts = {})
......
{
"result":"failure",
"error_message":"Resource not found"
}
{
"result":"failure",
"error_message":"<%= request.env['sinatra.error'].message %>"
}
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