wraps instance_action in begin/rescue block to handle
  and gracefully output errors.

  Also throw an ArgumentError if instance_action is called
  without an id
---
 src/app/controllers/instance_controller.rb       |   77 ++++++++++++----------
 src/app/views/instance/instance_action.haml      |    2 +
 src/spec/controllers/instance_controller_spec.rb |   26 +++++++
 3 files changed, 71 insertions(+), 34 deletions(-)
 create mode 100644 src/app/views/instance/instance_action.haml
 create mode 100644 src/spec/controllers/instance_controller_spec.rb

diff --git a/src/app/controllers/instance_controller.rb 
b/src/app/controllers/instance_controller.rb
index ad90404..2f2f254 100644
--- a/src/app/controllers/instance_controller.rb
+++ b/src/app/controllers/instance_controller.rb
@@ -125,45 +125,54 @@ class InstanceController < ApplicationController
       end
     end
 
-    @instance = Instance.find((params[:id] || []).first)
-    require_privilege(Privilege::INSTANCE_CONTROL,@instance.pool)
-
-    if params[:instance_details]
-      render :action => 'show'
-      return
-    end
-
-    if params[:remove_failed]
-      action = remove_failed
-    else
-      # action list will be longer (restart, start, stop..)
-      action = if params[:shutdown]
-                 'stop'
-               #elsif params[:restart]
-               #  'restart'
-               end
-
-      unless @instance.valid_action?(action)
-        raise ActionError.new("'#{action}' is an invalid action.")
+    begin
+      if params[:id].nil? || params[:id].blank?
+        raise ArgumentError.new("You must select an instance to perform this 
action")
       end
 
-      # not sure if task is used as everything goes through condor
-      #permissons check here
-      @task = @instance.queue_action(@current_user, action)
-      unless @task
-        raise ActionError.new("#{action} cannot be performed on this 
instance.")
+      @instance = Instance.find(params[:id])
+      require_privilege(Privilege::INSTANCE_CONTROL,@instance.pool)
+
+      if params[:instance_details]
+        render :action => 'show'
+        return
       end
 
-      case action
-        when 'stop'
-          condormatic_instance_stop(@task)
-        when 'destroy'
-          condormatic_instance_destroy(@task)
-        when 'start'
-          condormatic_instance_create(@task)
-        else
-          raise ActionError.new("Sorry, action '#{action}' is currently not 
supported by condor backend.")
+      if params[:remove_failed]
+        action = remove_failed
+      else
+        # action list will be longer (restart, start, stop..)
+        action = if params[:shutdown]
+                   'stop'
+                 #elsif params[:restart]
+                 #  'restart'
+                 end
+
+        unless @instance.valid_action?(action)
+          raise ActionError.new("'#{action}' is an invalid action.")
+        end
+
+        # not sure if task is used as everything goes through condor
+        #permissons check here
+        @task = @instance.queue_action(@current_user, action)
+        unless @task
+          raise ActionError.new("#{action} cannot be performed on this 
instance.")
+        end
+
+        case action
+          when 'stop'
+            condormatic_instance_stop(@task)
+          when 'destroy'
+            condormatic_instance_destroy(@task)
+          when 'start'
+            condormatic_instance_create(@task)
+          else
+            raise ActionError.new("Sorry, action '#{action}' is currently not 
supported by condor backend.")
+        end
       end
+    rescue Exception => e
+      @error = e.to_s
+      return
     end
 
     flash[:notice] = "#[email protected]}: #{action} was successfully queued."
diff --git a/src/app/views/instance/instance_action.haml 
b/src/app/views/instance/instance_action.haml
new file mode 100644
index 0000000..509163b
--- /dev/null
+++ b/src/app/views/instance/instance_action.haml
@@ -0,0 +1,2 @@
+- # will only be rendered if there is an error
+= @error
diff --git a/src/spec/controllers/instance_controller_spec.rb 
b/src/spec/controllers/instance_controller_spec.rb
new file mode 100644
index 0000000..f5aec4f
--- /dev/null
+++ b/src/spec/controllers/instance_controller_spec.rb
@@ -0,0 +1,26 @@
+require 'spec_helper'
+
+describe InstanceController do
+  fixtures :all
+  before(:each) do
+    @admin_permission = Factory :admin_permission
+    @admin = @admin_permission.user
+    activate_authlogic
+  end
+
+  it "should render error if instance_action is called without an instance id" 
do
+    UserSession.create(@admin)
+    post :instance_action
+    response.should render_template("instance/instance_action.haml")
+    assigns[:error].should == "You must select an instance to perform this 
action"
+  end
+
+  it "should render error if instance_action is called with invalid action" do
+    UserSession.create(@admin)
+    @inst = Factory :mock_running_instance
+    @inst.save!
+    post :instance_action, :id => @inst.id
+    response.should render_template("instance/instance_action.haml")
+    assigns[:error].should == "'' is an invalid action."
+  end
+end
-- 
1.7.2.3

_______________________________________________
deltacloud-devel mailing list
[email protected]
https://fedorahosted.org/mailman/listinfo/deltacloud-devel

Reply via email to