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