module Fog::Vsphere::Compute::Shared

Constants

ATTR_TO_PROP

Attributes

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

Protected Instance Methods

choose_finder(ref_or_name, distributedswitch) click to toggle source
# File lib/fog/vsphere/requests/compute/get_network.rb, line 35
def choose_finder(ref_or_name, distributedswitch)
  case distributedswitch
  when String
    # only the one will do
    proc do |n|
      n._ref == ref_or_name || (
        n.is_a?(RbVmomi::VIM::DistributedVirtualPortgroup) && (n.name == ref_or_name) &&
        (n.config.distributedVirtualSwitch.name == distributedswitch)
      )
    end
  when :dvs
    # the first distributed virtual switch will do - selected by network - gives control to vsphere
    proc do |n|
      n._ref == ref_or_name || (
        n.is_a?(RbVmomi::VIM::DistributedVirtualPortgroup) && (n.name == ref_or_name)
      )
    end
  else
    # the first matching network will do, seems like the non-distributed networks come first
    proc { |n| n._ref == ref_or_name || n.name == ref_or_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 184
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 175
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 31
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 325
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 321
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”

rubocop:enable Metrics/MethodLength

# File lib/fog/vsphere/compute.rb, line 280
def parent_attribute(path, type)
  element = case type
            when :datacenter
              RbVmomi::VIM::Datacenter
            when :cluster
              RbVmomi::VIM::ComputeResource
            when :host
              RbVmomi::VIM::HostSystem
            else
              raise 'Unknown type'
            end
  path.select { |x| x[0].is_a? element }.flatten
rescue
  nil
end
parse_boot_order(vm_boot_devs) click to toggle source

Maps RbVmomi boot order values to fog understandable strings. Result is uniqued, what effectively means that the order is defined by the first occurence.

# File lib/fog/vsphere/compute.rb, line 298
def parse_boot_order(vm_boot_devs)
  return unless vm_boot_devs.is_a?(Array)
  vm_boot_devs.map do |vm_boot_dev|
    case vm_boot_dev
    when RbVmomi::VIM::VirtualMachineBootOptionsBootableEthernetDevice
      'network'
    when RbVmomi::VIM::VirtualMachineBootOptionsBootableDiskDevice
      'disk'
    when RbVmomi::VIM::VirtualMachineBootOptionsBootableCdromDevice
      'cdrom'
    when RbVmomi::VIM::VirtualMachineBootOptionsBootableFloppyDevice
      'floppy'
    end
  end.compact.uniq
end
parse_extra_config(vm_extra_config) click to toggle source

Flattens Array of RbVmomi::VIM::OptionValue to simple hash

# File lib/fog/vsphere/compute.rb, line 315
def parse_extra_config(vm_extra_config)
  return unless vm_extra_config.is_a?(Array)
  vm_extra_config.map { |entry| [entry[:key], entry[:value]] }.to_h
end
props_to_attr_hash(vm_mob_ref, props) click to toggle source

rubocop:disable Metrics/MethodLength

# File lib/fog/vsphere/compute.rb, line 192
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.
    attrs['boot_order'] = parse_boot_order(attrs['boot_order'])

    if attrs['hypervisor'].is_a?(RbVmomi::VIM::HostSystem)
      host = attrs['hypervisor']

      # @note
      #   - User might not have permission to access host
      #   - i.e. host.name might raise NoPermission error
      has_permission_to_access_host = true
      begin
        host.name
      rescue RbVmomi::Fault => e
        raise e unless e.message && e.message['NoPermission:']
        has_permission_to_access_host = false
      end

      attrs['datacenter'] = proc {
        begin
          if has_permission_to_access_host
            parent_attribute(host.path, :datacenter)[1]
          else
            parent_attribute(vm_mob_ref.path, :datacenter)[1]
          end
        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
      }

      attrs['extra_config'] = parse_extra_config(attrs['extra_config'])
    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 ArgumentError, 'cluster option is required' unless options['resource_pool'][0]
  raise Fog::Vsphere::Compute::NotFound, "Datacenter #{options['datacenter']} Doesn't Exist!" unless get_datacenter(options['datacenter'])
  if options['template_datacenter'] && !get_datacenter(options['template_datacenter'])
    raise Fog::Vsphere::Compute::NotFound, "Datacenter #{options['template_datacenter']} Doesn't Exist!"
  end
  raise Fog::Vsphere::Compute::NotFound, "Template #{options['template_path']} Doesn't Exist!" unless get_virtual_machine(options['template_path'], options['template_datacenter'] || options['datacenter'])
  raise Fog::Vsphere::Compute::NotFound, "Cluster #{options['resource_pool'][0]} Doesn't Exist in the DC!" unless get_raw_cluster(options["resource_pool"][0], options['datacenter'])
  raise ArgumentError, 'path option is required' unless options.fetch('dest_folder', '/')
  if options.key?('datastore') && !options['datastore'].nil? && !get_raw_datastore(options['datastore'], options['datacenter'])
    raise Fog::Vsphere::Compute::NotFound, "Datastore #{options['datastore']} Doesn't Exist!"
  end
  if options.key?('storage_pod') && !options['storage_pod'].nil? && !get_raw_storage_pod(options['storage_pod'], options['datacenter'])
    raise Fog::Vsphere::Compute::NotFound, "Storage Pod #{options['storage_pod']} Doesn't Exist!"
  end
  options
end