Sha256: 204edccdf6ddaf38783fc37d84536b4b379291b47bdd1652e2bfe410510fcb4d

Contents?: true

Size: 1.96 KB

Versions: 4

Compression:

Stored size: 1.96 KB

Contents

module Fog
  module Compute
    class Vsphere
      class Real
        def get_virtual_machine(id, datacenter_name = nil)
          # The larger the VM list the longer it will take if not searching based on UUID.
          convert_vm_mob_ref_to_attr_hash(get_vm_ref(id, datacenter_name))
        end

        protected

        def get_vm_ref(id, dc = nil)
          raw_datacenter = find_raw_datacenter(dc) if dc
          vm = case is_uuid?(id)
                 # UUID based
                 when true
                   params = {:uuid => id, :vmSearch => true, :instanceUuid => true}
                   params[:datacenter] = raw_datacenter if dc
                   @connection.searchIndex.FindByUuid(params)
                 else
                   # try to find based on VM name
                   if dc
                     get_vm_by_name(id, dc)
                   else
                     raw_datacenters.map { |d| get_vm_by_name(id, d["name"])}.compact.first
                   end
               end
          vm ? vm : raise(Fog::Compute::Vsphere::NotFound, "#{id} was not found")
        end

        def get_vm_by_name(name, dc)
          vms = raw_list_all_virtual_machines(dc)

          if name.include?('/')
            folder, basename = name.split('/')
            vms.keep_if { |v| v["name"] == basename && v.parent["name"] == folder }.first
          else
            vms.keep_if { |v| v["name"] == name }.first
          end
        end
      end

      class Mock
        def get_virtual_machine(id, datacenter_name = nil)
          if is_uuid?(id)
            vm = list_virtual_machines({ 'instance_uuid' => id, 'datacenter' => datacenter_name }).first
          else
            # try to find based on VM name. May need to handle the path of the VM
            vm = list_virtual_machines({ 'name' => id, 'datacenter' => datacenter_name }).first
          end
          vm ? vm : raise(Fog::Compute::Vsphere::NotFound, "#{id} was not found")
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
vagrant-cloudstack-1.2.0 vendor/bundle/gems/fog-1.32.0/lib/fog/vsphere/requests/compute/get_virtual_machine.rb
fog-1.32.0 lib/fog/vsphere/requests/compute/get_virtual_machine.rb
fog-1.31.0 lib/fog/vsphere/requests/compute/get_virtual_machine.rb
fog-1.30.0 lib/fog/vsphere/requests/compute/get_virtual_machine.rb