From 88de0d008f6c2138348bdfeaa566d620cc266ed9 Mon Sep 17 00:00:00 2001 From: Tejas Ravindra Mandke Date: Sun, 31 Mar 2013 09:43:06 -0700 Subject: [PATCH] [vsphere|compute] Bulk fetch all managed views VM properties --- lib/fog/vsphere/compute.rb | 16 +++++++++++++--- .../requests/compute/list_virtual_machines.rb | 10 +++++----- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/lib/fog/vsphere/compute.rb b/lib/fog/vsphere/compute.rb index 7e1ce1ca1..520639b77 100644 --- a/lib/fog/vsphere/compute.rb +++ b/lib/fog/vsphere/compute.rb @@ -74,6 +74,8 @@ module Fog :id => 'config.instanceUuid', :name => 'name', :uuid => 'config.uuid', + :template => 'config.template', + :parent => 'parent', :hostname => 'summary.guest.hostName', :operatingsystem => 'summary.guest.guestFullName', :ipaddress => 'guest.ipAddress', @@ -88,6 +90,11 @@ module Fog :guest_id => 'summary.guest.guestId', } + def convert_vm_view_to_attr_hash(vms) + vms = @connection.serviceContent.propertyCollector.collectMultiple(vms,*ATTR_TO_PROP.values.uniq) + vms.map { |vm| props_to_attr_hash(*vm) } + end + # Utility method to convert a VMware managed object into an attribute hash. # This should only really be necessary for the real class. # This method is expected to be called by the request methods @@ -96,6 +103,10 @@ module Fog return nil unless vm_mob_ref props = vm_mob_ref.collect!(*ATTR_TO_PROP.values.uniq) + props_to_attr_hash vm_mob_ref, props + end + + def props_to_attr_hash vm_mob_ref, props # NOTE: Object.tap is in 1.8.7 and later. # Here we create the hash object that this method returns, but first we need # to add a few more attributes that require additional calls to the vSphere @@ -124,12 +135,11 @@ module Fog # This inline rescue catches any standard error. While a VM is # cloning, a call to the macs method will throw and NoMethodError attrs['mac_addresses'] = vm_mob_ref.macs rescue nil - # Rescue nil to catch testing while vm_mob_ref isn't reaL?? - attrs['path'] = "/"+vm_mob_ref.parent.path.map(&:last).join('/') rescue nil + # Rescue nil to catch testing while vm_mob_ref isn't reaL?? + attrs['path'] = "/"+attrs['parent'].path.map(&:last).join('/') rescue nil attrs['relative_path'] = (attrs['path'].split('/').reject {|e| e.empty?} - ["Datacenters", attrs['datacenter'], "vm"]).join("/") rescue nil end end - # returns the parent object based on a type # provides both real RbVmomi object and its name. # e.g. diff --git a/lib/fog/vsphere/requests/compute/list_virtual_machines.rb b/lib/fog/vsphere/requests/compute/list_virtual_machines.rb index c7377284c..a3ec7e3b1 100644 --- a/lib/fog/vsphere/requests/compute/list_virtual_machines.rb +++ b/lib/fog/vsphere/requests/compute/list_virtual_machines.rb @@ -35,12 +35,12 @@ module Fog :recursive => true }).view end.flatten - # remove all template based virtual machines - vms.delete_if { |v| v.config.template } - vms.map do |vm_mob| - convert_vm_mob_ref_to_attr_hash(vm_mob) - end + vms = convert_vm_view_to_attr_hash(vms) + + # remove all template based virtual machines + vms.delete_if { |v| v['template'] } + vms end def get_folder_path(folder, root = nil)