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

(#10570) Use nil in-place of missing attributes

Without this patch, `Fog::Compute::Vsphere#convert_vm_mob_ref_to_attr_hash`
method produces unhandled exceptions during VMware cloning and listing
operations. The root cause of these exceptions are based on the fact
that some VMware virtual machine attributes: hypervisor name, and
macaddress, are not available until the cloning process has finished.

These exceptions can be triggered when external events take place within
the VMware infrastructure such as end-users cloning machines via some
other VMware management tool.

This patch solves the problem by catching any exceptions that occur
during attribute lookups for both the hypervisor name and the virtual
machine macaddress, and setting them to nil.

This patch also removes the host attribute from the hash generated by
the `Fog::Compute::Vsphere#convert_vm_mob_ref_to_attr_hash` method. The
hypervisor attribute is added instead, which is an alias to host.

This patch changes the behaviour of the
`Fog::Compute::Vsphere#convert_vm_mob_ref_to_attr_hash` method by
catching exceptions for missing attributes, and setting them to nil.
This commit is contained in:
Kelsey Hightower 2011-11-07 07:08:54 -05:00
parent 762aa624ac
commit bc74e06d12

View file

@ -39,7 +39,7 @@ module Fog
:ipaddress => 'guest.ipAddress', :ipaddress => 'guest.ipAddress',
:power_state => 'runtime.powerState', :power_state => 'runtime.powerState',
:connection_state => 'runtime.connectionState', :connection_state => 'runtime.connectionState',
:host => 'runtime.host', :hypervisor => 'runtime.host',
:tools_state => 'guest.toolsStatus', :tools_state => 'guest.toolsStatus',
:tools_version => 'guest.toolsVersionStatus', :tools_version => 'guest.toolsVersionStatus',
:is_a_template => 'config.template', :is_a_template => 'config.template',
@ -53,11 +53,26 @@ module Fog
return nil unless vm_mob_ref return nil unless vm_mob_ref
props = vm_mob_ref.collect! *ATTR_TO_PROP.values.uniq props = vm_mob_ref.collect! *ATTR_TO_PROP.values.uniq
# 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
# API. The hypervisor name and mac_addresses attributes may not be available
# so we need catch any exceptions thrown during lookup and set them to nil.
#
# The use of the "tap" method here is a convience, it allows us to update the
# hash object without expliclty returning the hash at the end of the method.
Hash[ATTR_TO_PROP.map { |k,v| [k.to_s, props[v]] }].tap do |attrs| Hash[ATTR_TO_PROP.map { |k,v| [k.to_s, props[v]] }].tap do |attrs|
attrs['id'] ||= vm_mob_ref._ref attrs['id'] ||= vm_mob_ref._ref
attrs['mo_ref'] = vm_mob_ref._ref attrs['mo_ref'] = vm_mob_ref._ref
attrs['hypervisor'] = attrs['host'].name # The name method "magically" appears after a VM is ready and
attrs['mac_addresses'] = vm_mob_ref.macs # finished cloning.
if attrs['hypervisor'].kind_of?(RbVmomi::VIM::HostSystem) then
# If it's not ready, set the hypervisor to nil
attrs['hypervisor'] = attrs['hypervisor'].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['path'] = get_folder_path(vm_mob_ref.parent) attrs['path'] = get_folder_path(vm_mob_ref.parent)
end end
end end