Class: VcenterLib::VmConverter

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/vcenter_lib/vm_converter.rb

Overview

convert VMware managed object into a simple hash

Constant Summary collapse

ATTRIBUTES =
[
  'name',
  'config.annotation',
  'config.cpuHotAddEnabled',
  'config.instanceUuid',
  'config.firmware',
  'config.memoryHotAddEnabled',
  'config.template',
  'config.uuid',
  'config.hardware.numCoresPerSocket',
  'config.hardware.numCPU',
  'config.hardware.memoryMB',
  'config.guestFullName',
  'config.guestId',
  'config.version',
  'guest.guestState',
  'guest.ipAddress',
  'guest.toolsStatus',
  'guest.toolsVersionStatus',
  'overallStatus',
  'runtime.bootTime',
  'runtime.connectionState',
  'runtime.maxCpuUsage',
  'runtime.maxMemoryUsage',
  'runtime.powerState',
  'summary.config.numEthernetCards',
  'summary.config.numVirtualDisks',
  'summary.guest.guestFullName',
  'summary.guest.hostName',

  'runtime.host',
  # 'resourcePool',
  # 'parent',
  # 'summary.quickStats',

  # summary:          'summary',
  # storage:          'storage',
  # devices:          'config.hardware.device',
].freeze

Instance Method Summary collapse

Methods included from Logging

included, #logger, #logger=

Constructor Details

#initialize(vcenter) ⇒ VmConverter

Returns a new instance of VmConverter.



49
50
51
# File 'lib/vcenter_lib/vm_converter.rb', line 49

def initialize(vcenter)
  @vcenter = vcenter
end

Instance Method Details

#factsObject

get all vms and their facts as hash with vm.name as key



54
55
56
57
58
59
60
61
# File 'lib/vcenter_lib/vm_converter.rb', line 54

def facts
  logger.debug "get complete data of all VMs in all datacenters: begin"
  result = Hash[vm_mos_to_h(@vcenter.vms).map do |h|
    [h['name'], Hash[h.map { |k, v| [k.tr('.', '_'), v] }]]
  end]
  logger.debug "get complete data of all VMs in all datacenters: end"
  result
end

#vm_mo_to_h(vm_mo, attributes = ATTRIBUTES) ⇒ Object

convert a VMware RbVmomi::VIM::ManagedObject into a simple hash.



64
65
66
67
# File 'lib/vcenter_lib/vm_converter.rb', line 64

def vm_mo_to_h(vm_mo, attributes = ATTRIBUTES)
  return nil unless vm_mo
  props2h(vm_mo.collect!(*attributes))
end

#vm_mos_to_h(vm_mobs, attributes = ATTRIBUTES) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/vcenter_lib/vm_converter.rb', line 69

def vm_mos_to_h(vm_mobs, attributes = ATTRIBUTES)
  vms = @vcenter.serviceContent.propertyCollector.collectMultiple(vm_mobs, *attributes)
  hosts = {} # cache already known hosts here
  vms.map do |_vm, props|
    extra = {}
    props.delete_if do |_k, v|
      if v.is_a? RbVmomi::VIM::HostSystem
        extra = hosts[v] || {}
        if extra.empty?
          # rubocop:disable Style/RescueModifier
          extra['datacenter'] = attribute(v.path, RbVmomi::VIM::Datacenter) rescue nil
          extra['cluster'] = attribute(v.path, RbVmomi::VIM::ClusterComputeResource) rescue nil
          extra['hypervisor'] = v.name rescue nil
          # rubocop:enable Style/RescueModifier
          hosts[v] = extra
        end
        true
      end
    end
    props.merge!(extra)
  end
end