From: martyntaylor <[email protected]>

---
 src/app/controllers/quota_controller.rb        |   73 +++++++----------------
 src/app/models/quota.rb                        |   60 +++++++++++--------
 src/db/migrate/20090802000000_create_quotas.rb |   10 ++--
 3 files changed, 62 insertions(+), 81 deletions(-)

diff --git a/src/app/controllers/quota_controller.rb 
b/src/app/controllers/quota_controller.rb
index 83e538e..c462f45 100644
--- a/src/app/controllers/quota_controller.rb
+++ b/src/app/controllers/quota_controller.rb
@@ -30,51 +30,6 @@ class QuotaController < ApplicationController
     require_privilege(Privilege::QUOTA_VIEW, @parent)
   end
 
-  def new
-    @parent = get_parent_object(params)
-    @parent_type = params[:parent_type]
-    @name = get_parent_name(@parent, @parent_type)
-    require_privilege(Privilege::QUOTA_MODIFY, @parent)
-  end
-
-  def create
-    #TODO Should be in Transaction
-    @parent = get_parent_object(params)
-    @parent_type = params[:parent_type]
-    @quota = @parent.quota
-    if !...@quota
-      require_privilege(Privilege::QUOTA_MODIFY, @parent)
-      @quota = Quota.new(params[:quota])
-
-      # Populate Current Pool totals to Quota
-      @parent.instances.each do |instance|
-        hwp = HardwareProfile.find(instance.hardware_profile_id)
-        if instance.state == Instance::STATE_RUNNING
-          @quota.running_instances += 1
-          @quota.running_memory = @quota.running_memory.to_f + 
hwp.memory.value.to_f
-          @quota.running_cpus = @quota.running_cpus.to_f + hwp.cpu.value.to_f
-        end
-
-        if InstanceObserver::ACTIVE_STATES.include?(instance.state)
-          @quota.total_instances += 1
-          @quota.total_storage = @quota.total_storage.to_f + 
hwp.storage.value.to_f
-        end
-      end
-
-      if @quota.save
-        flash[:notice] = "Quota Created!"
-        @parent.quota_id = @quota.id
-        @parent.save!
-        redirect_to :action => 'show', :id => @parent, :parent_type => 
@parent_type
-      else
-        render :action => :new
-      end
-    else
-      @parent.errors.add("quota_id", "There already exists a quota for this " 
+ @parent_type)
-      render :action => :new
-    end
-  end
-
   def edit
     @parent = get_parent_object(params)
     @parent_type = params[:parent_type]
@@ -99,15 +54,22 @@ class QuotaController < ApplicationController
     end
   end
 
-  def delete
-    @parent = get_parent_object(params)
+  def reset
+    @parent = @parent = get_parent_object(params)
     @parent_type = params[:parent_type]
-    @quota = @parent.quota
     require_privilege(Privilege::QUOTA_MODIFY, @parent)
-    if @quota.delete
-      flash[:notice] = "Quota Deleted!"
-      redirect_to :action => 'show', :id => @parent, :parent_type => 
@parent_type
+
+    @quota = @parent.quota
+    @quota.maximum_running_cpus = Quota::NO_LIMIT
+    @quota.maximum_running_instances = Quota::NO_LIMIT
+    @quota.maximum_running_memory = Quota::NO_LIMIT
+    @quota.maximum_total_instances = Quota::NO_LIMIT
+    @quota.maximum_total_storage  = Quota::NO_LIMIT
+
+    if @quota.save!
+      flash[:notice] = "Quota updated!"
     end
+      redirect_to :action => 'show', :id => @parent, :parent_type => 
@parent_type
   end
 
   private
@@ -129,5 +91,14 @@ class QuotaController < ApplicationController
     #TODO Throw no match to pool or cloud account exception
   end
 
+  def check_params_infinite_limits(params)
+    params.each_pair do |key, value|
+      if value == ""
+        params[key] = Quota::NO_LIMIT
+      end
+    end
+    return params
+  end
+
 
 end
diff --git a/src/app/models/quota.rb b/src/app/models/quota.rb
index 43ab9a2..324a384 100644
--- a/src/app/models/quota.rb
+++ b/src/app/models/quota.rb
@@ -20,33 +20,24 @@
 # Likewise, all the methods added will be available for all controllers.
 
 class Quota < ActiveRecord::Base
-  has_one :pool
-  has_one :cloud_account
-
-  validates_presence_of :maximum_running_instances
-  validates_presence_of :maximum_running_memory
-  validates_presence_of :maximum_running_cpus
 
-  validates_presence_of :maximum_total_storage
-  validates_presence_of :maximum_total_instances
+  QuotaResource = Struct.new(:name, :used, :max, :available, :unit)
 
-  validates_numericality_of :maximum_running_instances
-  validates_numericality_of :maximum_running_memory
-  validates_numericality_of :maximum_running_cpus
-
-  validates_numericality_of :maximum_total_storage
-  validates_numericality_of :maximum_total_instances
+  NO_LIMIT = nil
 
