From: martyntaylor <[email protected]>

---
 src/app/controllers/pool_controller.rb         |   30 +++++++++++-
 src/app/models/instance_observer.rb            |    9 ++--
 src/app/views/pool/new_quota.html.erb          |   18 +++++++
 src/app/views/pool/quota.html.erb              |    3 +-
 src/config/environment.rb                      |    2 +-
 src/db/migrate/20090802000000_create_quotas.rb |    6 --
 src/spec/models/instance_observer_spec.rb      |   60 ++++++++++++++++++------
 7 files changed, 99 insertions(+), 29 deletions(-)
 create mode 100644 src/app/views/pool/new_quota.html.erb

diff --git a/src/app/controllers/pool_controller.rb 
b/src/app/controllers/pool_controller.rb
index d5fc8af..632d0cb 100644
--- a/src/app/controllers/pool_controller.rb
+++ b/src/app/controllers/pool_controller.rb
@@ -143,8 +143,32 @@ class PoolController < ApplicationController
 
   def quota
     @pool = Pool.find(params[:id])
-    puts @pool.id
-    @quota = Quota.find(:first, :conditions => { :pool_id => @pool })
+    if Quota.exists?(@pool.quota_id)
+      @quota = Quota.find(@pool.quota_id)
+    end
+    require_privilege(Privilege::QUOTA_VIEW, @pool)
+  end
+
+  def new_quota
+    @pool = Pool.find(params[:id])
+    require_privilege(Privilege::POOL_MODIFY, @pool)
+  end
+
+  def create_quota
+    if !Quota.exists?(@pool.quota_id)
+      @pool = Pool.find(params[:pool][:id])
+      require_privilege(Privilege::POOL_MODIFY, @pool)
+
+      @quota = Quota.new(params[:quota])
+      @quota.save!
+
+      @pool.quota_id = @quota
+      @pool.save!
+
+      redirect_to :action => 'quota', :id => @pool
+    else
+      errors.add("quota_id", "There already exists a quota for this pool")
+    end
   end
 
-end
+end
\ No newline at end of file
diff --git a/src/app/models/instance_observer.rb 
b/src/app/models/instance_observer.rb
index ab1a095..9b0461a 100644
--- a/src/app/models/instance_observer.rb
+++ b/src/app/models/instance_observer.rb
@@ -1,6 +1,6 @@
 class InstanceObserver < ActiveRecord::Observer
 
-  ACTIVE_STATES = [ Instance::STATE_PENDING, Instance::STATE_RUNNING, 
Instance::STATE_SHUTTING_DOWN ]
+  ACTIVE_STATES = [ Instance::STATE_NEW, Instance::STATE_PENDING, 
Instance::STATE_RUNNING, Instance::STATE_SHUTTING_DOWN, Instance::STATE_STOPPED 
]
 
   def before_save(an_instance)
     if an_instance.changed?
@@ -34,7 +34,8 @@ class InstanceObserver < ActiveRecord::Observer
   def update_quota(state_from, state_to, an_instance)
 
     hwp = HardwareProfile.find(an_instance.hardware_profile_id)
-    quota = Quota.find(:first, :conditions => { :pool_id => 
an_instance.pool_id })
+    pool = Pool.find(an_instance.pool_id)
+    quota = Quota.find(pool.quota_id)
 
     if(quota)
       if state_to == Instance::STATE_RUNNING
@@ -43,7 +44,7 @@ class InstanceObserver < ActiveRecord::Observer
         quota.running_memory += hwp.memory
       elsif state_from == Instance::STATE_RUNNING
         #TODO update running cpus
-        quota.running_instances --
+        quota.running_instances -= 1
         quota.running_memory -= hwp.memory
       end
 
@@ -55,7 +56,7 @@ class InstanceObserver < ActiveRecord::Observer
         quota.total_instances -= 1
       end
 
-      quota.save
+      quota.save!
     end
   end
 end
diff --git a/src/app/views/pool/new_quota.html.erb 
b/src/app/views/pool/new_quota.html.erb
new file mode 100644
index 0000000..4290862
--- /dev/null
+++ b/src/app/views/pool/new_quota.html.erb
@@ -0,0 +1,18 @@
+<div class="dcloud_form">
+  <%= error_messages_for 'pool' %>
+
+  <h2>Create Quota for Pool: <%= @pool.name %></legend></h2><br />
+
+  <% form_tag :action => 'create_quota' do -%>
+    <%=hidden_field :pool, :id %>
+    <ul>
+      <li><label>Max Running Instances</label><%= text_field :quota, 
:maximum_running_instances %></li>
+         <li><label>Max Running Memory</label><%= text_field :quota, 
:maximum_running_memory %></li>
+         <li><label>Max Running CPUs</label><%= text_field :quota, 
:maximum_running_cpus %></li>
+         <li><label>Max Total Instances</label><%= text_field :quota, 
:maximum_total_instances %></li>
+         <li><label>Max Total Storage</label><%= text_field :quota, 
:maximum_total_storage %></li>
+    </ul>
+    </fieldset>
+    <%= submit_tag "Save", :class => "submit" %>
+  <% end %>
+</div>
\ No newline at end of file
diff --git a/src/app/views/pool/quota.html.erb 
b/src/app/views/pool/quota.html.erb
index edd4463..09b227e 100644
--- a/src/app/views/pool/quota.html.erb
+++ b/src/app/views/pool/quota.html.erb
@@ -1,5 +1,6 @@
-<% if @quota == nil %>
+<% if !...@quota %>
 <h1>There is no quota associated with this pool</h1>
