Steve Linabery
2008-Dec-12 18:46 UTC
[Ovirt-devel] [PATCH server] Add audit trail to model for members joining/leaving containers
E.g. hosts joining/leaving hardware_pools --- src/app/models/hardware_pool.rb | 13 ++++++++++- src/app/models/host.rb | 1 + src/app/models/membership_audit_event.rb | 26 ++++++++++++++++++++++ src/app/models/pool.rb | 1 + src/db/migrate/032_add_pool_audit_trail.rb | 32 ++++++++++++++++++++++++++++ 5 files changed, 72 insertions(+), 1 deletions(-) create mode 100644 src/app/models/membership_audit_event.rb create mode 100644 src/db/migrate/032_add_pool_audit_trail.rb diff --git a/src/app/models/hardware_pool.rb b/src/app/models/hardware_pool.rb index d39d8e7..8307ce4 100644 --- a/src/app/models/hardware_pool.rb +++ b/src/app/models/hardware_pool.rb @@ -54,8 +54,19 @@ class HardwarePool < Pool hosts = Host.find(:all, :conditions => "id in (#{host_ids.join(', ')})") transaction do hosts.each do |host| - host.hardware_pool_id = target_pool_id + + leave = MembershipAuditEvent.new({ :member_target => host, + :container_target => host.hardware_pool, + :action => MembershipAuditEvent::LEAVE }) + leave.save! + + host.hardware_pool = HardwarePool.find(target_pool_id) host.save! + + join = MembershipAuditEvent.new({ :member_target => host, + :container_target => host.hardware_pool, + :action => MembershipAuditEvent::JOIN }) + join.save! end end end diff --git a/src/app/models/host.rb b/src/app/models/host.rb index 640782d..5e24dcf 100644 --- a/src/app/models/host.rb +++ b/src/app/models/host.rb @@ -23,6 +23,7 @@ class Host < ActiveRecord::Base belongs_to :hardware_pool belongs_to :bonding_type + has_many :membership_audit_events, :as => :member_target, :dependent => :destroy, :order => "created_at ASC" has_many :cpus, :dependent => :destroy has_many :nics, :dependent => :destroy has_many :bondings, :dependent => :destroy diff --git a/src/app/models/membership_audit_event.rb b/src/app/models/membership_audit_event.rb new file mode 100644 index 0000000..b36cbee --- /dev/null +++ b/src/app/models/membership_audit_event.rb @@ -0,0 +1,26 @@ +# +# Copyright (C) 2008 Red Hat, Inc. +# Written by Steve Linabery <slinabery at redhat.com> +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; version 2 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, +# MA 02110-1301, USA. A copy of the GNU General Public License is +# also available at http://www.gnu.org/copyleft/gpl.html. + +class MembershipAuditEvent < ActiveRecord::Base + belongs_to :container_target, :polymorphic => true + belongs_to :member_target, :polymorphic => true + + JOIN = "join" + LEAVE = "leave" +end diff --git a/src/app/models/pool.rb b/src/app/models/pool.rb index 7034e79..614325a 100644 --- a/src/app/models/pool.rb +++ b/src/app/models/pool.rb @@ -20,6 +20,7 @@ class Pool < ActiveRecord::Base acts_as_nested_set + has_many :membership_audit_events, :as => :container_target, :dependent => :destroy # moved associations here so that nested set :include directives work # TODO: find a way to put this back into vm_resource_pool.rb has_many :vms, :dependent => :nullify, :order => "id ASC", :foreign_key => :vm_resource_pool_id diff --git a/src/db/migrate/032_add_pool_audit_trail.rb b/src/db/migrate/032_add_pool_audit_trail.rb new file mode 100644 index 0000000..656ad87 --- /dev/null +++ b/src/db/migrate/032_add_pool_audit_trail.rb @@ -0,0 +1,32 @@ +# +# Copyright (C) 2008 Red Hat, Inc. +# Written by Steve Linabery <slinabery at redhat.com> +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; version 2 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, +# MA 02110-1301, USA. A copy of the GNU General Public License is +# also available at http://www.gnu.org/copyleft/gpl.html. + +class AddPoolAuditTrail < ActiveRecord::Migration + def self.up + create_table :membership_audit_events do |t| + t.timestamp :created_at + t.string :action + t.integer :container_target_id + t.string :container_target_type + t.integer :member_target_id + t.string :member_target_type + t.integer :lock_version, :default => 0 + end + end +end -- 1.6.0.4
Scott Seago
2008-Dec-12 21:27 UTC
[Ovirt-devel] [PATCH server] Add audit trail to model for members joining/leaving containers
Steve Linabery wrote:> E.g. hosts joining/leaving hardware_pools > --- > src/app/models/hardware_pool.rb | 13 ++++++++++- > src/app/models/host.rb | 1 + > src/app/models/membership_audit_event.rb | 26 ++++++++++++++++++++++ > src/app/models/pool.rb | 1 + > src/db/migrate/032_add_pool_audit_trail.rb | 32 ++++++++++++++++++++++++++++ > 5 files changed, 72 insertions(+), 1 deletions(-) > create mode 100644 src/app/models/membership_audit_event.rb > create mode 100644 src/db/migrate/032_add_pool_audit_trail.rb > >Works for me. ACK. What's the plan for making this data accessible via the UI and/or another API? Scott