incubator-deltacloud-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfoj...@redhat.com
Subject [PATCH core 1/3] Added 405 HTTP code when calling invalid instance action
Date Wed, 03 Nov 2010 13:46:24 GMT
From: Michal Fojtik <mfojtik@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


Mime
View raw message