+<%= link_to "Create a Quota", {:controller => "pool", :action => "new_quota", 
:id => @pool}, :class=>"actionlink"%>
 <% else %>
     <table>
       <thead>
diff --git a/src/config/environment.rb b/src/config/environment.rb
index 1721126..9f0fa4f 100644
--- a/src/config/environment.rb
+++ b/src/config/environment.rb
@@ -43,7 +43,7 @@ Rails::Initializer.run do |config|
   config.gem "gettext",     :lib => "gettext_rails"
   config.gem "gettext", :lib => "gettext_activerecord"
   config.gem "authlogic"
-  config.gem "deltacloud-client", :lib => "deltacloud"
+  config.gem "deltacloud-cliesrc/config/environment.rbnt", :lib => "deltacloud"
   config.gem "haml"
   config.gem "will_paginate"
 
diff --git a/src/db/migrate/20090802000000_create_quotas.rb 
b/src/db/migrate/20090802000000_create_quotas.rb
index 29cc5bf..01c885d 100644
--- a/src/db/migrate/20090802000000_create_quotas.rb
+++ b/src/db/migrate/20090802000000_create_quotas.rb
@@ -20,12 +20,6 @@
 class CreateQuotas < ActiveRecord::Migration
   def self.up
     create_table :quotas do |t|
-      t.integer :running_cpus
-      t.integer :running_memory
-      t.integer :running_instances
-      t.integer :total_storage
-      t.integer :total_instances
-      t.integer :pool_id
       t.integer :running_cpus, :default => 0
       t.integer :running_memory, :default => 0
       t.integer :running_instances, :default => 0
diff --git a/src/spec/models/instance_observer_spec.rb 
b/src/spec/models/instance_observer_spec.rb
index 76514e5..7051955 100644
--- a/src/spec/models/instance_observer_spec.rb
+++ b/src/spec/models/instance_observer_spec.rb
@@ -4,7 +4,10 @@ describe InstanceObserver do
 
   before(:each) do
    @timestamp = Time.now
-   @instance = Factory :new_instance
+   @hwp = Factory :mock_hwp1
+   @pool = Factory :pool
+   @quota = Factory(:quota, :pool_id => @pool.id)
+   @instance = Factory(:new_instance, :pool => @pool, :hardware_profile => 
@hwp)
   end
 
   it "should set started at timestamp when instance goes to state pending" do
@@ -55,7 +58,7 @@ describe InstanceObserver do
     sleep(1)
 
     @instance.state = Instance::STATE_SHUTTING_DOWN
-    @instance.save
+    @instance.save!
 
     @instance.acc_running_time.should >= 1
     @instance.acc_running_time.should <= 2
@@ -87,22 +90,51 @@ describe InstanceObserver do
     @instance.acc_stopped_time.should <= 2
   end
 
-  it "should update a pools quota when an associated instance state goes to 
running" do
-    hwp = Factory :mock_hwp1
-    pool = Factory :pool
-    quota = Factory(:quota, :pool_id => pool.id)
-    instance = Factory(:new_instance, :pool => pool, :hardware_profile => hwp)
+  it "should update quota when a new pool instance is created" do
+    @quota = Quota.find(@quota)
+
+    @quota.total_instances.should == 1
+    @quota.total_storage.should == @hwp.storage
+  end
+
+  it "should update quota when a pool instance goes into an inactive state" do
+    @quota = Quota.find(@quota)
+
+    @instance.state = Instance::STATE_CREATE_FAILED
+    @quota.total_instances.should == 0
+    @quota.total_storage.should == 0
+  end
+
+  it "should update a pools quota when an pool instance state goes to running" 
do
+    @quota = Quota.find(@quota)
+
+    @instance.state = Instance::STATE_RUNNING
+    @instance.save!
 
-    instance.state = Instance::STATE_RUNNING
-    instance.save
+    @quota = Quota.find(@quota.id)
+    @quota.running_instances.should == 1
+    @quota.running_memory.should == @hwp.memory
+    #TODO test for cpus
+
+    @quota.total_storage.should == @hwp.storage
+    @quota.total_instances.should == 1
+  end
+
+  it "should update a pools quota when an associated instance state goes from 
running another active state" do
+    @quota = Quota.find(@quota)
+
+    @instance.state = Instance::STATE_RUNNING
+    @instance.save!
+
+    @instance.state = Instance::STATE_SHUTTING_DOWN
+    @instance.save!
 
-    quota = Quota.find_by_id(quota.id)
-    quota.running_instances.should == 1
-    quota.running_memory.should == hwp.memory
     #TODO test for cpus
+    @quota.running_instances.should == 0
+    @quota.running_memory.should == 0
 
-    quota.total_storage.should == hwp.storage
-    quota.total_instances.should == 1
+    @quota.total_storage.should == @hwp.storage
+    @quota.total_instances.should == 1
   end
 
 end
\ No newline at end of file
-- 
1.6.6.1

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

Reply via email to