You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@deltacloud.apache.org by mf...@redhat.com on 2012/03/13 14:42:41 UTC

[PATCH core 1/6] RHEV-M: Return 400 when API_PROVIDER is not set

From: Michal Fojtik <mf...@redhat.com>


Signed-off-by: Michal fojtik <mf...@redhat.com>
---
 .../lib/deltacloud/drivers/rhevm/rhevm_driver.rb   |    5 +++--
 1 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/server/lib/deltacloud/drivers/rhevm/rhevm_driver.rb b/server/lib/deltacloud/drivers/rhevm/rhevm_driver.rb
index ac4fe72..05fdbaf 100644
--- a/server/lib/deltacloud/drivers/rhevm/rhevm_driver.rb
+++ b/server/lib/deltacloud/drivers/rhevm/rhevm_driver.rb
@@ -206,8 +206,9 @@ class RHEVMDriver < Deltacloud::BaseDriver
   private
 
   def new_client(credentials)
-    url, datacenter = api_provider.split(';')
     safely do
+      raise 'No API provider set for this request.' unless api_provider
+      url, datacenter = api_provider.split(';')
       OVIRT::Client.new(credentials.user, credentials.password, url, datacenter)
     end
   end
@@ -348,7 +349,7 @@ class RHEVMDriver < Deltacloud::BaseDriver
       status 500
     end
 
-    on /(Bad Request|Parameter name)/ do
+    on /(Bad Request|Parameter name|No API provider)/ do
       status 400
     end
 
-- 
1.7.9.1


[PATCH core 4/6] Core: HTTP code 400 now captured by 'error' helper in Sinatra

Posted by mf...@redhat.com.
From: Michal Fojtik <mf...@redhat.com>

Currently the 40x error codes are not captured by 'error' helper
we have in server.rb. To capture the 400 code we need to use
this helper more expecitely, givin the exception class as param.

With this patch, the 400 errors are reported correctly, using XML
or HTML view.

Signed-off-by: Michal fojtik <mf...@redhat.com>
---
 server/lib/deltacloud/server.rb |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)

diff --git a/server/lib/deltacloud/server.rb b/server/lib/deltacloud/server.rb
index 53dd705..d47c87f 100644
--- a/server/lib/deltacloud/server.rb
+++ b/server/lib/deltacloud/server.rb
@@ -85,6 +85,10 @@ error do
   report_error
 end
 
+error Deltacloud::ExceptionHandler::ValidationFailure do
+  report_error
+end
+
 before do
   # Respond with 400, If we don't get a http Host header,
   halt 400, "Unable to find HTTP Host header" if @env['HTTP_HOST'] == nil
-- 
1.7.9.1


[PATCH core 3/6] Client: Re-use server message for 40x HTTP statuses (DTACLOUD-160)

Posted by mf...@redhat.com.
From: Michal Fojtik <mf...@redhat.com>

Previously the 40x HTTP response status was dropped
and replaced by 'default' description from HTTP specification.
This patch will allow to send user-defined messages from
server. For example code 400 (Bad request) can now indicate
invalid parameters to the client.

Signed-off-by: Michal fojtik <mf...@redhat.com>
---
 client/lib/deltacloud.rb |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/client/lib/deltacloud.rb b/client/lib/deltacloud.rb
index e0cb2e4..e6ae89e 100644
--- a/client/lib/deltacloud.rb
+++ b/client/lib/deltacloud.rb
@@ -355,10 +355,10 @@ module DeltaCloud
     end
 
     def response_error(response)
-      if response.code.to_s =~ /4(\d{2})/
+      xml = Nokogiri::XML(response.to_s)
+      if (xml/'message').empty? and response.code.to_s =~ /4(\d{2})/
         DeltaCloud::HTTPError.client_error(response.code)
       else
