Skip to content
This repository has been archived by the owner on Dec 27, 2022. It is now read-only.

Commit

Permalink
finished tagging API. need specs for Tag REST API
Browse files Browse the repository at this point in the history
  • Loading branch information
lusis committed Apr 12, 2011
1 parent 1a00123 commit 8ba5a85
Show file tree
Hide file tree
Showing 14 changed files with 146 additions and 58 deletions.
1 change: 1 addition & 0 deletions lib/noah/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class App < Sinatra::Base
erb :'500'
end

load File.join(File.dirname(__FILE__), 'tag_routes.rb')
load File.join(File.dirname(__FILE__), 'host_routes.rb')
load File.join(File.dirname(__FILE__), 'service_routes.rb')
load File.join(File.dirname(__FILE__), 'application_routes.rb')
Expand Down
5 changes: 3 additions & 2 deletions lib/noah/application_routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ class Noah::App
put '/applications/:appname/link' do |appname|
required_params = ["link_name"]
data = JSON.parse(request.body.read)
(data.keys.sort == required_params.sort) ? (a = Noah::Applicaiton.find(:name => appname).first) : (raise "Missing Parameters")
#a.nil? ? (halt 404) : (a.link!
(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/?' do |appname|
Expand Down
8 changes: 8 additions & 0 deletions lib/noah/configuration_routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ class Noah::App
end
end

put '/configurations/:configname/tag' do |configname|
required_params = ["tags"]
data = JSON.parse(request.body.read)
(data.keys.sort == required_params.sort) ? (c=Noah::Configuration.find(:name=>configname).first) : (raise "Missing Parameters")
c.nil? ? (halt 404) : (c.tag!(data['tags']))
c.to_json

end
put '/configurations/:configname/watch' do |configname|
required_params = ["endpoint"]
data = JSON.parse(request.body.read)
Expand Down
10 changes: 5 additions & 5 deletions lib/noah/ephemeral_routes.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
class Noah::App
get '/e/?' do
get '/ephemerals/?' do
halt 404
end

get '/e/*' do
get '/ephemerals/*' do
params["splat"].size == 0 ? (halt 404) : (e=Noah::Ephemeral.find(:path => "/#{params["splat"][0]}").first)
(halt 404) if e.nil?
content_type "application/octet-stream"
e.data.nil? ? "" : "#{e.data}"
end

put '/e/*/watch' do
put '/ephemerals/*/watch' do
required_params = ["endpoint"]
data = JSON.parse(request.body.read)
(data.keys.sort == required_params.sort) ? (e = Noah::Watcher.find(:path => params[:splat][0]).first) : (raise "Missing Parameters")
e.nil? ? (halt 404) : (w = e.watch!(:endpoint => data["endpoint"]))
w.to_json
end

put '/e/*' do
put '/ephemerals/*' do
raise("Data too large") if request.body.size > 512
d = request.body.read || nil
opts = {:path => "/#{params[:splat][0]}", :data => d}
Expand All @@ -32,7 +32,7 @@ class Noah::App
end
end

delete '/e/*' do
delete '/ephemerals/*' do
p = params[:splat][0]
e = Noah::Ephemeral.find(:path => "/"+p).first
if e
Expand Down
16 changes: 16 additions & 0 deletions lib/noah/host_routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ class Noah::App
end
end

put '/hosts/:hostname/tag' do |hostname|
required_params = ["tags"]
data = JSON.parse(request.body.read)
(data.keys.sort == required_params.sort) ? (a=Noah::Host.find(:name=>hostname).first) : (raise "Missing Parameters")
a.nil? ? (halt 404) : (a.tag!(data['tags']))
a.to_json
end

put '/hosts/:hostname/watch' do |hostname|
required_params = ["endpoint"]
data = JSON.parse(request.body.read)
Expand All @@ -40,6 +48,14 @@ class Noah::App
w.to_json
end

put '/hosts/:hostname/link' do |hostname|
required_params = ["link_name"]
data = JSON.parse(request.body.read)
(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 = ["name", "status"]
data = JSON.parse(request.body.read)
Expand Down
5 changes: 3 additions & 2 deletions lib/noah/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,11 @@ def self.models

end

require File.join(File.dirname(__FILE__), 'models','tags')
require File.join(File.dirname(__FILE__), 'linkable')
require File.join(File.dirname(__FILE__), 'models','link')
require File.join(File.dirname(__FILE__), 'taggable')
require File.join(File.dirname(__FILE__), 'linkable')
require File.join(File.dirname(__FILE__), 'models','tags')
require File.join(File.dirname(__FILE__), 'models','link')
require File.join(File.dirname(__FILE__), 'models','hosts')
require File.join(File.dirname(__FILE__), 'models','services')
require File.join(File.dirname(__FILE__), 'models','applications')
Expand Down
10 changes: 4 additions & 6 deletions lib/noah/models/link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def nodes=(node)
end
else
self.key[:nodes].sadd(node.key)
n.links << self
node.links << self
end
end

Expand All @@ -38,6 +38,7 @@ def to_hash
%w[applications configurations hosts services ephemerals].each {|x| instance_variable_set("@#{x}", Hash.new)}
if nodes.size > 0
nodes.each do |node|
rejected_vals = [:created_at, :updated_at, :links, :name]
n = node_to_class(node)
cls = class_to_lower(n.class.to_s)
hsh = instance_variable_get("@#{cls}s")
Expand All @@ -53,7 +54,7 @@ def to_hash
when "host"
hsh["#{n.name}"].merge!({:id => n.id, :status => n.status, :tags => n.to_hash[:tags], :services => n.to_hash[:services]})
else
hsh[n.name].merge!(n.to_hash.reject{|key, value| key == :created_at || key == :updated_at || key == :name})
hsh["#{n.name}"].merge!(n.to_hash.reject{|key, value| rejected_vals.member?(key)})
end
end
end
Expand All @@ -68,10 +69,7 @@ def name
class <<self
def find_or_create(opts={})
begin
find(opts).first.nil? ? obj=new(opts) : obj=find(opts).first
if obj.valid?
obj.save
end
find(opts).first.nil? ? obj=create(opts) : obj=find(opts).first
obj
rescue Exception => e
e.message
Expand Down
64 changes: 51 additions & 13 deletions lib/noah/models/tags.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module Noah

class Tag < Model
attribute :name
attribute :members
Expand All @@ -23,35 +24,72 @@ def members
cls = class_to_lower(n.class.to_s)
hash_key = "#{cls}s".to_sym
hsh[hash_key] = Array.new unless hsh.has_key?(hash_key)
hsh[hash_key] << n.name
hsh[hash_key] << n
end
hsh
end

def self.tagged(tag)
t = find(:name => tag).first
t.members
def to_hash
h = {:name => name, :created_at => created_at, :updated_at => updated_at}
h.merge!(members_to_hash)
super.merge(h)
end

class <<self
def find_or_create(opts={})
begin
find(opts).first.nil? ? obj=new(opts) : obj=find(opts).first
if obj.valid?
obj.save
def find_or_create(opts={})
begin
find(opts).first.nil? ? obj=create(opts) : obj=find(opts).first
obj
rescue Exception => e
e.message
end
obj
rescue Exception => e
e.message
end
end
end

private
def node_to_class(node)
node.match(/^Noah::(.*):(\d+)$/)
Noah.const_get($1).send(:[], $2)
end

def members_to_hash
new_hash = Hash.new
members.keys.each {|k| new_hash[k] = Hash.new}
members.each do |category, member_array|
rejected_vals = [:created_at, :updated_at, :tags, :name, :host]
h = new_hash[category]
member_array.each {|mem| h["#{mem.name}"] = Hash.new unless h.has_key?(mem.name)}
case category
when :services
member_array.each do |s|
sh = Noah::Host[s.host_id].name
h["#{s.name}"]["#{sh}"] = Hash.new unless h["#{s.name}"].has_key?(sh)
h["#{s.name}"]["#{sh}"].merge!(s.to_hash.reject{|k,v| rejected_vals.member?(k)})
end
else
member_array.each do |m|
h["#{m.name}"].merge!(m.to_hash.reject{|k,v| rejected_vals.member?(k)})
end
end
end
new_hash
end
end

class Tags
def self.all(options = {})
tag_hash = Hash.new
options.empty? ? tags=Tag.all.sort : tags=Tag.find(options).sort
tags.each {|t| tag_hash["#{t.name}"] = t.to_hash.reject {|k,v| k == :name } }
tag_hash
end

class <<self
def tagged(tag)
t = Tag.find(:name => tag).first
t.members
end
end
end

end
8 changes: 8 additions & 0 deletions lib/noah/service_routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ class Noah::App
services.to_json
end

put '/services/:servicename/:hostname/tag' do |servicename, hostname|
required_params = ["tags"]
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.tag!(data['tags']))
a.to_json
end

put '/services/:servicename/watch' do |servicename|
required_params = ["endpoint"]
data = JSON.parse(request.body.read)
Expand Down
14 changes: 14 additions & 0 deletions lib/noah/tag_routes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Noah::App

get '/tags/:tagname/?' do |tagname|
tags = Noah::Tags.all(:name => tagname).to_hash
(halt 404) if tags.size == 0
tags.to_json
end

get '/tags/?' do
tags = Noah::Tags.all.to_hash
(halt 404) if tags.size == 0
tags.to_json
end
end
2 changes: 0 additions & 2 deletions spec/noahapp_application_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
response.has_key?("configurations").should == true
c = response["configurations"]
c.has_key?(@c.name).should == true
p @c
p c
c["#{@c.name}"]["format"].should == "#{@c.format}"
c["#{@c.name}"]["body"].should == "#{@c.body}"
end
Expand Down
30 changes: 15 additions & 15 deletions spec/noahapp_ephemeral_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

describe "GET" do
it "all ephemerals should return 404" do
get '/e'
get '/ephemerals'
last_response.should_not be_ok
last_response.status.should == 404
response = last_response.should return_json
Expand All @@ -22,13 +22,13 @@
end

it "named path with data should work" do
get '/e/foo/bar/baz'
get '/ephemerals/foo/bar/baz'
last_response.should be_ok
last_response.body.should == 'value1'
end

it "named path without data should work" do
get '/e/baz/bar'
get '/ephemerals/baz/bar'
last_response.status.should == 200
last_response.body.should == ""
end
Expand All @@ -45,7 +45,7 @@

describe "PUT" do
it "new ephemeral with data should work" do
put '/e/whiz/bang/', 'value3'
put '/ephemerals/whiz/bang/', 'value3'
last_response.should be_ok
response = last_response.should return_json
response['result'].should == 'success'
Expand All @@ -55,7 +55,7 @@
end

it "new ephemeral without data should work" do
put '/e/bang/whiz'
put '/ephemerals/bang/whiz'
last_response.should be_ok
response = last_response.should return_json
response['result'].should == 'success'
Expand All @@ -67,35 +67,35 @@

it "existing ephemeral with data should work" do
Noah::Ephemeral.create(:path => '/new/ephemeral', :data => 'old_value')
get '/e/new/ephemeral'
get '/ephemerals/new/ephemeral'
last_response.should be_ok
last_response.body.should == 'old_value'
put '/e/new/ephemeral', 'new_value'
put '/ephemerals/new/ephemeral', 'new_value'
last_response.should be_ok
get '/e/new/ephemeral'
get '/ephemerals/new/ephemeral'
last_response.should be_ok
last_response.body.should == 'new_value'
end
it "existing ephemeral without data should work" do
Noah::Ephemeral.create(:path => '/a/random/key')
get '/e/a/random/key'
get '/ephemerals/a/random/key'
last_response.should be_ok
last_response.body.should == ""
put '/e/a/random/key', 'a new value'
put '/ephemerals/a/random/key', 'a new value'
last_response.should be_ok
get '/e/a/random/key'
get '/ephemerals/a/random/key'
last_response.should be_ok
last_response.body.should == 'a new value'
end
it "ephemeral with reserved word in subpath should work" do
Noah::PROTECTED_PATHS.each do |path|
put "/e/a/valid/path/with/#{path}"
put "/ephemerals/a/valid/path/with/#{path}"
last_response.should be_ok
end
end
it "ephemeral with reserved word as path should not work" do
Noah::PROTECTED_PATHS.each do |path|
put "/e/#{path}/other/stuff"
put "/ephemerals/#{path}/other/stuff"
last_response.should_not be_ok
response = last_response.should return_json
response['error_message'].should == 'Path is reserved'
Expand All @@ -108,7 +108,7 @@
it "existing path should work" do
e = Noah::Ephemeral.new(:path => '/slart/i/bart/fast', :data => 'someddata')
e.save
delete "/e/slart/i/bart/fast"
delete "/ephemerals/slart/i/bart/fast"
last_response.should be_ok
response = last_response.should return_json
response['result'].should == 'success'
Expand All @@ -118,7 +118,7 @@
end

it "invalid path should not work" do
delete '/e/fork/spoon/knife'
delete '/ephemerals/fork/spoon/knife'
last_response.should_not be_ok
last_response.status.should == 404
response = last_response.should return_json
Expand Down

0 comments on commit 8ba5a85

Please sign in to comment.