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 2010/11/03 14:46:23 UTC

Small core fixes

Hi,

Fixes include:

1) 405 code is returned from API when client request for an action which
   is not allowed/supported by instance and its state.
   So for example when you want to call 'stop' action on 'stopped' instance
   instead of returning 200, API will return 405 code.

2) Performance fix for cases when you have a lot of YAML files inside tmp dir.

3) Just some views/HAML files for 405 error.

 -- Michal


[PATCH core 3/3] Added XML and HTML representation of 405 error code

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

---
 server/views/errors/not_allowed.html.haml |    6 ++++++
 server/views/errors/not_allowed.xml.haml  |    2 ++
 2 files changed, 8 insertions(+), 0 deletions(-)
 create mode 100644 server/views/errors/not_allowed.html.haml
 create mode 100644 server/views/errors/not_allowed.xml.haml

diff --git a/server/views/errors/not_allowed.html.haml b/server/views/errors/not_allowed.html.haml
new file mode 100644
index 0000000..59e9fe9
--- /dev/null
+++ b/server/views/errors/not_allowed.html.haml
@@ -0,0 +1,6 @@
+%h1 Method Not Allowed
+
+%dl
+  %di
+    %dt Request URL
+    %dd= request.env['REQUEST_URI']
diff --git a/server/views/errors/not_allowed.xml.haml b/server/views/errors/not_allowed.xml.haml
new file mode 100644
index 0000000..3bad335
--- /dev/null
+++ b/server/views/errors/not_allowed.xml.haml
@@ -0,0 +1,2 @@
+%error{:url => "#{request.env['REQUEST_URI']}", :status => "#{response.status}"}
+  %message Method not allowed for this resource
-- 
1.7.2.3


[PATCH core 1/3] Added 405 HTTP code when calling invalid instance action

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

---
 .../lib/deltacloud/helpers/application_helper.rb   |    8 ++++++++
 server/lib/deltacloud/models/instance.rb           |   20 +++++++++++++++-----
 2 files changed, 23 insertions(+), 5 deletions(-)

diff --git a/server/lib/deltacloud/helpers/application_helper.rb b/server/lib/deltacloud/helpers/application_helper.rb
index c7fa481..189148c 100644
--- a/server/lib/deltacloud/helpers/application_helper.rb
+++ b/server/lib/deltacloud/helpers/application_helper.rb
@@ -102,6 +102,14 @@ module ApplicationHelper
   end
 
   def instance_action(name)
+    original_instance = driver.instance(credentials, :id => params[:id])
+
+    # If original instance doesn't include called action
+    # return with 405 error (Method is not Allowed)
+    unless driver.instance_actions_for(original_instance.state).include?(name.to_sym)
+      return report_error(405, 'not_allowed')
+    end
+
     @instance = driver.send(:"#{name}_instance", credentials, params["id"])
 
     return redirect(instances_url) if name.eql?(:destroy) or @instance.class!=Instance
diff --git a/server/lib/deltacloud/models/instance.rb b/server/lib/deltacloud/models/instance.rb
index 9cf69b8..0167331 100644
--- a/server/lib/deltacloud/models/instance.rb
+++ b/server/lib/deltacloud/models/instance.rb
@@ -29,10 +29,20 @@ class Instance < BaseModel
   attr_accessor :private_addresses
   attr_accessor :instance_profile
   attr_accessor :launch_time
- def initialize(init=nil)
-   super(init)
-   self.actions = [] if self.actions.nil?
-   self.public_addresses = [] if self.public_addresses.nil?
-   self.private_addresses = [] if self.private_addresses.nil?
+  
+  def initialize(init=nil)
+    super(init)
+    self.actions = [] if self.actions.nil?
+    self.public_addresses = [] if self.public_addresses.nil?
+    self.private_addresses = [] if self.private_addresses.nil?
   end
+
+  def method_missing(name, *args)
+    if name =~ /is_(\w+)\?/
+      return true if self.state.downcase.eql?($1)
+    else
+      raise NoMethodError
+    end
+  end
+
 end
-- 
1.7.2.3


Re: Small core fixes

Posted by Michal Fojtik <mf...@redhat.com>.
On 05/11/10 11:34 +0100, Ladislav Martincik wrote:
>
>On Nov 3, 2010, at 2:46 PM, mfojtik@redhat.com wrote:
>
>> Hi,
>>
>> Fixes include:
>>
>> 1) 405 code is returned from API when client request for an action which
>>   is not allowed/supported by instance and its state.
>>   So for example when you want to call 'stop' action on 'stopped' instance
>>   instead of returning 200, API will return 405 code.
>>
>> 2) Performance fix for cases when you have a lot of YAML files inside tmp dir.
>>
>> 3) Just some views/HAML files for 405 error.
>>
>> -- Michal
>>
>
>ACK to all 3 patches

