module Fog::Vsphere::Compute::Shared
Constants
- ATTR_TO_PROP
Attributes
Protected Instance Methods
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.key == 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 || n.key == 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 || (n.is_a?(RbVmomi::VIM::DistributedVirtualPortgroup) && n.key == ref_or_name) } end end
Source
# File lib/fog/vsphere/compute.rb, line 192 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
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.
Source
# File lib/fog/vsphere/compute.rb, line 183 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
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
Source
# File lib/fog/vsphere/compute.rb, line 349 def is_uuid?(id) id.is_a?(String) && /[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}/.match?(id) end
Source
# File lib/fog/vsphere/compute.rb, line 345 def managed_obj_id(obj) obj.to_s.match(/\("([^"]+)"\)/)[1] end
returns vmware managed obj id string
Source
# File lib/fog/vsphere/compute.rb, line 290 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
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
Source
# File lib/fog/vsphere/compute.rb, line 308 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
Maps RbVmomi boot order values to fog understandable strings. Result is uniqued, what effectively means that the order is defined by the first occurence.
Source
# File lib/fog/vsphere/compute.rb, line 330 def parse_disks(vm_disks) return unless vm_disks.is_a?(Array) vm_disks.grep(RbVmomi::VIM::VirtualDisk).map do |d| { :label => d.deviceInfo.label, :capacity => d.capacityInBytes } end end
Source
# File lib/fog/vsphere/compute.rb, line 325 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
Flattens Array of RbVmomi::VIM::OptionValue to simple hash
Source
# File lib/fog/vsphere/compute.rb, line 337 def parse_partitions(vm_partitions) return unless vm_partitions.is_a?(Array) vm_partitions.grep(RbVmomi::VIM::GuestDiskInfo).map do |p| { :path => p.diskPath, :free => p.freeSpace, :capacity => p.capacity } end end
Source
# File lib/fog/vsphere/compute.rb, line 200 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['disks'] = parse_disks(attrs['disks']) attrs['partitions'] = parse_partitions(attrs['partitions']) 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
rubocop:disable Metrics/MethodLength
Private Instance Methods
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