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

refactor of host and service web api. no more arrays

parent 31b093bc
......@@ -25,7 +25,6 @@ class Noah::App
# GET all {Hosts}
get '/hosts/?' do
hosts.map {|h| h.to_hash}
if hosts.size == 0
halt 404
else
......
......@@ -4,6 +4,7 @@ module Noah
# Host model
# @return {Host} a {Host} object
include Taggable
include Linkable
attribute :name
attribute :status
collection :services, Service
......@@ -21,9 +22,11 @@ module Noah
# @return [Hash] A hash representation of a {Host}
def to_hash
arr = []
services.sort.each {|s| arr << {:id => s.id, :status => s.status, :name => s.name}}
h = {:name => name, :status => status, :created_at => created_at, :updated_at => updated_at, :services => arr}
services_hash = Hash.new
services.sort.each do |svc|
services_hash["#{svc.name}"] = svc.status
end
h = {:name => name, :status => status, :created_at => created_at, :updated_at => updated_at, :services => services_hash}
super.merge(h)
end
......@@ -50,7 +53,10 @@ module Noah
# @param [Hash] optional filters for results
# @return [Array] Array of {Host} objects
def self.all(options = {})
options.empty? ? Noah::Host.all.sort : Noah::Host.find(options).sort
host_hash = Hash.new
options.empty? ? hosts=Noah::Host.all.sort : hosts=Noah::Host.find(options).sort
hosts.each {|x| host_hash["#{x.name}"] = x.to_hash.reject {|k,v| k == :name } }
host_hash
end
end
end
......@@ -42,16 +42,17 @@ module Noah
# all of this bs is because services are unique per [servicename, hostname]
# so if I add multiple services just by name to the hash, I'll wipe the previous one
# a better option would be for services to be named slug style
if cls == "service"
hsh["#{n.name}-#{n.id}"] = Hash.new unless hsh.has_key?("#{n.name}-#{n.id}")
hsh["#{n.name}-#{n.id}"].merge!({n.to_hash[:host] => n.to_hash})
else
hsh["#{n.name}"] = Hash.new unless hsh.has_key?(n.name)
hsh[n.name].merge!(n.to_hash)
case cls
when "service"
sh = Noah::Host[n.host_id].name
hsh["#{n.name}"]["#{sh}"] = Hash.new unless hsh["#{n.name}"].has_key?("#{sh}")
hsh["#{n.name}"]["#{sh}"].merge!({:id => n.id, :status => n.status, :tags => n.to_hash[:tags]})
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})
end
# all of this bs is because services are unique per [servicename, hostname]
# so if I add multiple services just by name to the hash, I'll wipe the previous one
# a better option would be for services to be named slug style
end
end
h = {:name => name, :hosts => @hosts, :services => @services, :applications => @applications, :configurations => @configurations, :ephemerals => @ephemerals, :created_at => created_at, :updated_at => updated_at}
......
......@@ -2,6 +2,7 @@ module Noah
class Service < Model
include Taggable
include Linkable
attribute :name
attribute :status
reference :host, Host
......@@ -48,7 +49,15 @@ module Noah
class Services
def self.all(options = {})
options.empty? ? Service.all.sort : Service.find(options).sort
service_hash = Hash.new
options.empty? ? services=Service.all.sort : services=Service.find(options).sort
services.each do |svc|
service_hash["#{svc.name}"] = Hash.new unless service_hash.has_key?(svc.name)
host_name = Noah::Host[svc.host_id].name
service_hash["#{svc.name}"]["#{host_name}"] = Hash.new
service_hash["#{svc.name}"]["#{host_name}"].merge!({:id => svc.id, :status => svc.status, :tags => svc.to_hash[:tags], :links => svc.to_hash[:links], :created_at => svc.created_at, :updated_at => svc.updated_at})
end
service_hash
end
end
end
......@@ -13,22 +13,14 @@ class Noah::App
get '/services/:servicename/?' do |servicename|
s = services(:name => servicename)
s.map {|x| x.to_hash}
if s.empty?
halt 404
else
(halt 404) if s.size == 0
s.to_json
end
end
get '/services/?' do
if services.empty?
halt 404
else
services.map {|s| s.to_hash}
(halt 404) if services.size == 0
services.to_json
end
end
put '/services/:servicename/watch' do |servicename|
required_params = ["endpoint"]
......
......@@ -73,12 +73,12 @@ describe "Using the Host Model", :reset_redis => true do
host2 = Noah::Host.create(:name => hostname2, :status => status2)
host1.save
host2.save
Noah::Hosts.all.class.should == Array
Noah::Hosts.all.class.should == Hash
Noah::Hosts.all.size.should == 2
Noah::Hosts.all.first.name.should == hostname1
Noah::Hosts.all.first.status.should == status1
Noah::Hosts.all.last.name.should == hostname2
Noah::Hosts.all.last.status.should == status2
Noah::Hosts.all.has_key?(hostname1).should == true
Noah::Hosts.all[hostname1][:status].should == status1
Noah::Hosts.all.has_key?(hostname2).should == true
Noah::Hosts.all[hostname2][:status].should == status2
end
end
......
......@@ -20,10 +20,10 @@ describe "Using the Host API", :reset_redis => false, :populate_sample_data => t
response["name"].should == "localhost"
response["status"].should == "up"
services.size.should == 2
services.first["name"].should == "redis"
services.first["status"].should == "up"
services.last["name"].should == "noah"
services.last["status"].should == "up"
services.has_key?("redis").should == true
services.has_key?("noah").should == true
services["redis"].should == "up"
services["noah"].should == "up"
end
it "named service for host should work" do
......
......@@ -16,18 +16,19 @@ describe "Using the Service API", :reset_redis => false, :populate_sample_data =
get '/services'
last_response.should be_ok
response = last_response.should return_json
response.is_a?(Array).should == true
response.is_a?(Hash).should == true
end
it "all named services should work" do
get "/services/#{@sample_service[:name]}"
last_response.should be_ok
response = last_response.should return_json
response.is_a?(Array).should == true
s = response.first
s["id"].should == @s.id
s["name"].should == @s.name
s["status"].should == @s.status
s["host"].should == @h.name
response.is_a?(Hash).should == true
response.has_key?(@s.name).should == true
response[@s.name].is_a?(Hash).should == true
response[@s.name].has_key?(@h.name).should == true
response[@s.name][@h.name].is_a?(Hash).should == true
response[@s.name][@h.name]["id"].should == @s.id
response[@s.name][@h.name]["status"].should == @s.status
end
it "named service for host should work" do
get "/services/#{@sample_service[:name]}/#{@sample_host[:name]}"
......
......@@ -57,8 +57,8 @@ describe "Noah Service Model", :reset_redis => true do
h.save
end
Noah::Services.all.size.should == 2
Noah::Services.all.first.name.should == "s1"
Noah::Services.all.last.name.should == "s2"
Noah::Services.all.has_key?("s1").should == true
Noah::Services.all.has_key?("s2").should == true
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