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

linking DRYd up

parent 3f1a3d55
......@@ -6,6 +6,7 @@ module Noah
class App < Sinatra::Base
helpers Noah::SinatraBaseHelpers
helpers Noah::SinatraTagHelpers
helpers Noah::SinatraLinkHelpers
configure do
set :app_file, __FILE__
......
module Noah
module SinatraLinkHelpers
def link(primitive, name, link)
case primitive
when "services"
servicename, hostname = name.split('/')
obj = host_service(hostname, servicename)
when 'hosts'
obj = Noah::Host.find(:name=>name).first
when 'configurations'
obj = Noah::Configuration.find(:name=>name).first
when 'applications'
obj = Noah::Application.find(:name=>name).first
when 'ephemerals'
obj = Noah::Ephemeral.find(:path=>"/#{name}").first
else
halt 404
end
obj.nil? ? (halt 404) : (obj.link! link)
obj.to_json
end
def unlink(primitive, name, link)
case primitive
when "services"
servicename, hostname = name.split('/')
obj = host_service(hostname, servicename)
when "hosts"
obj = Noah::Host.find(:name=>name).first
when "configurations"
obj = Noah::Configuration.find(:name=>name).first
when "applications"
obj = Noah::Application.find(:name=>name).first
when "ephemerals"
obj = Noah::Ephemeral.find(:path=>"/#{name}").first
else
halt 404
end
obj.nil? ? (halt 404) : (obj.unlink! link)
obj.to_json
end
end
end
......@@ -39,6 +39,7 @@ module Noah
else
halt 404
end
obj.nil? ? (halt 404) : (obj.untag!(tags))
obj.to_json
end
......
......@@ -21,14 +21,6 @@ class Noah::App
w.to_json
end
put '/applications/:appname/link' do |appname|
required_params = ["link_name"]
data = JSON.parse(request.body.read)
(data.keys.sort == required_params.sort) ? (a = Noah::Application.find(:name => appname).first) : (raise "Missing Parameters")
a.nil? ? (halt 404) : (a.link! data["link_name"])
a.to_json
end
put '/applications/:appname/configurations/:configname/?' do |appname, configname|
required_params = ["format", "body"]
raise "Missing Parameters" if request.body.nil?
......
......@@ -29,15 +29,6 @@ class Noah::App
configs.to_json
end
# Add configuration object to a custom link hierarchy
put '/configurations/:configname/link' do |configname|
required_params = ["link_name"]
data = JSON.parse(request.body.read)
(data.keys.sort == required_params.sort) ? (a = Noah::Configuration.find(:name => configname).first) : (raise "Missing Parameters")
a.nil? ? (halt 404) : (a.link! data["link_name"])
a.to_json
end
# Add a watch to a configuration object
put '/configurations/:configname/watch' do |configname|
required_params = ["endpoint"]
......
......@@ -19,6 +19,7 @@ class Noah::App
end
put '/ephemerals/*' do
pass if params["splat"][0].match(/^.*\/link/)
raise("Data too large") if request.body.size > 512
d = request.body.read || nil
opts = {:path => "/#{params[:splat][0]}", :data => d}
......@@ -33,6 +34,7 @@ class Noah::App
end
delete '/ephemerals/*' do
pass if params["splat"][0].match(/^.*\/link/)
p = params[:splat][0]
e = Noah::Ephemeral.find(:path => "/"+p).first
if e
......
......@@ -41,15 +41,6 @@ class Noah::App
w.to_json
end
put '/hosts/:hostname/link' do |hostname|
required_params = ["link_name"]
data = JSON.parse(request.body.read)
raise "Missing parameters" if data.nil?
(data.keys.sort == required_params.sort) ? (a = Noah::Host.find(:name => hostname).first) : (raise "Missing Parameters")
a.nil? ? (halt 404) : (a.link! data["link_name"])
a.to_json
end
put '/hosts/:hostname/?' do |hostname|
required_params = ["status"]
data = JSON.parse(request.body.read)
......
class Noah::App
put '/:primitive/*/link' do |primitive, name|
required_params = ["link_name"]
data = JSON.parse(request.body.read)
(data.keys.sort == required_params.sort) ? (link(primitive, name, data["link_name"])) : (raise "Missing Parameters")
end
delete '/:primitive/*/link' do |primitive, name|
required_params = ["link_name"]
data = JSON.parse(request.body.read)
(data.keys.sort == required_params.sort) ? (unlink(primitive, name, data["link_name"])) : (raise "Missing Parameters")
end
get '/:link_name/:model_name/:item/?' do |path, model, item|
link_name = find_named_link(path)
(halt 404) if link_name.to_hash.has_key?(model.to_sym) == false
......
......@@ -22,22 +22,6 @@ class Noah::App
services.to_json
end
put '/services/:servicename/link' do |appname|
required_params = ["link_name"]
data = JSON.parse(request.body.read)
(data.keys.sort == required_params.sort) ? (a = Noah::Service.find(:name => servicename).first) : (raise "Missing Parameters")
a.nil? ? (halt 404) : (a.link! data["link_name"])
a.to_json
end
put '/services/:servicename/:hostname/link' do |servicename, hostname|
required_params = ["link_name"]
data = JSON.parse(request.body.read)
(data.keys.sort == required_params.sort) ? (a=host_service(hostname, servicename)) : (raise "Missing Parameters")
a.nil? ? (halt 404) : (a.link! data["link_name"])
a.to_json
end
put '/services/:servicename/watch' do |servicename|
required_params = ["endpoint"]
data = JSON.parse(request.body.read)
......
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
describe "Using the Link API", :reset_redis => true do
before(:each) do
Ohm.redis.flushdb
@link_name = {"link_name"=>"rspec_org"}
@host = Noah::Host.create(:name => 'linked_host', :status => 'up')
@service = Noah::Service.create(:name => 'linked_service', :status => 'down', :host_id => @host.id)
@application = Noah::Application.create(:name => 'linked_application')
@configuration = Noah::Configuration.create(:name => 'linked_configuration', :format => 'string', :body => 'some_string')
@ephemeral = Noah::Ephemeral.create(:path => '/tagged/ephemeral')
end
after(:each) do
Ohm.redis.flushdb
end
describe "calling" do
describe "PUT" do
['host', 'service', 'application', 'configuration', 'ephemeral'].each do |primitive|
it "link to #{primitive} should work" do
obj = instance_variable_get("@#{primitive}")
case primitive
when "ephemeral"
put "/#{primitive}s#{obj.path}/link", @link_name.to_json
when "service"
put "/#{primitive}s/#{obj.name}/#{@host.name}/link", @link_name.to_json
else
put "/#{primitive}s/#{obj.name}/link", @link_name.to_json
end
last_response.should be_ok
response = last_response.should return_json
response['links'][0].should == @link_name["link_name"]
end
end
end
describe "DELETE" do
# ['host', 'service', 'application', 'configuration', 'ephemeral'].each do |primitive|
# it "multiple tags from #{primitive} should work" do
# obj = instance_variable_get("@#{primitive}")
# case primitive
# when "ephemeral"
# delete "/#{primitive}s#{obj.path}/tag", @multi_tags.to_json
# when "service"
# delete "/#{primitive}s/#{obj.name}/#{@host.name}/tag", @multi_tags.to_json
# else
# delete "/#{primitive}s/#{obj.name}/tag", @multi_tags.to_json
# end
# last_response.should be_ok
# response = last_response.should return_json
# response['tags'].sort.should_not == @multi_tags["tags"].sort
# response['tags'].size.should == 0
# end
# end
end
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