From 27a689b95156770de1ae1e84566fa3c96963ba1f Mon Sep 17 00:00:00 2001 From: Jeff McCune Date: Tue, 6 Sep 2011 16:49:28 -0700 Subject: [PATCH] (#9241) Don't fail when trying to model a cloning VM With the introduction of the vm_clone API request, a server may be returned in the list which does not have a complete configuration yet. This is because the system is still in the process of being cloned. This is a problem because Fog would throw an undefined method error when calling the config method of the vm managed object instance. This patch fixes the problem by checking if the config method returns something and only sets attributes that are known to be available for a cloning VM. --- lib/fog/vsphere/models/compute/server.rb | 42 ++++++++++++++---------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/lib/fog/vsphere/models/compute/server.rb b/lib/fog/vsphere/models/compute/server.rb index 4f42d4ded..dfbd2c183 100644 --- a/lib/fog/vsphere/models/compute/server.rb +++ b/lib/fog/vsphere/models/compute/server.rb @@ -32,25 +32,33 @@ module Fog attribute :hypervisor, :aliases => 'host' attribute :is_a_template - # Create an instance of the server model from a + # Return an attribute hash suitable for the initializer given a # vSphere Managed Object (mob) instance. def self.attribute_hash_from_mob(vm) - { - :id => vm.config.instanceUuid || vm._ref, - :name => vm.name, - :uuid => vm.config.uuid, - :instance_uuid => vm.config.instanceUuid, - :hostname => vm.summary.guest.hostName, - :operatingsystem => vm.summary.guest.guestFullName, - :ipaddress => vm.summary.guest.ipAddress, - :power_state => vm.runtime.powerState, - :connection_state => vm.runtime.connectionState, - :hypervisor => vm.runtime.host.name, - :tools_state => vm.summary.guest.toolsStatus, - :tools_version => vm.summary.guest.toolsVersionStatus, - :mac_addresses => vm.macs, - :is_a_template => vm.config.template - } + return {} unless vm + # A cloning VM doesn't have a configuration yet. Unfortuantely we just get + # a RunTime exception. + begin + is_ready = vm.config ? true : false + rescue RuntimeError + is_ready = nil + end + { + :id => is_ready ? vm.config.instanceUuid : vm._ref, + :name => vm.name, + :uuid => is_ready ? vm.config.uuid : 'unavailable', + :instance_uuid => is_ready ? vm.config.instanceUuid : 'unavailable', + :hostname => vm.summary.guest.hostName, + :operatingsystem => vm.summary.guest.guestFullName, + :ipaddress => vm.summary.guest.ipAddress, + :power_state => vm.runtime.powerState, + :connection_state => vm.runtime.connectionState, + :hypervisor => vm.runtime.host ? vm.runtime.host.name : 'unknown', + :tools_state => vm.summary.guest.toolsStatus, + :tools_version => vm.summary.guest.toolsVersionStatus, + :mac_addresses => is_ready ? vm.macs : nil, + :is_a_template => is_ready ? vm.config.template : nil + } end def start