From: martyntaylor <[email protected]>

---
 src/app/controllers/pool_controller.rb |   30 +++++++++++++++++++++++-------
 src/app/models/instance_observer.rb    |    4 ++--
 src/app/models/quota.rb                |    1 +
 src/app/models/task.rb                 |    3 ++-
 src/spec/models/task_spec.rb           |    5 -----
 5 files changed, 28 insertions(+), 15 deletions(-)

diff --git a/src/app/controllers/pool_controller.rb 
b/src/app/controllers/pool_controller.rb
index 7ff39b7..75e2bcd 100644
--- a/src/app/controllers/pool_controller.rb
+++ b/src/app/controllers/pool_controller.rb
@@ -160,15 +160,31 @@ class PoolController < ApplicationController
 
     if !Quota.exists?(@pool.quota_id)
       require_privilege(Privilege::QUOTA_MODIFY, @pool)
-
       @quota = Quota.new(params[:quota])
-      @quota.save!
-
-      @pool.quota_id = @quota.id
-      @pool.save!
 
-      flash[:notice] = "Quota Created!"
-      redirect_to :action => 'quota', :id => @pool
+      # Populate Current Pool totals to Quota
+      @pool.instances.each do |instance|
+        hwp = HardwareProfile.find(instance.hardware_profile_id)
+        if instance.state == Instance::STATE_RUNNING
+          @quota.running_instances += 1
+          @quota.running_memory += hwp.memory
+          #TODO Add CPUs
+        end
+
+        if InstanceObserver::ACTIVE_STATES.include?(instance.state)
+          @quota.total_instances += 1
+          @quota.total_storage += hwp.storage
+        end
+      end
+
+      if @quota.save
+        flash[:notice] = "Quota Created!"
+        @pool.quota_id = @quota.id
+        @pool.save!
+        redirect_to :action => 'quota', :id => @pool
+      else
+        render :action => :new_quota
+      end
     else
       errors.add("quota_id", "There already exists a quota for this pool")
       render :action => :new_quota
diff --git a/src/app/models/instance_observer.rb 
b/src/app/models/instance_observer.rb
index 9b0461a..725a3a7 100644
--- a/src/app/models/instance_observer.rb
+++ b/src/app/models/instance_observer.rb
@@ -35,9 +35,9 @@ class InstanceObserver < ActiveRecord::Observer
 
     hwp = HardwareProfile.find(an_instance.hardware_profile_id)
     pool = Pool.find(an_instance.pool_id)
-    quota = Quota.find(pool.quota_id)
 
-    if(quota)
+    if(Quota.exists?(pool.quota_id))
+      quota = Quota.find(pool.quota_id)
       if state_to == Instance::STATE_RUNNING
         #TODO update running cpus
         quota.running_instances += 1
diff --git a/src/app/models/quota.rb b/src/app/models/quota.rb
index 5e16065..57662a7 100644
--- a/src/app/models/quota.rb
+++ b/src/app/models/quota.rb
@@ -37,6 +37,7 @@ class Quota < ActiveRecord::Base
   validates_numericality_of :maximum_total_storage
   validates_numericality_of :maximum_total_instances
 
+
   def can_create_instance?(instance)
     hwp = HardwareProfile.find(instance.hardware_profile_id)
 
diff --git a/src/app/models/task.rb b/src/app/models/task.rb
index ebbde40..feec26a 100644
--- a/src/app/models/task.rb
+++ b/src/app/models/task.rb
@@ -116,7 +116,8 @@ class Task < ActiveRecord::Base
 
   def validate
     errors.add("created_at", "Task started but does not have the creation time 
set") if time_started and created_at.nil?
-    errors.add("time_started", "Task ends but does not have the start time 
set") if time_ended and time_started.nil?
+    # Removed check on time_started exisiting. if time_ended does.  This can 
now occur, when the task fails before is starts.  e.g. When Over Qutoa
+    #errors.add("time_started", "Task ends but does not have the start time 
set") if time_ended and time_started.nil?
     errors.add("time_ended", "Tasks ends before it's started") unless 
time_ended.nil? or time_started.nil? or time_ended > time_started
     errors.add("time_started", "Tasks starts before it's created") unless 
time_started.nil? or created_at.nil? or time_started > created_at
   end
diff --git a/src/spec/models/task_spec.rb b/src/spec/models/task_spec.rb
index d392cb2..c5e7188 100644
--- a/src/spec/models/task_spec.rb
+++ b/src/spec/models/task_spec.rb
@@ -43,11 +43,6 @@ describe Task do
     @task.should_not be_valid
   end
 
-  it "should have 'started' time set if it ended" do
-    @task.attributes = @valid_attributes.except :time_started
-    @task.should_not be_valid
-  end
-
   it "should not be valid if it started before it was created" do
     @task.attributes = @valid_attributes
     @task.time_started = @task.created_at - 1.minute
-- 
1.6.6.1

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

Reply via email to