From ad3256339c855d8da20689495ddc434ff3680314 Mon Sep 17 00:00:00 2001 From: Brian Hartsock Date: Sat, 9 Jul 2011 17:46:15 -0400 Subject: [PATCH] Improved error handling --- lib/fog/rackspace/load_balancer.rb | 43 ++++++++++++++++--- .../requests/create_load_balancer.rb | 12 +++--- .../rackspace/requests/load_balancer_tests.rb | 24 +++++++++-- 3 files changed, 64 insertions(+), 15 deletions(-) diff --git a/lib/fog/rackspace/load_balancer.rb b/lib/fog/rackspace/load_balancer.rb index 6b8dac106..963e61774 100644 --- a/lib/fog/rackspace/load_balancer.rb +++ b/lib/fog/rackspace/load_balancer.rb @@ -2,6 +2,38 @@ module Fog module Rackspace 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/' ORD_ENDPOINT = 'https://ord.loadbalancers.api.rackspacecloud.com/v1.0/' @@ -47,13 +79,12 @@ module Fog :host => @host, :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 - raise case error - when Excon::Errors::NotFound - Fog::Compute::Rackspace::NotFound.slurp(error) - else - error - end + raise ServiceError.slurp error end unless response.body.empty? response.body = JSON.parse(response.body) diff --git a/lib/fog/rackspace/requests/create_load_balancer.rb b/lib/fog/rackspace/requests/create_load_balancer.rb index dff770d98..d218e3a59 100644 --- a/lib/fog/rackspace/requests/create_load_balancer.rb +++ b/lib/fog/rackspace/requests/create_load_balancer.rb @@ -2,14 +2,14 @@ module Fog module Rackspace class LoadBalancer class Real - def create_load_balancer(options = {}) + def create_load_balancer(name, protocol, port, virtualIps, nodes) data = { 'loadBalancer' => { - 'name' => options[:name], - 'port' => options[:port], - 'protocol' => options[:protocol], - 'virtualIps' => options[:virtualIps], - 'nodes' => options[:nodes] + 'name' => name, + 'port' => port, + 'protocol' => protocol, + 'virtualIps' => virtualIps, + 'nodes' => nodes #Is algorithm allowed on create? } } diff --git a/tests/rackspace/requests/load_balancer_tests.rb b/tests/rackspace/requests/load_balancer_tests.rb index c60de2b23..473f6b2e6 100644 --- a/tests/rackspace/requests/load_balancer_tests.rb +++ b/tests/rackspace/requests/load_balancer_tests.rb @@ -37,17 +37,22 @@ Shindo.tests('Fog::Rackspace::LoadBalancer | load_balancer_tests', ['rackspace'] 'connectionLogging' => { 'enabled' => Fog::Boolean } }} + @lb = Fog::Rackspace::LoadBalancer.new tests('success') do @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 - 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 + tests('#create_load_balancer(fog, )').formats(LOAD_BALANCER_FORMAT) do + 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'] data 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 @lb.get_load_balancer(@lb_id).body end @@ -75,5 +80,18 @@ Shindo.tests('Fog::Rackspace::LoadBalancer | load_balancer_tests', ['rackspace'] end 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