From 036a0370744002d6727e68fdf075a3a3adca2559 Mon Sep 17 00:00:00 2001 From: Tejas Ravindra Mandke Date: Sun, 31 Mar 2013 17:00:44 -0700 Subject: [PATCH] [vsphere|compute] Switch some attributes to lazyload --- lib/fog/vsphere/compute.rb | 17 ++++++----------- lib/fog/vsphere/models/compute/server.rb | 10 ++++++++++ 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/lib/fog/vsphere/compute.rb b/lib/fog/vsphere/compute.rb index 520639b77..47e1599ee 100644 --- a/lib/fog/vsphere/compute.rb +++ b/lib/fog/vsphere/compute.rb @@ -121,20 +121,15 @@ module Fog # The name method "magically" appears after a VM is ready and # finished cloning. if attrs['hypervisor'].kind_of?(RbVmomi::VIM::HostSystem) - begin - host = attrs['hypervisor'] - attrs['datacenter'] = parent_attribute(host.path, :datacenter)[1] - attrs['cluster'] = parent_attribute(host.path, :cluster)[1] - attrs['hypervisor'] = host.name - attrs['resource_pool'] = (vm_mob_ref.resourcePool || host.resourcePool).name rescue nil - rescue - # If it's not ready, set the hypervisor to nil - attrs['hypervisor'] = nil - end + host = attrs['hypervisor'] + attrs['datacenter'] = Proc.new { parent_attribute(host.path, :datacenter)[1] rescue nil } + attrs['cluster'] = Proc.new { parent_attribute(host.path, :cluster)[1] rescue nil } + attrs['hypervisor'] = Proc.new { host.name rescue nil } + attrs['resource_pool'] = Proc.new {(vm_mob_ref.resourcePool || host.resourcePool).name rescue nil} end # 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 + attrs['mac_addresses'] = Proc.new {vm_mob_ref.macs 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 diff --git a/lib/fog/vsphere/models/compute/server.rb b/lib/fog/vsphere/models/compute/server.rb index 44065f56c..086a90260 100644 --- a/lib/fog/vsphere/models/compute/server.rb +++ b/lib/fog/vsphere/models/compute/server.rb @@ -52,6 +52,16 @@ module Fog initialize_volumes end + # Lazy Loaded Attributes + [:datacenter, :cluster, :hypervisor, :resource_pool, :mac_addresses].each do |attr| + define_method attr do + attributes[attr] = attributes[attr].call if attributes[attr].is_a?(Proc) + attributes[attr] + end + end + # End Lazy Loaded Attributes + + def vm_reconfig_memory(options = {}) requires :instance_uuid, :memory service.vm_reconfig_memory('instance_uuid' => instance_uuid, 'memory' => memory)