[PATCH] added specs for edit cloud account changes
by Mo Morsi
---
.../controllers/cloud_accounts_controller_spec.rb | 51 ++++++++++++++++++++
src/spec/factories/cloud_account.rb | 8 +++-
src/spec/factories/provider.rb | 2 +-
src/spec/models/cloud_account_spec.rb | 21 ++++++++
4 files changed, 80 insertions(+), 2 deletions(-)
create mode 100644 src/spec/controllers/cloud_accounts_controller_spec.rb
create mode 100644 src/spec/models/cloud_account_spec.rb
diff --git a/src/spec/controllers/cloud_accounts_controller_spec.rb b/src/spec/controllers/cloud_accounts_controller_spec.rb
new file mode 100644
index 0000000..f1d4a0d
--- /dev/null
+++ b/src/spec/controllers/cloud_accounts_controller_spec.rb
@@ -0,0 +1,51 @@
+require 'spec_helper'
+
+describe CloudAccountsController do
+
+ fixtures :all
+ before(:each) do
+ @cloud_account = Factory :mock_cloud_account
+ @provider = @cloud_account.provider
+
+ @admin_permission = Permission.create :role => Role.find(:first, :conditions => ['name = ?', 'Provider Administrator']),
+ :permission_object => @provider,
+ :user => Factory(:provider_admin_user)
+ @admin = @admin_permission.user
+ activate_authlogic
+ end
+
+ it "should permit users with account modify permission to access edit cloud account interface" do
+ UserSession.create(@admin)
+ get :edit, :id => @cloud_account.id
+ response.should be_success
+ response.should render_template("edit")
+ end
+
+ it "should allow users with account modify permission to update a cloud account" do
+ UserSession.create(@admin)
+ post :update, :cloud_account => { :id => @cloud_account.id, :password => 'foobar' }
+ response.should redirect_to("http://test.host/provider/accounts/#{@provider.id}")
+ CloudAccount.find((a)cloud_account.id).password.should == "foobar"
+ end
+
+ it "should allow users with account modify permission to delete a cloud account" do
+ UserSession.create(@admin)
+ lambda do
+ post :destroy, :id => @cloud_account.id
+ end.should change(CloudAccount, :count).by(-1)
+ response.should redirect_to("http://test.host/provider/accounts/#{@provider.id}")
+ CloudAccount.find(:first, :conditions => ['id = ?', @cloud_account.id]).should be_nil
+ end
+
+ it "should deny access to users without account modify permission" do
+ get :edit, :id => @cloud_account.id
+ response.should_not be_success
+
+ post :update, :id => @cloud_account.id, :cloud_account => { :password => 'foobar' }
+ response.should_not be_success
+
+ post :destroy, :id => @cloud_account.id
+ response.should_not be_success
+ end
+
+end
diff --git a/src/spec/factories/cloud_account.rb b/src/spec/factories/cloud_account.rb
index b1e4cc4..f494b52 100644
--- a/src/spec/factories/cloud_account.rb
+++ b/src/spec/factories/cloud_account.rb
@@ -2,4 +2,10 @@ Factory.define :cloud_account do |f|
f.sequence(:username) { |n| "testUser#(n)" }
f.password "testPassword"
f.association :provider
-end
\ No newline at end of file
+end
+
+Factory.define :mock_cloud_account, :parent => :cloud_account do |f|
+ f.sequence(:username) { |n| "testMockUser#(n)" }
+ f.password "testMockPassword"
+ f.provider { |p| p.association(:mock_provider) }
+end
diff --git a/src/spec/factories/provider.rb b/src/spec/factories/provider.rb
index 1d3e3b7..d53fc67 100644
--- a/src/spec/factories/provider.rb
+++ b/src/spec/factories/provider.rb
@@ -16,4 +16,4 @@ Factory.define :mock_provider2, :parent => :provider do |p|
p.cloud_type 'mock'
p.url 'http://localhost:3001/api'
p.after_create { |p| p.realms << Factory(:realm3, :provider => p) }
-end
\ No newline at end of file
+end
diff --git a/src/spec/models/cloud_account_spec.rb b/src/spec/models/cloud_account_spec.rb
new file mode 100644
index 0000000..0afe5a3
--- /dev/null
+++ b/src/spec/models/cloud_account_spec.rb
@@ -0,0 +1,21 @@
+require 'spec_helper'
+
+describe CloudAccount do
+ fixtures :all
+ before(:each) do
+ @cloud_account = Factory :mock_cloud_account
+ end
+
+ it "should not be destroyable if it has instances" do
+ @cloud_account.instances << Instance.new
+ @cloud_account.destroyable?.should_not be_true
+ @cloud_account.destroy
+ CloudAccount.find((a)cloud_account.id).should_not be_nil
+
+
+ @cloud_account.instances.clear
+ @cloud_account.destroyable?.should be_true
+ @cloud_account.destroy
+ CloudAccount.find(:first, :conditions => ['id = ?', @cloud_account.id]).should be_nil
+ end
+end
--
1.6.2.5
14 years, 1 month
[PATCH] ui and backend to edit / delete cloud accounts
by Mo Morsi
---
src/app/controllers/cloud_accounts_controller.rb | 28 ++++++++++++++++++++++
src/app/controllers/provider_controller.rb | 4 +-
src/app/models/cloud_account.rb | 7 +++++
src/app/views/cloud_accounts/_form.html.erb | 12 +++++++++
src/app/views/cloud_accounts/edit.html.erb | 10 ++++++++
src/app/views/provider/accounts.html.erb | 4 +++
src/app/views/provider/new_account.html.erb | 13 ++--------
7 files changed, 66 insertions(+), 12 deletions(-)
create mode 100644 src/app/views/cloud_accounts/_form.html.erb
create mode 100644 src/app/views/cloud_accounts/edit.html.erb
diff --git a/src/app/controllers/cloud_accounts_controller.rb b/src/app/controllers/cloud_accounts_controller.rb
index 26c1a63..05e024e 100644
--- a/src/app/controllers/cloud_accounts_controller.rb
+++ b/src/app/controllers/cloud_accounts_controller.rb
@@ -66,5 +66,33 @@ class CloudAccountsController < ApplicationController
redirect_to :controller => "pool", :action => 'show', :id => @pool.id
end
+ def edit
+ @cloud_account = CloudAccount.find(params[:id])
+ @provider = @cloud_account.provider
+ require_privilege(Privilege::ACCOUNT_MODIFY,@provider)
+ end
+ def update
+ @cloud_account = CloudAccount.find(params[:cloud_account][:id])
+ require_privilege(Privilege::ACCOUNT_MODIFY,@cloud_account.provider)
+ if @cloud_account.update_attributes(params[:cloud_account])
+ flash[:notice] = "Cloud Account updated!"
+ redirect_to :controller => 'provider', :action => 'accounts', :id => @cloud_account.provider.id
+ else
+ render :action => :edit
+ end
+ end
+
+ def destroy
+ acct = CloudAccount.find(params[:id])
+ provider = acct.provider
+ require_privilege(Privilege::ACCOUNT_MODIFY,provider)
+ if acct.destroyable?
+ CloudAccount.destroy(params[:id])
+ flash[:notice] = "Cloud Account destroyed"
+ else
+ flash[:notice] = "Cloud Account could not be destroyed"
+ end
+ redirect_to :controller => 'provider', :action => 'accounts', :id => provider.id
+ end
end
diff --git a/src/app/controllers/provider_controller.rb b/src/app/controllers/provider_controller.rb
index 0252a26..23b5ba0 100644
--- a/src/app/controllers/provider_controller.rb
+++ b/src/app/controllers/provider_controller.rb
@@ -74,8 +74,8 @@ class ProviderController < ApplicationController
def create_account
require_privilege(Privilege::ACCOUNT_MODIFY)
- @acct = CloudAccount.find_or_create(params[:account])
- @provider = Provider.find(params[:account][:provider_id])
+ @acct = CloudAccount.find_or_create(params[:cloud_account])
+ @provider = Provider.find(params[:cloud_account][:provider_id])
@provider.cloud_accounts << @acct
redirect_to :action => 'accounts', :id => @provider.id
end
diff --git a/src/app/models/cloud_account.rb b/src/app/models/cloud_account.rb
index 62cecc3..1d6b3e9 100644
--- a/src/app/models/cloud_account.rb
+++ b/src/app/models/cloud_account.rb
@@ -38,6 +38,13 @@ class CloudAccount < ActiveRecord::Base
:include => [:role],
:order => "permissions.id ASC"
+
+ before_destroy {|entry| entry.destroyable? }
+
+ def destroyable?
+ self.instances.empty?
+ end
+
def connect
begin
return DeltaCloud.new(username, password, provider.url)
diff --git a/src/app/views/cloud_accounts/_form.html.erb b/src/app/views/cloud_accounts/_form.html.erb
new file mode 100644
index 0000000..9b7b176
--- /dev/null
+++ b/src/app/views/cloud_accounts/_form.html.erb
@@ -0,0 +1,12 @@
+<fieldset>
+<legend>Account</legend>
+<%= hidden_field :cloud_account, :id %>
+<%= hidden_field :cloud_account, :provider_id, :value => @provider.id %>
+<ul>
+<% if @cloud_account.nil? %>
+<li><label>UserName<span>UserName for the account you wish to connect to this pool.</span></label><%= text_field :cloud_account, :username %></li>
+<% end %>
+<li><label>Password<span>Password for the account you wish to connect to this pool.</span></label><%=password_field :cloud_account, :password %></li>
+</ul>
+</fieldset>
+<%= submit_tag "Save", :class => "submit" %>
diff --git a/src/app/views/cloud_accounts/edit.html.erb b/src/app/views/cloud_accounts/edit.html.erb
new file mode 100644
index 0000000..3ae86aa
--- /dev/null
+++ b/src/app/views/cloud_accounts/edit.html.erb
@@ -0,0 +1,10 @@
+<div class="dcloud_form">
+ <%= error_messages_for 'cloud_account' %>
+
+ <h2>Edit Cloud Account</h2><br />
+
+ <% form_for @cloud_account, :url => { :action => 'update' } do |form| -%>
+ <%= render :partial => 'form' %>
+ <% end %>
+
+</div>
diff --git a/src/app/views/provider/accounts.html.erb b/src/app/views/provider/accounts.html.erb
index 595443d..a763a33 100644
--- a/src/app/views/provider/accounts.html.erb
+++ b/src/app/views/provider/accounts.html.erb
@@ -5,12 +5,16 @@
<thead>
<tr>
<th scope="col">Username</th>
+ <th scope="col"></th>
+ <th scope="col"></th>
</tr>
</thead>
<tbody>
<%(a)provider.cloud_accounts.each {|acct| %>
<tr>
<td><%= acct.username %></td>
+ <td><%= link_to "Edit", {:controller => 'cloud_accounts', :action => 'edit', :id => acct.id} if has_account_modify?(@provider) %></td>
+ <td><%= link_to "Delete", {:controller => 'cloud_accounts', :action => 'destroy', :id => acct.id} if has_account_modify?(@provider) && acct.destroyable? %></td>
</tr>
<% } %>
</tbody>
diff --git a/src/app/views/provider/new_account.html.erb b/src/app/views/provider/new_account.html.erb
index b9ae229..eed474f 100644
--- a/src/app/views/provider/new_account.html.erb
+++ b/src/app/views/provider/new_account.html.erb
@@ -4,15 +4,8 @@
<h2>Create an Account for this Provider</h2><br />
- <% form_tag :action => 'create_account' do -%>
- <fieldset>
- <legend>Account</legend>
- <ul>
- <li><label>UserName<span>UserName for the account you wish to connect to this pool.</span></label><%= text_field :account, :username %></li>
- <li><label>Password<span>Password for the account you wish to connect to this pool.</span></label><%=password_field :account, :password %></li>
- </ul>
- <%=hidden_field :account, :provider_id, :value => @provider.id %>
- </fieldset>
- <%= submit_tag "Save", :class => "submit" %>
+ <% form_tag :action => 'create_account' do |form| -%>
+ <%= render :partial => 'cloud_accounts/form' %>
<% end %>
+
</div>
--
1.6.2.5
14 years, 1 month
[PATCH] ui and backend to edit / delete cloud accounts
by Mo Morsi
---
src/app/controllers/cloud_accounts_controller.rb | 20 ++++++++++++++++++++
src/app/controllers/provider_controller.rb | 4 ++--
src/app/views/cloud_accounts/_form.html.erb | 10 ++++++++++
src/app/views/cloud_accounts/edit.html.erb | 10 ++++++++++
src/app/views/provider/accounts.html.erb | 4 ++++
src/app/views/provider/new_account.html.erb | 13 +++----------
6 files changed, 49 insertions(+), 12 deletions(-)
create mode 100644 src/app/views/cloud_accounts/_form.html.erb
create mode 100644 src/app/views/cloud_accounts/edit.html.erb
diff --git a/src/app/controllers/cloud_accounts_controller.rb b/src/app/controllers/cloud_accounts_controller.rb
index 26c1a63..fb09468 100644
--- a/src/app/controllers/cloud_accounts_controller.rb
+++ b/src/app/controllers/cloud_accounts_controller.rb
@@ -66,5 +66,25 @@ class CloudAccountsController < ApplicationController
redirect_to :controller => "pool", :action => 'show', :id => @pool.id
end
+ def edit
+ @cloud_account = CloudAccount.find(params[:id])
+ @provider = @cloud_account.provider
+ end
+ def update
+ @cloud_account = CloudAccount.find(params[:cloud_account][:id])
+ if @cloud_account.update_attributes(params[:cloud_account])
+ flash[:notice] = "Cloud Account updated!"
+ redirect_to :controller => 'provider', :action => 'accounts', :id => @cloud_account.provider.id
+ else
+ render :action => :edit
+ end
+ end
+
+ def destroy
+ provider = CloudAccount.find(params[:id]).provider
+ CloudAccount.destroy(params[:id])
+ flash[:notice] = "Cloud Account destroyed"
+ redirect_to :controller => 'provider', :action => 'accounts', :id => provider.id
+ end
end
diff --git a/src/app/controllers/provider_controller.rb b/src/app/controllers/provider_controller.rb
index 0252a26..23b5ba0 100644
--- a/src/app/controllers/provider_controller.rb
+++ b/src/app/controllers/provider_controller.rb
@@ -74,8 +74,8 @@ class ProviderController < ApplicationController
def create_account
require_privilege(Privilege::ACCOUNT_MODIFY)
- @acct = CloudAccount.find_or_create(params[:account])
- @provider = Provider.find(params[:account][:provider_id])
+ @acct = CloudAccount.find_or_create(params[:cloud_account])
+ @provider = Provider.find(params[:cloud_account][:provider_id])
@provider.cloud_accounts << @acct
redirect_to :action => 'accounts', :id => @provider.id
end
diff --git a/src/app/views/cloud_accounts/_form.html.erb b/src/app/views/cloud_accounts/_form.html.erb
new file mode 100644
index 0000000..504b5b7
--- /dev/null
+++ b/src/app/views/cloud_accounts/_form.html.erb
@@ -0,0 +1,10 @@
+<fieldset>
+<legend>Account</legend>
+<%= hidden_field :cloud_account, :id %>
+<%= hidden_field :cloud_account, :provider_id, :value => @provider.id %>
+<ul>
+<li><label>UserName<span>UserName for the account you wish to connect to this pool.</span></label><%= text_field :cloud_account, :username %></li>
+<li><label>Password<span>Password for the account you wish to connect to this pool.</span></label><%=password_field :cloud_account, :password %></li>
+</ul>
+</fieldset>
+<%= submit_tag "Save", :class => "submit" %>
diff --git a/src/app/views/cloud_accounts/edit.html.erb b/src/app/views/cloud_accounts/edit.html.erb
new file mode 100644
index 0000000..3ae86aa
--- /dev/null
+++ b/src/app/views/cloud_accounts/edit.html.erb
@@ -0,0 +1,10 @@
+<div class="dcloud_form">
+ <%= error_messages_for 'cloud_account' %>
+
+ <h2>Edit Cloud Account</h2><br />
+
+ <% form_for @cloud_account, :url => { :action => 'update' } do |form| -%>
+ <%= render :partial => 'form' %>
+ <% end %>
+
+</div>
diff --git a/src/app/views/provider/accounts.html.erb b/src/app/views/provider/accounts.html.erb
index 595443d..ebde82f 100644
--- a/src/app/views/provider/accounts.html.erb
+++ b/src/app/views/provider/accounts.html.erb
@@ -5,12 +5,16 @@
<thead>
<tr>
<th scope="col">Username</th>
+ <th scope="col"></th>
+ <th scope="col"></th>
</tr>
</thead>
<tbody>
<%(a)provider.cloud_accounts.each {|acct| %>
<tr>
<td><%= acct.username %></td>
+ <td><%= link_to "Edit", {:controller => 'cloud_accounts', :action => 'edit', :id => acct.id} %></td>
+ <td><%= link_to "Delete", {:controller => 'cloud_accounts', :action => 'destroy', :id => acct.id} %></td>
</tr>
<% } %>
</tbody>
diff --git a/src/app/views/provider/new_account.html.erb b/src/app/views/provider/new_account.html.erb
index b9ae229..eed474f 100644
--- a/src/app/views/provider/new_account.html.erb
+++ b/src/app/views/provider/new_account.html.erb
@@ -4,15 +4,8 @@
<h2>Create an Account for this Provider</h2><br />
- <% form_tag :action => 'create_account' do -%>
- <fieldset>
- <legend>Account</legend>
- <ul>
- <li><label>UserName<span>UserName for the account you wish to connect to this pool.</span></label><%= text_field :account, :username %></li>
- <li><label>Password<span>Password for the account you wish to connect to this pool.</span></label><%=password_field :account, :password %></li>
- </ul>
- <%=hidden_field :account, :provider_id, :value => @provider.id %>
- </fieldset>
- <%= submit_tag "Save", :class => "submit" %>
+ <% form_tag :action => 'create_account' do |form| -%>
+ <%= render :partial => 'cloud_accounts/form' %>
<% end %>
+
</div>
--
1.6.2.5
14 years, 1 month
Can't build deltacloud ruby client
by Sayli Karmarkar
|I was trying to build deltacloud ruby client with the latest code and gem build deltacloud-client.gemspec inside core/client seems to be giving me -
|ERROR: While executing gem ... (ArgumentError)
(eval):27:in `[]': wrong number of arguments (5 for 1)
Am I missing some step in the build procedure or is it a bug in spec file?
--
Sayli Karmarkar
skarmark(a)redhat.com
Systems Management
http://www.redhat.com
14 years, 1 month
[PATCH] Allow the EC2 driver to use public AMI's.
by Chris Lalancette
When using the amazon-ec2 gem method "describe_images", if
both :owner_id and :image_id are specified, it means that
it will only fetch information about images matching
:image_id that are owned by owner :owner_id. Unfortunately,
this means public AMI's are excluded. In the case where
we know the image_id, don't set the owner_id; the number of
responses can only be 0 (meaning that image_id wasn't found)
or 1 (meaning that image_id was found), so it shouldn't
pose a performance problem. With this in place I was able
to start an EC2 instance from a public AMI.
Signed-off-by: Chris Lalancette <clalance(a)redhat.com>
---
server/lib/deltacloud/drivers/ec2/ec2_driver.rb | 11 ++++++++---
1 files changed, 8 insertions(+), 3 deletions(-)
diff --git a/server/lib/deltacloud/drivers/ec2/ec2_driver.rb b/server/lib/deltacloud/drivers/ec2/ec2_driver.rb
index 3134469..c8b26cc 100644
--- a/server/lib/deltacloud/drivers/ec2/ec2_driver.rb
+++ b/server/lib/deltacloud/drivers/ec2/ec2_driver.rb
@@ -99,9 +99,14 @@ class EC2Driver < Deltacloud::BaseDriver
def images(credentials, opts={} )
ec2 = new_client(credentials)
img_arr = []
- config = { :owner_id => "amazon" }
- config.merge!({ :owner_id => opts[:owner_id] }) if opts and opts[:owner_id]
- config.merge!({ :image_id => opts[:id] }) if opts and opts[:id]
+ # if we know the image_id, we don't want to limit by owner_id, since this
+ # will exclude public images
+ if (opts and opts[:id])
+ config = { :image_id => opts[:id] }
+ else
+ config = { :owner_id => "amazon" }
+ config.merge!({ :owner_id => opts[:owner_id] }) if opts and opts[:owner_id]
+ end
safely do
ec2.describe_images(config).imagesSet.item.each do |image|
img_arr << convert_image(image)
--
1.7.0.1
14 years, 1 month
Python Library
by Michael Brown
I've written a native Python client library for Deltacloud. It has a very
similar interface to the Ruby client.
I want to make this library available for others to use. How should I go
about doing this?
Michael Brown
ITng Services
14 years, 1 month
deltacloud-core-0.0.1-1.spec and RPM
by Michal Fojtik
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi all,
I have created a specfile [1] and RPM [2] for deltacloud-core based on
gem version which was recently published on gemcutter [3].
It should build without problems on Fedora 13/12.
Please let me known if you experience any problems with specfile or if
you have any issues with RPM.
- - Michal
[1] http://mifo.sk/RPMS/rubygem-deltacloud-core.spec
[2] http://mifo.sk/RPMS/rubygem-deltacloud-core-0.0.1-1.fc13.src.rpm
[3] http://rubygems.org/gems/deltacloud-core
- --
- --------------------------------------------------------
Michal Fojtik, mfojtik(a)redhat.com, +420 532 294 4307
Ruby / Ruby On Rails Developer
Deltacloud API: http://deltacloud.org
- --------------------------------------------------------
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.14 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iQEcBAEBAgAGBQJL1Y+XAAoJEEwRP1Pf2/iqjWsH/3UY1rtgYTiEEClQoYKrevvB
hPC3VT5w2DpEIQ7ZMVwOmslUBT2dw7v+3ISiNpJbWWZbfRbL1/goxEgUvD+5T259
j6/ei32tXrsymufKGMCUazvVCxOBvawbBb6UW0RMKD0i4bqhsudQ+QgiG0H7Dchb
nKsjdtLedvP1Lz2T8vVskITzR6JTHii0btANYBSopGNZhizWFBFObO43wjG+nQKf
+bu2JHPwsAqHNQt2VcHbAhikMtz7QHUV248y1NCCQHUKLGbTRGqu+orl/2hh3EPx
9lFxpaVw8+pVnN6UJbOodltI5T+ltWEfzELDROVmpH6/9Rug//HKYw1tIZ8xZ1U=
=/YoB
-----END PGP SIGNATURE-----
14 years, 1 month
[PATCH 1/6] Updated model/observer calulate submit to provider time
by Martyn Taylor
From: martyntaylor <mtaylor(a)redhat.com>
---
src/app/models/task.rb | 8 +++++++-
src/app/models/task_observer.rb | 22 ++++++++++++++++++++++
src/db/migrate/20090831140000_create_tasks.rb | 1 +
src/spec/models/task_observer_spec.rb | 24 ++++++++++++++++++++++++
4 files changed, 54 insertions(+), 1 deletions(-)
create mode 100644 src/app/models/task_observer.rb
create mode 100644 src/spec/models/task_observer_spec.rb
diff --git a/src/app/models/task.rb b/src/app/models/task.rb
index 6cb907e..9f23ef2 100644
--- a/src/app/models/task.rb
+++ b/src/app/models/task.rb
@@ -28,6 +28,7 @@ class Task < ActiveRecord::Base
# moved associations here so that nested set :include directives work
STATE_QUEUED = "queued"
+ STATE_PENDING = "pending"
STATE_RUNNING = "running"
STATE_FINISHED = "finished"
STATE_PAUSED = "paused"
@@ -35,7 +36,7 @@ class Task < ActiveRecord::Base
STATE_CANCELED = "canceled"
COMPLETED_STATES = [STATE_FINISHED, STATE_FAILED, STATE_CANCELED]
- WORKING_STATES = [STATE_QUEUED, STATE_RUNNING, STATE_PAUSED]
+ WORKING_STATES = [STATE_QUEUED, STATE_RUNNING, STATE_PAUSED, STATE_PENDING]
validates_inclusion_of :type,
:in => %w( InstanceTask )
@@ -48,6 +49,7 @@ class Task < ActiveRecord::Base
# depending on subclass, action, state
TASK_STATES_OPTIONS = [["Queued", Task::STATE_QUEUED],
+ ["Pending", Task::STATE_PENDING],
["Running", Task::STATE_RUNNING],
["Paused", Task::STATE_PAUSED],
["Finished", Task::STATE_FINISHED],
@@ -92,6 +94,10 @@ class Task < ActiveRecord::Base
ret_val
end
+ def submission_time
+ time.started - time.submitted
+ end
+
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?
diff --git a/src/app/models/task_observer.rb b/src/app/models/task_observer.rb
new file mode 100644
index 0000000..3e2d803
--- /dev/null
+++ b/src/app/models/task_observer.rb
@@ -0,0 +1,22 @@
+class TaskObserver < ActiveRecord::Observer
+ def after_save(a_task)
+ if a_task.changed?
+ change = a_task.changes['state']
+ if change
+ update_timestamp(change[0], change[1], a_task)
+ end
+ end
+ end
+
+ def update_timestamp(state_from, state_to, a_task)
+ puts state_to
+ if state_to == Task::STATE_RUNNING
+ a_task.time_started = Time.now
+ elsif state_to == Task::STATE_PENDING
+ a_task.time_submitted = Time.now
+ end
+ end
+
+end
+
+TaskObserver.instance
\ No newline at end of file
diff --git a/src/db/migrate/20090831140000_create_tasks.rb b/src/db/migrate/20090831140000_create_tasks.rb
index a9e099f..2c3e0e2 100644
--- a/src/db/migrate/20090831140000_create_tasks.rb
+++ b/src/db/migrate/20090831140000_create_tasks.rb
@@ -28,6 +28,7 @@ class CreateTasks < ActiveRecord::Migration
t.string :task_target_type
t.string :args
t.timestamp :created_at
+ t.timestamp :time_submitted
t.timestamp :time_started
t.timestamp :time_ended
t.text :message
diff --git a/src/spec/models/task_observer_spec.rb b/src/spec/models/task_observer_spec.rb
new file mode 100644
index 0000000..5d31693
--- /dev/null
+++ b/src/spec/models/task_observer_spec.rb
@@ -0,0 +1,24 @@
+require 'spec_helper'
+
+describe TaskObserver do
+
+ before(:each) do
+ @timestamp = Time.now
+ @task = InstanceTask.new({})
+ end
+
+ it "should set started at timestamp when the task goes to state running" do
+ @task.state = Task::STATE_RUNNING
+ @task.save
+
+ @task.time_started.should >= @timestamp
+ end
+
+ it "should set time submitted timestamp when the task goes to state pending" do
+ @task.state = Task::STATE_PENDING
+ @task.save
+
+ @task.time_submitted.should >= @timestamp
+ end
+
+end
\ No newline at end of file
--
1.6.6.1
14 years, 1 month