1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

[vsphere|compute] Switch some attributes to lazyload

This commit is contained in:
Tejas Ravindra Mandke 2013-03-31 17:00:44 -07:00 committed by Jeff McCune
parent 88de0d008f
commit 036a037074
2 changed files with 16 additions and 11 deletions

View file

@ -121,20 +121,15 @@ module Fog
# The name method "magically" appears after a VM is ready and # The name method "magically" appears after a VM is ready and
# finished cloning. # finished cloning.
if attrs['hypervisor'].kind_of?(RbVmomi::VIM::HostSystem) if attrs['hypervisor'].kind_of?(RbVmomi::VIM::HostSystem)
begin host = attrs['hypervisor']
host = attrs['hypervisor'] attrs['datacenter'] = Proc.new { parent_attribute(host.path, :datacenter)[1] rescue nil }
attrs['datacenter'] = parent_attribute(host.path, :datacenter)[1] attrs['cluster'] = Proc.new { parent_attribute(host.path, :cluster)[1] rescue nil }
attrs['cluster'] = parent_attribute(host.path, :cluster)[1] attrs['hypervisor'] = Proc.new { host.name rescue nil }
attrs['hypervisor'] = host.name attrs['resource_pool'] = Proc.new {(vm_mob_ref.resourcePool || host.resourcePool).name rescue nil}
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
end end
# This inline rescue catches any standard error. While a VM is # This inline rescue catches any standard error. While a VM is
# cloning, a call to the macs method will throw and NoMethodError # 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?? # Rescue nil to catch testing while vm_mob_ref isn't reaL??
attrs['path'] = "/"+attrs['parent'].path.map(&:last).join('/') rescue nil 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 attrs['relative_path'] = (attrs['path'].split('/').reject {|e| e.empty?} - ["Datacenters", attrs['datacenter'], "vm"]).join("/") rescue nil

View file

@ -52,6 +52,16 @@ module Fog
initialize_volumes initialize_volumes
end 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 = {}) def vm_reconfig_memory(options = {})
requires :instance_uuid, :memory requires :instance_uuid, :memory
service.vm_reconfig_memory('instance_uuid' => instance_uuid, 'memory' => memory) service.vm_reconfig_memory('instance_uuid' => instance_uuid, 'memory' => memory)