Scott Seago
2008-Oct-15 15:07 UTC
[Ovirt-devel] [PATCH] model apis for the storage tree view for the new VM form.
HardwarePool.storage_tree returns a list of storage pools in the HW pool. For
each pool, the :children hash element has a list of storage volumes, filtered to
include only available volumes (and ones attached to the current VM for edit
operations). For each storage volume, if it supports LVM, then it has a
:children element with LVM volumes defined on it.
The hash includes fields showing which are available for use (:available) and
which support creating new child volumes (currently iSCSI volumes, with NFS
Pools soon to be added)
Signed-off-by: Scott Seago <sseago at redhat.com>
---
src/app/models/hardware_pool.rb | 6 +++++
src/app/models/iscsi_storage_volume.rb | 6 +++++
src/app/models/storage_pool.rb | 27 +++++++++++++++++++++++++
src/app/models/storage_volume.rb | 34 ++++++++++++++++++++++++++++++++
4 files changed, 73 insertions(+), 0 deletions(-)
diff --git a/src/app/models/hardware_pool.rb b/src/app/models/hardware_pool.rb
index 160f663..6780329 100644
--- a/src/app/models/hardware_pool.rb
+++ b/src/app/models/hardware_pool.rb
@@ -101,4 +101,10 @@ class HardwarePool < Pool
return {:total => total, :labels => labels}
end
+ def storage_tree(vm_to_include=nil)
+ storage_pools.find(:all,
+ :conditions => "type !=
'LvmStoragePool'").collect do |pool|
+ pool.storage_tree_element(vm_to_include)
+ end
+ end
end
diff --git a/src/app/models/iscsi_storage_volume.rb
b/src/app/models/iscsi_storage_volume.rb
index b254cd8..00d7db2 100644
--- a/src/app/models/iscsi_storage_volume.rb
+++ b/src/app/models/iscsi_storage_volume.rb
@@ -21,4 +21,10 @@ class IscsiStorageVolume < StorageVolume
def label_components
"#{storage_pool[:target]}:#{lun}"
end
+
+ #FIXME: should also take available free space into account
+ def supports_lvm_subdivision
+ return true
+ end
+
end
diff --git a/src/app/models/storage_pool.rb b/src/app/models/storage_pool.rb
index 7a6f8f0..aed2902 100644
--- a/src/app/models/storage_pool.rb
+++ b/src/app/models/storage_pool.rb
@@ -28,6 +28,9 @@ class StoragePool < ActiveRecord::Base
def total_size_in_gb
find(:all).inject(0){ |sum, sv| sum + sv.size_in_gb }
end
+ def full_vm_list
+ find(:all).inject([]){ |list, sv| list + sv.vms }
+ end
end
has_many :smart_pool_tags, :as => :tagged, :dependent => :destroy
@@ -73,4 +76,28 @@ class StoragePool < ActiveRecord::Base
def search_users
hardware_pool.search_users
end
+
+ def user_subdividable
+ false
+ end
+
+ def storage_tree_element(vm_to_include=nil)
+ return_hash = { :id => id,
+ :type => self[:type],
+ :text => display_name,
+ :name => display_name,
+ :available => false,
+ :create_volume => user_subdividable,
+ :selected => false}
+ condition = "vms.id is null"
+ if (vm_to_include and vm_to_include.id)
+ condition +=" or vms.id=#{vm_to_include.id}"
+ end
+ return_hash[:children] = storage_volumes.find(:all,
+ :include => :vms,
+ :conditions => condition).collect do |volume|
+ volume.storage_tree_element(vm_to_include)
+ end
+ return_hash
+ end
end
diff --git a/src/app/models/storage_volume.rb b/src/app/models/storage_volume.rb
index 378b58f..3bd5da0 100644
--- a/src/app/models/storage_volume.rb
+++ b/src/app/models/storage_volume.rb
@@ -70,4 +70,38 @@ class StorageVolume < ActiveRecord::Base
return []
end
end
+
+ def supports_lvm_subdivision
+ return false
+ end
+
+ def storage_tree_element(vm_to_include=nil)
+ vm_ids = vms.collect {|vm| vm.id}
+ return_hash = { :id => id,
+ :type => self[:type],
+ :text => display_name,
+ :name => display_name,
+ :available => ((vm_ids.empty?) or
+ (vm_to_include and vm_to_include.id and
+ vm_ids.include?(vm_to_include.id))),
+ :create_volume => supports_lvm_subdivision,
+ :selected => (!vm_ids.empty? and vm_to_include and vm_to_include.id
and
+ (vm_ids.include?(vm_to_include.id)))}
+ if lvm_storage_pool
+ if return_hash[:available]
+ return_hash[:available] =
lvm_storage_pool.storage_volumes.full_vm_list.empty?
+ end
+ condition = "vms.id is null"
+ if (vm_to_include and vm_to_include.id)
+ condition +=" or vms.id=#{vm_to_include.id}"
+ end
+ return_hash[:children] = lvm_storage_pool.storage_volumes.find(:all,
+ :include => :vms,
+ :conditions => condition).collect do |volume|
+ volume.storage_tree_element(vm_to_include)
+ end
+ end
+ return_hash
+ end
+
end
--
1.5.5.1
Jason Guiditta
2008-Oct-20 19:17 UTC
[Ovirt-devel] [PATCH] model apis for the storage tree view for the new VM form.
On Wed, 2008-10-15 at 15:07 +0000, Scott Seago wrote:> HardwarePool.storage_tree returns a list of storage pools in the HW pool. For each pool, the :children hash element has a list of storage volumes, filtered to include only available volumes (and ones attached to the current VM for edit operations). For each storage volume, if it supports LVM, then it has a :children element with LVM volumes defined on it. > > The hash includes fields showing which are available for use (:available) and which support creating new child volumes (currently iSCSI volumes, with NFS Pools soon to be added) > > Signed-off-by: Scott Seago <sseago at redhat.com> > --- > src/app/models/hardware_pool.rb | 6 +++++ > src/app/models/iscsi_storage_volume.rb | 6 +++++ > src/app/models/storage_pool.rb | 27 +++++++++++++++++++++++++ > src/app/models/storage_volume.rb | 34 ++++++++++++++++++++++++++++++++ > 4 files changed, 73 insertions(+), 0 deletions(-) > > diff --git a/src/app/models/hardware_pool.rb b/src/app/models/hardware_pool.rb > index 160f663..6780329 100644 > --- a/src/app/models/hardware_pool.rb > +++ b/src/app/models/hardware_pool.rb > @@ -101,4 +101,10 @@ class HardwarePool < Pool > return {:total => total, :labels => labels} > end > > + def storage_tree(vm_to_include=nil) > + storage_pools.find(:all, > + :conditions => "type != 'LvmStoragePool'").collect do |pool| > + pool.storage_tree_element(vm_to_include) > + end > + end > end > diff --git a/src/app/models/iscsi_storage_volume.rb b/src/app/models/iscsi_storage_volume.rb > index b254cd8..00d7db2 100644 > --- a/src/app/models/iscsi_storage_volume.rb > +++ b/src/app/models/iscsi_storage_volume.rb > @@ -21,4 +21,10 @@ class IscsiStorageVolume < StorageVolume > def label_components > "#{storage_pool[:target]}:#{lun}" > end > + > + #FIXME: should also take available free space into account > + def supports_lvm_subdivision > + return true > + end > + > end > diff --git a/src/app/models/storage_pool.rb b/src/app/models/storage_pool.rb > index 7a6f8f0..aed2902 100644 > --- a/src/app/models/storage_pool.rb > +++ b/src/app/models/storage_pool.rb > @@ -28,6 +28,9 @@ class StoragePool < ActiveRecord::Base > def total_size_in_gb > find(:all).inject(0){ |sum, sv| sum + sv.size_in_gb } > end > + def full_vm_list > + find(:all).inject([]){ |list, sv| list + sv.vms } > + end > end > > has_many :smart_pool_tags, :as => :tagged, :dependent => :destroy > @@ -73,4 +76,28 @@ class StoragePool < ActiveRecord::Base > def search_users > hardware_pool.search_users > end > + > + def user_subdividable > + false > + end > + > + def storage_tree_element(vm_to_include=nil) > + return_hash = { :id => id, > + :type => self[:type], > + :text => display_name, > + :name => display_name, > + :available => false, > + :create_volume => user_subdividable, > + :selected => false} > + condition = "vms.id is null" > + if (vm_to_include and vm_to_include.id) > + condition +=" or vms.id=#{vm_to_include.id}" > + end > + return_hash[:children] = storage_volumes.find(:all, > + :include => :vms, > + :conditions => condition).collect do |volume| > + volume.storage_tree_element(vm_to_include) > + end > + return_hash > + end > end > diff --git a/src/app/models/storage_volume.rb b/src/app/models/storage_volume.rb > index 378b58f..3bd5da0 100644 > --- a/src/app/models/storage_volume.rb > +++ b/src/app/models/storage_volume.rb > @@ -70,4 +70,38 @@ class StorageVolume < ActiveRecord::Base > return [] > end > end > + > + def supports_lvm_subdivision > + return false > + end > + > + def storage_tree_element(vm_to_include=nil) > + vm_ids = vms.collect {|vm| vm.id} > + return_hash = { :id => id, > + :type => self[:type], > + :text => display_name, > + :name => display_name, > + :available => ((vm_ids.empty?) or > + (vm_to_include and vm_to_include.id and > + vm_ids.include?(vm_to_include.id))), > + :create_volume => supports_lvm_subdivision, > + :selected => (!vm_ids.empty? and vm_to_include and vm_to_include.id and > + (vm_ids.include?(vm_to_include.id)))} > + if lvm_storage_pool > + if return_hash[:available] > + return_hash[:available] = lvm_storage_pool.storage_volumes.full_vm_list.empty? > + end > + condition = "vms.id is null" > + if (vm_to_include and vm_to_include.id) > + condition +=" or vms.id=#{vm_to_include.id}" > + end > + return_hash[:children] = lvm_storage_pool.storage_volumes.find(:all, > + :include => :vms, > + :conditions => condition).collect do |volume| > + volume.storage_tree_element(vm_to_include) > + end > + end > + return_hash > + end > + > endACK. I have tested this out by manually creating some objects and associations in the rails console. calling storage_tree returns expected results. We can probably drop the :text attribute of the hash (unneeded), but that can be altered at any time as we are tweaking the json. -j