Thanks! Pushing into SVN.

-- 
--------------------------------------------------------
Michal Fojtik, mfojtik@redhat.com
Deltacloud API: http://deltacloud.org
--------------------------------------------------------

Re: Small core fixes

Posted by Ladislav Martincik <lm...@redhat.com>.
On Nov 3, 2010, at 2:46 PM, mfojtik@redhat.com wrote:

> Hi,
> 
> Fixes include:
> 
> 1) 405 code is returned from API when client request for an action which
>   is not allowed/supported by instance and its state.
>   So for example when you want to call 'stop' action on 'stopped' instance
>   instead of returning 200, API will return 405 code.
> 
> 2) Performance fix for cases when you have a lot of YAML files inside tmp dir.
> 
> 3) Just some views/HAML files for 405 error.
> 
> -- Michal
> 

ACK to all 3 patches

Re: [PATCH core 2/3] Fix Mock driver speed in case you have a lot of YAML files

Posted by Michal Fojtik <mf...@redhat.com>.
On 03/11/10 14:46 +0100, mfojtik@redhat.com wrote:
>From: Michal Fojtik <mf...@redhat.com>
>
>---
> server/lib/deltacloud/drivers/mock/mock_driver.rb |   13 ++++++++++++-
> 1 files changed, 12 insertions(+), 1 deletions(-)
>
>diff --git a/server/lib/deltacloud/drivers/mock/mock_driver.rb b/server/lib/deltacloud/drivers/mock/mock_driver.rb
>index 847319b..52c63b7 100644
>--- a/server/lib/deltacloud/drivers/mock/mock_driver.rb
>+++ b/server/lib/deltacloud/drivers/mock/mock_driver.rb
>@@ -130,12 +130,23 @@ class MockDriver < Deltacloud::BaseDriver
>   #
>   # Instances
>   #
>+  require 'ruby-prof'

^^ Please ignore this line, it will be removed before push ;-)
>
>+
>+  def instance(credentials, opts={})
>+    check_credentials( credentials )
>+    instance_filename = File.join(@storage_root, 'instances', "#{opts[:id]}.yml")
>+    return nil unless File.exists?(instance_filename)
>+    instance = YAML::load_file(instance_filename)
>+    instance[:actions] = instance_actions_for( instance[:state] )
>+    instance[:id] = File::basename(instance_filename, ".yml")
>+    Instance.new(instance)
>+  end
> 
>   def instances(credentials, opts=nil)
>     check_credentials( credentials )
>     instances = []
>     Dir[ "#{@storage_root}/instances/*.yml" ].each do |instance_file|
>-      instance = YAML.load( File.read( instance_file ) )
>+      instance = YAML::load_file(instance_file)
>       if ( instance[:owner_id] == credentials.user )
>         instance[:id] = File.basename( instance_file, ".yml" )
>         instance[:actions] = instance_actions_for( instance[:state] )
>-- 
>1.7.2.3
>

-- 
--------------------------------------------------------
Michal Fojtik, mfojtik@redhat.com
Deltacloud API: http://deltacloud.org
--------------------------------------------------------

[PATCH core 2/3] Fix Mock driver speed in case you have a lot of YAML files

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

---
 server/lib/deltacloud/drivers/mock/mock_driver.rb |   13 ++++++++++++-
 1 files changed, 12 insertions(+), 1 deletions(-)

diff --git a/server/lib/deltacloud/drivers/mock/mock_driver.rb b/server/lib/deltacloud/drivers/mock/mock_driver.rb
index 847319b..52c63b7 100644
--- a/server/lib/deltacloud/drivers/mock/mock_driver.rb
+++ b/server/lib/deltacloud/drivers/mock/mock_driver.rb
@@ -130,12 +130,23 @@ class MockDriver < Deltacloud::BaseDriver
   #
   # Instances
   #
+  require 'ruby-prof'
+
+  def instance(credentials, opts={})
+    check_credentials( credentials )
+    instance_filename = File.join(@storage_root, 'instances', "#{opts[:id]}.yml")
+    return nil unless File.exists?(instance_filename)
+    instance = YAML::load_file(instance_filename)
+    instance[:actions] = instance_actions_for( instance[:state] )
+    instance[:id] = File::basename(instance_filename, ".yml")
+    Instance.new(instance)
+  end
 
   def instances(credentials, opts=nil)
     check_credentials( credentials )
     instances = []
     Dir[ "#{@storage_root}/instances/*.yml" ].each do |instance_file|
-      instance = YAML.load( File.read( instance_file ) )
+      instance = YAML::load_file(instance_file)
       if ( instance[:owner_id] == credentials.user )
         instance[:id] = File.basename( instance_file, ".yml" )
         instance[:actions] = instance_actions_for( instance[:state] )
-- 
1.7.2.3