Module: Fog::Compute::Vsphere::Shared

Included in:
Mock, Real
Defined in:
lib/fog/vsphere/compute.rb,
lib/fog/vsphere/requests/compute/vm_clone.rb,
lib/fog/vsphere/requests/compute/vm_create.rb,
lib/fog/vsphere/requests/compute/find_vm_by_ref.rb

Constant Summary collapse

ATTR_TO_PROP =
{
  :id => 'config.instanceUuid',
  :name => 'name',
  :uuid => 'config.uuid',
  :instance_uuid => 'config.instanceUuid',
  :hostname => 'summary.guest.hostName',
  :operatingsystem => 'summary.guest.guestFullName',
  :ipaddress => 'guest.ipAddress',
  :power_state => 'runtime.powerState',
  :connection_state => 'runtime.connectionState',
  :hypervisor => 'runtime.host',
  :tools_state => 'guest.toolsStatus',
  :tools_version => 'guest.toolsVersionStatus',
  :is_a_template => 'config.template',
  :memory_mb => 'config.hardware.memoryMB',
  :cpus   => 'config.hardware.numCPU',
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#vsphere_is_vcenterObject (readonly)

Returns the value of attribute vsphere_is_vcenter.



34
35
36
# File 'lib/fog/vsphere/compute.rb', line 34

def vsphere_is_vcenter
  @vsphere_is_vcenter
end

#vsphere_revObject (readonly)

Returns the value of attribute vsphere_rev.



35
36
37
# File 'lib/fog/vsphere/compute.rb', line 35

def vsphere_rev
  @vsphere_rev
end

#vsphere_serverObject (readonly)

Returns the value of attribute vsphere_server.



36
37
38
# File 'lib/fog/vsphere/compute.rb', line 36

def vsphere_server
  @vsphere_server
end

#vsphere_usernameObject (readonly)

Returns the value of attribute vsphere_username.



37
38
39
# File 'lib/fog/vsphere/compute.rb', line 37

def vsphere_username
  @vsphere_username
end

Instance Method Details

#convert_vm_mob_ref_to_attr_hash(vm_mob_ref) ⇒ Object

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 in order to massage VMware Managed Object References into Attribute Hashes.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/fog/vsphere/compute.rb', line 61

def convert_vm_mob_ref_to_attr_hash(vm_mob_ref)
  return nil unless vm_mob_ref

  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 convenience, it allows us to update the
  # hash object without explicitly returning the hash at the end of the method.
  Hash[ATTR_TO_PROP.map { |k,v| [k.to_s, props[v]] }].tap do |attrs|
    attrs['id'] ||= vm_mob_ref._ref
    attrs['mo_ref'] = vm_mob_ref._ref
    # The name method "magically" appears after a VM is ready and
    # 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)
  end
end

#find_vm_by_ref(options = {}) ⇒ Object

REVISIT: This is a naive implementation and not very efficient since we find ALL VM’s and then iterate over them looking for the managed object reference id… There should be an easier way to obtain a reference to a VM using only the name or the _ref. This request is primarily intended to reload the attributes of a cloning VM which does not yet have an instance_uuid

Raises:

  • (ArgumentError)


12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/fog/vsphere/requests/compute/find_vm_by_ref.rb', line 12

def find_vm_by_ref(options = {})
  raise ArgumentError, "Must pass a vm_ref option" unless options['vm_ref']

  # This is the inefficient call
  all_vm_attributes = list_virtual_machines['virtual_machines']
  # Find the VM attributes of the reference
  if vm_attributes = all_vm_attributes.find { |vm| vm['mo_ref'] == options['vm_ref'] }
    response = { 'virtual_machine' => vm_attributes }
  else
    raise Fog::Compute::Vsphere::NotFound, "VirtualMachine with Managed Object Reference #{options['vm_ref']} could not be found."
  end
  response
end