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/find_vm_by_ref.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#vsphere_is_vcenterObject (readonly)

Returns the value of attribute vsphere_is_vcenter.



27
28
29
# File 'lib/fog/vsphere/compute.rb', line 27

def vsphere_is_vcenter
  @vsphere_is_vcenter
end

#vsphere_revObject (readonly)

Returns the value of attribute vsphere_rev.



28
29
30
# File 'lib/fog/vsphere/compute.rb', line 28

def vsphere_rev
  @vsphere_rev
end

#vsphere_serverObject (readonly)

Returns the value of attribute vsphere_server.



29
30
31
# File 'lib/fog/vsphere/compute.rb', line 29

def vsphere_server
  @vsphere_server
end

#vsphere_usernameObject (readonly)

Returns the value of attribute vsphere_username.



30
31
32
# File 'lib/fog/vsphere/compute.rb', line 30

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.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/fog/vsphere/compute.rb', line 36

def convert_vm_mob_ref_to_attr_hash(vm_mob_ref)
  return nil unless vm_mob_ref
  # A cloning VM doesn't have a configuration yet.  Unfortuantely we just get
  # a RunTime exception.
  begin
    is_ready = vm_mob_ref.config ? true : false
  rescue RuntimeError
    is_ready = nil
  end
  {
    'id'               => is_ready ? vm_mob_ref.config.instanceUuid : vm_mob_ref._ref,
    'mo_ref'           => vm_mob_ref._ref,
    'name'             => vm_mob_ref.name,
    'uuid'             => is_ready ? vm_mob_ref.config.uuid : nil,
    'instance_uuid'    => is_ready ? vm_mob_ref.config.instanceUuid : nil,
    'hostname'         => vm_mob_ref.summary.guest.hostName,
    'operatingsystem'  => vm_mob_ref.summary.guest.guestFullName,
    'ipaddress'        => vm_mob_ref.summary.guest.ipAddress,
    'power_state'      => vm_mob_ref.runtime.powerState,
    'connection_state' => vm_mob_ref.runtime.connectionState,
    'hypervisor'       => vm_mob_ref.runtime.host ? vm_mob_ref.runtime.host.name : nil,
    'tools_state'      => vm_mob_ref.summary.guest.toolsStatus,
    'tools_version'    => vm_mob_ref.summary.guest.toolsVersionStatus,
    'mac_addresses'    => is_ready ? vm_mob_ref.macs : nil,
    'is_a_template'    => is_ready ? vm_mob_ref.config.template : nil
  }
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