module Fog::Compute::Vsphere::Shared

Constants

ATTR_TO_PROP

Attributes

vsphere_is_vcenter[R]
vsphere_rev[R]
vsphere_server[R]
vsphere_username[R]

Protected Instance Methods

choose_finder(name, distributedswitch) click to toggle source
# File lib/fog/vsphere/requests/compute/get_network.rb, line 26
def choose_finder(name, distributedswitch)
  case distributedswitch
  when String
    # only the one will do
    proc do |n|
      n.is_a?(RbVmomi::VIM::DistributedVirtualPortgroup) && (n.name == name || n.key == name) &&
        (n.config.distributedVirtualSwitch.name == distributedswitch)
    end
  when :dvs
    # the first distributed virtual switch will do - selected by network - gives control to vsphere
    proc { |n| (n.name == name || n.key == name) && (n.class.to_s == 'DistributedVirtualPortgroup') }
  else
    # the first matching network will do, seems like the non-distributed networks come first
    proc { |n| (n.name == name || (n.is_a?(RbVmomi::VIM::DistributedVirtualPortgroup) && n.key == name)) }
  end
end
convert_vm_mob_ref_to_attr_hash(vm_mob_ref) click to toggle source

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.

# File lib/fog/vsphere/compute.rb, line 178
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)
  props_to_attr_hash vm_mob_ref, props
end
convert_vm_view_to_attr_hash(vms) click to toggle source
# File lib/fog/vsphere/compute.rb, line 169
def convert_vm_view_to_attr_hash(vms)
  vms = connection.serviceContent.propertyCollector.collectMultiple(vms, *ATTR_TO_PROP.values.uniq)
  vms.map { |vm| props_to_attr_hash(*vm) }
end
get_all_raw_networks(datacenter_name) click to toggle source
# File lib/fog/vsphere/requests/compute/get_network.rb, line 22
def get_all_raw_networks(datacenter_name)
  list_container_view(datacenter_name, 'Network', :networkFolder)
end
is_uuid?(id) click to toggle source
# File lib/fog/vsphere/compute.rb, line 273
def is_uuid?(id)
  !(id =~ /[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}/).nil?
end
managed_obj_id(obj) click to toggle source

returns vmware managed obj id string

# File lib/fog/vsphere/compute.rb, line 269
def managed_obj_id(obj)
  obj.to_s.match(/\("([^"]+)"\)/)[1]
end
parent_attribute(path, type) click to toggle source

returns the parent object based on a type provides both real RbVmomi object and its name. e.g.

Datacenter(“datacenter-2”), “dc-name”
# File lib/fog/vsphere/compute.rb, line 252
def parent_attribute(path, type)
  element = case type
            when :datacenter
              RbVmomi::VIM::Datacenter
            when :cluster
              RbVmomi::VIM::ClusterComputeResource
            when :host
              RbVmomi::VIM::HostSystem
            else
              raise 'Unknown type'
            end
  path.select { |x| x[0].is_a? element }.flatten
rescue
  nil
end
props_to_attr_hash(vm_mob_ref, props) click to toggle source
# File lib/fog/vsphere/compute.rb, line 185
def props_to_attr_hash(vm_mob_ref, props)
  # 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'].is_a?(RbVmomi::VIM::HostSystem)
      host = attrs['hypervisor']
      attrs['datacenter'] = proc {
        begin
                   parent_attribute(host.path, :datacenter)[1]
                 rescue
                   nil
                 end
      }
      attrs['cluster'] = proc {
        begin
                   parent_attribute(host.path, :cluster)[1]
                 rescue
                   nil
                 end
      }
      attrs['hypervisor'] = proc {
        begin
                   host.name
                 rescue
                   nil
                 end
      }
      attrs['resource_pool'] = proc {
        begin
                  (vm_mob_ref.resourcePool || host.resourcePool).name
                rescue
                  nil
                end
      }
    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'] = proc {
      begin
                vm_mob_ref.macs
              rescue
                nil
              end
    }
    # Rescue nil to catch testing while vm_mob_ref isn't reaL??
    attrs['path'] = begin
                      '/' + attrs['parent'].path.map(&:last).join('/')
                    rescue
                      nil
                    end
  end
end

Private Instance Methods

vm_clone_check_options(options) click to toggle source
# File lib/fog/vsphere/requests/compute/vm_clone.rb, line 7
def vm_clone_check_options(options)
  default_options = {
    'force'        => false,
    'linked_clone' => false,
    'nic_type' => 'VirtualE1000'
  }
  options = default_options.merge(options)
  options['storage_pod'] = nil if options['storage_pod'] == ''
  # Backwards compat for "path" option
  options['template_path'] ||= options['path']
  options['path'] ||= options['template_path']
  required_options = %w[datacenter template_path name]
  required_options.each do |param|
    raise ArgumentError, "#{required_options.join(', ')} are required" unless options.key? param
  end
  raise Fog::Compute::Vsphere::NotFound, "Datacenter #{options['datacenter']} Doesn't Exist!" unless get_datacenter(options['datacenter'])
  raise Fog::Compute::Vsphere::NotFound, "Template #{options['template_path']} Doesn't Exist!" unless get_virtual_machine(options['template_path'], options['datacenter'])
  if options.key?('storage_pod') && !options['storage_pod'].nil? && !get_raw_storage_pod(options['storage_pod'], options['datacenter'])
    raise Fog::Compute::Vsphere::NotFound, "Storage Pod #{options['storage_pod']} Doesn't Exist!"
  end
  options
end