+  has_one :pool
+  has_one :cloud_account
 
   def can_create_instance?(instance)
-    # TODO Fix: When this returns failed, instance gets deleted at some point 
from database.  It should be kept for audit
     hwp = instance.hardware_profile
 
     potential_total_storage = total_storage.to_f + hwp.storage.value.to_f
     potential_total_instances = total_instances + 1
 
-    if maximum_total_instances >= potential_total_instances && 
maximum_total_storage.to_f >= potential_total_storage.to_f
-      return true
+    # check for no quota
+    if Quota.no_limit(maximum_total_instances) || maximum_total_instances >= 
potential_total_instances &&
+       Quota.no_limit(maximum_total_storage) || maximum_total_storage.to_f >= 
potential_total_storage.to_f
+         return true
     end
     return false
   end
@@ -58,17 +49,36 @@ class Quota < ActiveRecord::Base
     potential_running_memory = running_memory.to_f + hwp.memory.value.to_f
     potential_running_cpus = running_cpus.to_f + hwp.cpu.value.to_f
 
-    if maximum_running_instances >= potential_running_instances && 
maximum_running_memory.to_f >= potential_running_memory && 
maximum_running_cpus.to_f >= potential_running_cpus
-      return true
+    if Quota.no_limit(maximum_running_instances) || maximum_running_instances 
>= potential_running_instances &&
+       Quota.no_limit(maximum_running_memory) || maximum_running_memory.to_f 
>= potential_running_memory &&
+       Quota.no_limit(maximum_running_cpus) || maximum_running_cpus.to_f >= 
potential_running_cpus
+         return true
     end
     return false
   end
 
-  def validate
-    errors.add("maximum_running_instances", "cannot be less than the current 
running instances") if maximum_running_instances < running_instances
-    errors.add("maximum_running_memory", "cannot be less than the current 
running memory") if maximum_running_memory.to_f < running_memory.to_f
-    errors.add("maximum_running_cpus", "cannot be less than the current 
running CPUs") if maximum_running_cpus.to_f < running_cpus.to_f
-    errors.add("maximum_total_storage", "cannot be less than the current total 
storage") if maximum_total_storage.to_f < total_storage.to_f
-    errors.add("maximum_total_instances", "cannot be less than the current 
total instances") if maximum_total_instances < total_instances
+  def quota_resources()
+    quota_resources =  {"running_instances" => QuotaResource.new("Running 
Instances", running_instances, maximum_running_instances, 
maximum_running_instances.to_f - running_instances.to_f, ""),
+            "running_memory" => QuotaResource.new("Running Memory", 
running_memory, maximum_running_memory, maximum_running_memory.to_f - 
running_memory.to_f, "MB"),
+            "running_cpus" => QuotaResource.new("Running CPUs", running_cpus, 
maximum_running_cpus, maximum_running_cpus.to_f - running_cpus.to_f, ""),
+            "total_instances" => QuotaResource.new("Total Instances", 
total_instances, maximum_total_instances, maximum_total_instances.to_f - 
total_instances.to_f, ""),
+            "total_storage" => QuotaResource.new("Total Storage", 
total_storage, maximum_total_storage, maximum_total_storage.to_f - 
total_storage.to_f, "GB")}
+
+    quota_resources.each_value do |quota_resource|
+      if Quota.no_limit(quota_resource.max)
+        quota_resource.max = "No Limit"
+        quota_resource.available = "N\A"
+      end
+    end
+
+    return quota_resources
+  end
+
+  def self.no_limit(resource)
+    if resource.to_s == NO_LIMIT.to_s
+      return true
+    end
+    return false
   end
+
 end
diff --git a/src/db/migrate/20090802000000_create_quotas.rb 
b/src/db/migrate/20090802000000_create_quotas.rb
index 9cdda4f..bfd195b 100644
--- a/src/db/migrate/20090802000000_create_quotas.rb
+++ b/src/db/migrate/20090802000000_create_quotas.rb
@@ -25,11 +25,11 @@ class CreateQuotas < ActiveRecord::Migration
       t.integer :running_instances, :default => 0
       t.string  :total_storage, :default => 0
       t.integer :total_instances, :default => 0
-      t.string :maximum_running_cpus
-      t.string  :maximum_running_memory
-      t.integer :maximum_running_instances
-      t.string  :maximum_total_storage
-      t.integer :maximum_total_instances
+      t.string :maximum_running_cpus, :default => nil
+      t.string  :maximum_running_memory, :default => nil
+      t.integer :maximum_running_instances, :default => nil
+      t.string  :maximum_total_storage, :default => nil
+      t.integer :maximum_total_instances, :default => nil
       t.integer :lock_version, :default => 0
       t.timestamps
     end
-- 
1.6.6.1

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

Reply via email to