-        xml = Nokogiri::XML(response.to_s)
         opts = {
           :driver => (xml/'backend').first[:driver],
           :provider => (xml/'backend').first[:provider],
-- 
1.7.9.1


Re: [PATCH core 1/6] RHEV-M: Return 400 when API_PROVIDER is not set

Posted by David Lutterkort <lu...@redhat.com>.
On Tue, 2012-03-13 at 14:42 +0100, mfojtik@redhat.com wrote:
> From: Michal Fojtik <mf...@redhat.com>
> 
> 
> Signed-off-by: Michal fojtik <mf...@redhat.com>
> ---
>  .../lib/deltacloud/drivers/rhevm/rhevm_driver.rb   |    5 +++--
>  1 files changed, 3 insertions(+), 2 deletions(-)
> 
> diff --git a/server/lib/deltacloud/drivers/rhevm/rhevm_driver.rb b/server/lib/deltacloud/drivers/rhevm/rhevm_driver.rb
> index ac4fe72..05fdbaf 100644
> --- a/server/lib/deltacloud/drivers/rhevm/rhevm_driver.rb
> +++ b/server/lib/deltacloud/drivers/rhevm/rhevm_driver.rb
> @@ -206,8 +206,9 @@ class RHEVMDriver < Deltacloud::BaseDriver
>    private
>  
>    def new_client(credentials)
> -    url, datacenter = api_provider.split(';')
>      safely do
> +      raise 'No API provider set for this request.' unless api_provider
> +      url, datacenter = api_provider.split(';')

While we're doing error checks, we should cover the case where
api_provider.split(';').size != 2 To be defensive, I'd do

        ary = api_provider.split(';')
        datacenter = ary.pop
        url = ary.join(';')


David



Re: [PATCH core 1/6] RHEV-M: Return 400 when API_PROVIDER is not set

Posted by "marios@redhat.com" <ma...@redhat.com>.
ACK series

On 13/03/12 15:42, mfojtik@redhat.com wrote:
> From: Michal Fojtik <mf...@redhat.com>
> 
> 
> Signed-off-by: Michal fojtik <mf...@redhat.com>
> ---
>  .../lib/deltacloud/drivers/rhevm/rhevm_driver.rb   |    5 +++--
>  1 files changed, 3 insertions(+), 2 deletions(-)
> 
> diff --git a/server/lib/deltacloud/drivers/rhevm/rhevm_driver.rb b/server/lib/deltacloud/drivers/rhevm/rhevm_driver.rb
> index ac4fe72..05fdbaf 100644
> --- a/server/lib/deltacloud/drivers/rhevm/rhevm_driver.rb
> +++ b/server/lib/deltacloud/drivers/rhevm/rhevm_driver.rb
> @@ -206,8 +206,9 @@ class RHEVMDriver < Deltacloud::BaseDriver
>    private
>  
>    def new_client(credentials)
> -    url, datacenter = api_provider.split(';')
>      safely do
> +      raise 'No API provider set for this request.' unless api_provider
> +      url, datacenter = api_provider.split(';')
>        OVIRT::Client.new(credentials.user, credentials.password, url, datacenter)
>      end
>    end
> @@ -348,7 +349,7 @@ class RHEVMDriver < Deltacloud::BaseDriver
>        status 500
>      end
>  
> -    on /(Bad Request|Parameter name)/ do
> +    on /(Bad Request|Parameter name|No API provider)/ do
>        status 400
>      end
>  


[PATCH core 6/6] Core: Added %backend tag to 400 HAML view

Posted by mf...@redhat.com.
From: Michal Fojtik <mf...@redhat.com>


Signed-off-by: Michal fojtik <mf...@redhat.com>
---
 server/views/errors/400.xml.haml |    3 +--
 1 files changed, 1 insertions(+), 2 deletions(-)

diff --git a/server/views/errors/400.xml.haml b/server/views/errors/400.xml.haml
index 4f2723f..dd512a2 100644
--- a/server/views/errors/400.xml.haml
+++ b/server/views/errors/400.xml.haml
@@ -1,4 +1,3 @@
 %error{:url => "#{request.env['REQUEST_URI']}", :status => "#{response.status}"}
-  %parameter #{@error.name}
   %message< #{cdata @error.message}
-
+  %backend{ :driver => driver_symbol, :provider => "#{Thread::current[:provider] || ENV['API_PROVIDER'] || 'default'}" }
-- 
1.7.9.1


[PATCH core 5/6] Core: Removed Failure exception from validation

Posted by mf...@redhat.com.
From: Michal Fojtik <mf...@redhat.com>

Previously we had two exceptions defined to handle the 400
errors (like params validation). This patch will remove
the one defined in validation.rb and re-use the one from
exception handling system.

All validation errors (400) should be now treated with
ValidationFailure exception from exceptions.rb.

Signed-off-by: Michal fojtik <mf...@redhat.com>
---
 .../drivers/terremark/terremark_driver.rb          |    4 ++-
 server/lib/deltacloud/server.rb                    |    4 ++-
 server/lib/deltacloud/validation.rb                |   23 +++++++-------------
 3 files changed, 14 insertions(+), 17 deletions(-)

diff --git a/server/lib/deltacloud/drivers/terremark/terremark_driver.rb b/server/lib/deltacloud/drivers/terremark/terremark_driver.rb
index c93bfb3..da58364 100644
--- a/server/lib/deltacloud/drivers/terremark/terremark_driver.rb
+++ b/server/lib/deltacloud/drivers/terremark/terremark_driver.rb
@@ -141,7 +141,9 @@ VAPP_STATE_MAP = { "0" =>  "PENDING", "1" =>  "PENDING", "2" =>  "STOPPED", "4"
     end
     unless ( (terremark_hwp.include?(:cpu, opts[:hwp_cpu].to_i)) &&
               (terremark_hwp.include?(:memory, opts[:hwp_memory].to_i)) ) then
-       raise Deltacloud::Validation::Failure.new(Deltacloud::Validation::Param.new(["cpu"]), "Error with cpu and/or memory values. you said cpu->#{opts[:hwp_cpu]} and mem->#{opts[:hwp_memory]}")
+        raise Deltacloud::ExceptionHandler::ValidationFailure.new(
+          StandardError.new("Error with cpu and/or memory values. you said cpu->#{opts[:hwp_cpu]} and mem->#{opts[:hwp_memory]}")
+        )
     end
     vapp_opts['cpus'] = opts[:hwp_cpu]
     vapp_opts['memory'] =  opts[:hwp_memory]
diff --git a/server/lib/deltacloud/server.rb b/server/lib/deltacloud/server.rb
index d47c87f..b845616 100644
--- a/server/lib/deltacloud/server.rb
+++ b/server/lib/deltacloud/server.rb
@@ -1245,7 +1245,9 @@ collection :firewalls do
       params['addresses'] = addresses
       params['groups'] = groups
       if addresses.empty? && groups.empty?
-        raise Deltacloud::Validation::Failure.new(nil, "No sources. Specify at least one source ip_address or group")
+        raise Deltacloud::ExceptionHandler::ValidationFailure.new(
+          StandardError.new("No sources. Specify at least one source ip_address or group")
+        )
       end
       driver.create_firewall_rule(credentials, params)
       @firewall = driver.firewall(credentials, {:id => params[:id]})
diff --git a/server/lib/deltacloud/validation.rb b/server/lib/deltacloud/validation.rb
index e14c532..3d29225 100644
--- a/server/lib/deltacloud/validation.rb
+++ b/server/lib/deltacloud/validation.rb
@@ -16,17 +16,6 @@
 
 module Deltacloud::Validation
 
-  class Failure < Deltacloud::ExceptionHandler::DeltacloudException
-    attr_reader :param
-    def initialize(e, message=nil)
-      message ||= e.message
-      super(400, e.class.name, message, [])
-    end
-    def name
-      param.name if @param
-    end
-  end
-
   class Param
     attr_reader :name, :klass, :type, :options, :description
 
@@ -87,21 +76,25 @@ module Deltacloud::Validation
   def validate(current_driver, all_params, values, credentials)
     all_params.each do |key, p|
       if p.required? and not values[p.name]
-        raise Failure.new(p, "Required parameter #{p.name} not found")
+        raise validation_exception "Required parameter #{p.name} not found"
       end
       next unless values[p.name]
       if p.hwp_property?
         profile = current_driver.hardware_profile(credentials, values['hwp_id'])
-        raise Failure.new(p, "Unknown hardware profile selected #{values['hwp_id']}") unless profile
+        raise validation_exception("Unknown hardware profile selected #{values['hwp_id']}") unless profile
         unless p.valid_hwp_value?(profile, values[p.name])
-          raise Failure.new(p, "Hardware profile property #{p.name} has invalid value #{values[p.name]}")
+          raise validation_exception("Hardware profile property #{p.name} has invalid value #{values[p.name]}")
         end
       else
         if not p.options.empty? and p.valid_value?(values[p.name])
-          raise Failure.new(p, "Parameter #{p.name} has value #{values[p.name]} which is not in #{p.options.join(", ")}")
+          raise validation_exception("Parameter #{p.name} has value #{values[p.name]} which is not in #{p.options.join(", ")}")
         end
       end
     end
   end
 
+  def validation_exception(message)
+    Deltacloud::ExceptionHandler::ValidationFailure.new(StandardError.new(message))
+  end
+
 end
-- 
1.7.9.1


[PATCH core 2/6] Client: Added 'reinstall' rake task to make gem testing more smooth

Posted by mf...@redhat.com.
From: Michal Fojtik <mf...@redhat.com>


Signed-off-by: Michal fojtik <mf...@redhat.com>
---
 client/Rakefile |    7 +++++++
 1 files changed, 7 insertions(+), 0 deletions(-)

diff --git a/client/Rakefile b/client/Rakefile
index 6bf6260..c5a9f70 100644
--- a/client/Rakefile
+++ b/client/Rakefile
@@ -49,6 +49,13 @@ if available?('rspec')
   end
 end
 
+desc "Reinstall gem"
+task :reinstall do
+  puts %x{gem uninstall deltacloud-client --all -I -x}
+  puts %x{gem build deltacloud-client.gemspec}
+  puts %x{gem install deltacloud-client-*.gem --local}
+end
+
 desc "Setup Fixtures"
 task 'fixtures' do
   FileUtils.rm_rf( File.dirname( __FILE__ ) + '/specs/data' )
-- 
1.7.9.1