1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

Improved error handling

This commit is contained in:
Brian Hartsock 2011-07-09 17:46:15 -04:00
parent 95dc27c1ab
commit ad3256339c
3 changed files with 64 additions and 15 deletions

View file

@ -2,6 +2,38 @@ module Fog
module Rackspace module Rackspace
class LoadBalancer < Fog::Service class LoadBalancer < Fog::Service
class ServiceError < Fog::Errors::Error
attr_reader :response_data
def self.slurp(error)
#TODO Where is the best place to do this json require
require 'json'
if error.response.body.empty?
data = nil
message = nil
else
data = JSON.parse(error.response.body)
message = data['message']
end
new_error = super(error, message)
new_error.instance_variable_set(:@response_data, data)
new_error
end
end
class BadRequest < ServiceError
attr_reader :validation_errors
def self.slurp(error)
new_error = super(error)
unless new_error.response_data.nil?
new_error.instance_variable_set(:@validation_errors, new_error.response_data['validationErrors'])
end
new_error
end
end
DFW_ENDPOINT = 'https://dfw.loadbalancers.api.rackspacecloud.com/v1.0/' DFW_ENDPOINT = 'https://dfw.loadbalancers.api.rackspacecloud.com/v1.0/'
ORD_ENDPOINT = 'https://ord.loadbalancers.api.rackspacecloud.com/v1.0/' ORD_ENDPOINT = 'https://ord.loadbalancers.api.rackspacecloud.com/v1.0/'
@ -47,13 +79,12 @@ module Fog
:host => @host, :host => @host,
:path => "#{@path}/#{params[:path]}" :path => "#{@path}/#{params[:path]}"
})) }))
rescue Excon::Errors::NotFound => error
raise NotFound.slurp error
rescue Excon::Errors::BadRequest => error
raise BadRequest.slurp error
rescue Excon::Errors::HTTPStatusError => error rescue Excon::Errors::HTTPStatusError => error
raise case error raise ServiceError.slurp error
when Excon::Errors::NotFound
Fog::Compute::Rackspace::NotFound.slurp(error)
else
error
end
end end
unless response.body.empty? unless response.body.empty?
response.body = JSON.parse(response.body) response.body = JSON.parse(response.body)

View file

@ -2,14 +2,14 @@ module Fog
module Rackspace module Rackspace
class LoadBalancer class LoadBalancer
class Real class Real
def create_load_balancer(options = {}) def create_load_balancer(name, protocol, port, virtualIps, nodes)
data = { data = {
'loadBalancer' => { 'loadBalancer' => {
'name' => options[:name], 'name' => name,
'port' => options[:port], 'port' => port,
'protocol' => options[:protocol], 'protocol' => protocol,
'virtualIps' => options[:virtualIps], 'virtualIps' => virtualIps,
'nodes' => options[:nodes] 'nodes' => nodes
#Is algorithm allowed on create? #Is algorithm allowed on create?
} }
} }

View file

@ -37,17 +37,22 @@ Shindo.tests('Fog::Rackspace::LoadBalancer | load_balancer_tests', ['rackspace']
'connectionLogging' => { 'enabled' => Fog::Boolean } 'connectionLogging' => { 'enabled' => Fog::Boolean }
}} }}
@lb = Fog::Rackspace::LoadBalancer.new
tests('success') do tests('success') do
@lb_id = nil @lb_id = nil
@lb = Fog::Rackspace::LoadBalancer.new @lb_name = 'fog' + Time.now.to_i.to_s
tests('#create_load_balancer()').formats(LOAD_BALANCER_FORMAT) do tests('#create_load_balancer(fog, )').formats(LOAD_BALANCER_FORMAT) do
data = @lb.create_load_balancer(:name => 'fog' + Time.now.to_i.to_s, :port => '80', :protocol => 'HTTP', :virtualIps => [{ :type => 'PUBLIC'}], :nodes => [{ :address => '10.0.0.1', :port => 80, :condition => 'ENABLED'}]).body data = @lb.create_load_balancer(@lb_name, 'HTTP', 80, [{ :type => 'PUBLIC'}], [{ :address => '10.0.0.1', :port => 80, :condition => 'ENABLED'}]).body
@lb_id = data['loadBalancer']['id'] @lb_id = data['loadBalancer']['id']
data data
end end
tests("#update_load_balancer(#{@lb_id}) while immutable").raises(Fog::Rackspace::LoadBalancer::ServiceError) do
@lb.update_load_balancer(@lb_id, { :port => 80 }).body
end
tests("get_load_balancer(#{@lb_id})").formats(LOAD_BALANCER_FORMAT) do tests("get_load_balancer(#{@lb_id})").formats(LOAD_BALANCER_FORMAT) do
@lb.get_load_balancer(@lb_id).body @lb.get_load_balancer(@lb_id).body
end end
@ -75,5 +80,18 @@ Shindo.tests('Fog::Rackspace::LoadBalancer | load_balancer_tests', ['rackspace']
end end
tests('failure') do tests('failure') do
tests('create_load_balancer(invalid name)').raises(Fog::Rackspace::LoadBalancer::BadRequest) do
@lb.create_load_balancer('', 'HTTP', 80, [{ :type => 'PUBLIC'}], [{ :address => '10.0.0.1', :port => 80, :condition => 'ENABLED'}])
end
tests('get_load_balancer(0)').raises(Fog::Rackspace::LoadBalancer::NotFound) do
@lb.get_load_balancer(0)
end
tests('delete_load_balancer(0)').raises(Fog::Rackspace::LoadBalancer::NotFound) do
@lb.delete_load_balancer(0)
end
tests('update_load_balancer(0)').raises(Fog::Rackspace::LoadBalancer::NotFound) do
@lb.update_load_balancer(0, { :name => 'newname' })
end
end end
end end