>From a60b4323cfe6018b319be67535fcb4fb46c8b708 Mon Sep 17 00:00:00 2001
From: martyntaylor <mtaylor@redhat.com>
Date: Thu, 28 Oct 2010 14:42:01 +0100
Subject: [PATCH aggregator] BZ#644534 Added check for Quota before creating instance

https://bugzilla.redhat.com/show_bug.cgi?id=644534

This patch adds a quota check to the webapp, so the user
will be notified that their instance cannot start until
they have free quota, but in the background condor will
continue to try to start the instance until such quota is
available. Instance will show in list as 'new' until
condor can do something with it.
---
 src/app/controllers/instance_controller.rb |   28 +++++++++++++++-------------
 src/app/models/quota.rb                    |   20 ++++++++++++--------
 2 files changed, 27 insertions(+), 21 deletions(-)

diff --git a/src/app/controllers/instance_controller.rb b/src/app/controllers/instance_controller.rb
index ad90404..24f263d 100644
--- a/src/app/controllers/instance_controller.rb
+++ b/src/app/controllers/instance_controller.rb
@@ -98,20 +98,22 @@ class InstanceController < ApplicationController
                       Pool.find(@instance.pool_id))
     #FIXME: This should probably be in a transaction
     if @instance.save!
-
-      @task = InstanceTask.new({:user        => current_user,
-                                :task_target => @instance,
-                                :action      => InstanceTask::ACTION_CREATE})
-      if @task.save
-        condormatic_instance_create(@task)
-        flash[:notice] = "Instance added."
-        redirect_to :action => 'index'
-      else
-        @pool = @instance.pool
-        render :action => 'configure'
-      end
+        @task = InstanceTask.new({:user        => current_user,
+                                  :task_target => @instance,
+                                  :action      => InstanceTask::ACTION_CREATE})
+        if @task.save
+          condormatic_instance_create(@task)
+          if Quota.can_start_instance?(@instance, nil)
+            flash[:notice] = "Instance added."
+          else
+            flash[:warning] = "Quota Exceeded: Instance will not start until you have free quota"
+          end
+        else
+          @pool = @instance.pool
+          render :action => 'configure'
+        end
+      redirect_to :action => 'index'
     else
-      #@pool = Pool.find(@instance.pool_id)
       @hardware_profiles = HardwareProfile.all
       render :action => 'configure'
     end
diff --git a/src/app/models/quota.rb b/src/app/models/quota.rb
index 83c20c4..49cd0ce 100644
--- a/src/app/models/quota.rb
+++ b/src/app/models/quota.rb
@@ -49,10 +49,12 @@ class Quota < ActiveRecord::Base
 
   def self.can_create_instance?(instance, cloud_account)
     [instance.owner, instance.pool, cloud_account].each do |parent|
-      quota = Quota.find(parent.quota_id)
-      potential_total_instances = quota.total_instances + 1
-      if !Quota.no_limit(quota.maximum_total_instances) && (quota.maximum_total_instances < potential_total_instances)
-        return false
+      if parent
+        quota = Quota.find(parent.quota_id)
+        potential_total_instances = quota.total_instances + 1
+        if !Quota.no_limit(quota.maximum_total_instances) && (quota.maximum_total_instances < potential_total_instances)
+          return false
+        end
       end
     end
     return true
@@ -60,10 +62,12 @@ class Quota < ActiveRecord::Base
 
   def self.can_start_instance?(instance, cloud_account)
     [instance.owner, instance.pool, cloud_account].each do |parent|
-      quota = Quota.find(parent.quota_id)
-      potential_running_instances = quota.running_instances + 1
-      if !Quota.no_limit(quota.maximum_running_instances) && quota.maximum_running_instances < potential_running_instances
-        return false
+      if parent
+        quota = Quota.find(parent.quota_id)
+        potential_running_instances = quota.running_instances + 1
+        if !Quota.no_limit(quota.maximum_running_instances) && quota.maximum_running_instances < potential_running_instances
+          return false
+        end
       end
     end
     return true
-- 
1.7.2.3

