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