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?
status 404 halt 404
r = {"result" => "failure","error" => "Host not found"}
r.to_json
else else
status 200
h.to_json h.to_json
end end
rescue Exception => e
status 500
r = {"result" => "failure","error" => "#{e.message}"}
r.to_json
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
status 200 halt 404
r = {"result" => "success", "response" => "No hosts found"}
r.to_json
else else
status 200
hosts.to_json hosts.to_json
end end
rescue Exception => e
status 500
r = {"result" => "failure","error" => "#{e.message}"}
r.to_json
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?
status 200
r = {"result" => "success","id" => "#{host.id}","status" => "#{host.status}", "name" => "#{host.name}"} r = {"result" => "success","id" => "#{host.id}","status" => "#{host.status}", "name" => "#{host.name}"}
r.to_json r.to_json
else else
status 500 raise host.errors
r = {"result" => "failure","error" => "#{app.errors}"}
r.to_json
end
rescue Exception => e
status 500
r = {"result" => "failure","error" => "#{e.message}"}
r.to_json
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
status 200
r = {"result" => "success", "id" => "#{host.id}", "name" => "#{host.name}", "service_count" => "#{services.size}"} r = {"result" => "success", "id" => "#{host.id}", "name" => "#{host.name}", "service_count" => "#{services.size}"}
r.to_json r.to_json
else else
status 404 halt 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
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)
status 200 if hs.nil?
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}
status 200 if s.empty?
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
if services.empty?
halt 404
else
services.map {|s| s.to_hash} services.map {|s| s.to_hash}
services.to_json 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?
status 404 halt 404
r = {"result" => "failure","error" => "Application not found"}
r.to_json
else else
status 200 app.to_json
"#{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?
app.save if 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}"}
r.to_json
end end
rescue Exception => e else
status 500 raise app.errors
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?
status 404 halt 404
r = {"result" => "failure","error" => "Application not found"}
r.to_json
else else
Configuration.find(:application_id => app.id).sort.each {|x| x.delete} if app.configurations.size > 0 configurations = []
Configuration.find(:application_id => app.id).sort.each {|x| configurations << x; x.delete} if app.configurations.size > 0
app.delete app.delete
status 200 r = {"result" => "success", "action" => "delete", "id" => "#{app.id}", "name" => "#{app.name}", "configurations" => "#{configurations.size}"}
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,28 +195,39 @@ namespace '/c' do ...@@ -227,28 +195,39 @@ 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?
halt 404
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] content_type content_type_mapping[c.format.to_sym] if content_type_mapping[c.format.to_sym]
"#{c.body}" 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
if a.nil?
halt 404
else
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 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"]
...@@ -256,42 +235,26 @@ namespace '/c' do ...@@ -256,42 +235,26 @@ namespace '/c' do
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
status 200
r = {"result" => "success","id" => "#{config.id}"} r = {"result" => "success","id" => "#{config.id}"}
r.to_json r.to_json
else else
status 500 raise config.errors
r = {"result" => "failure","error" => "#{config.errors}"}
r.to_json
end
rescue Exception => e
status 500
r = {"result" => "failure","error" => "#{e.message}"}
r.to_json
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
status 200
r = {"result" => "success", "id" => "#{config.id}", "item" => "#{appname}/#{element}"} r = {"result" => "success", "id" => "#{config.id}", "item" => "#{appname}/#{element}"}
r.to_json r.to_json
else else
r = {"result" => "failure","error" => "Configuration element not found"} halt 404
halt 404, r.to_json
end end
else else
r = {"result" => "failure","error" => "Application not found"} halt 404
halt 404, r.to_json
end
rescue Exception => e
r = {"result" => "failure", "error" => "#{e.message}"}
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,14 +16,22 @@ helpers do ...@@ -16,14 +16,22 @@ 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
if h.nil?
nil
else
Services.all(:host_id => id) Services.all(:host_id => id)
end end
end
def application(opts = {}) def application(opts = {})
Application.find(opts).first Application.find(opts).first
......
{
"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