Module: SearchHelper

Overview

Some helpers for faster searching of the inventory

Instance Method Summary collapse

Instance Method Details

#get_all_vm_objects(properties = ['name']) ⇒ Array<RbVmomi::VIM::ObjectContent>

Retrieves all the VM objects and returns their ObjectContents Note that since it’s a ObjectContent coming back, the individual object’s [] will return only the properties you asked for and ‘#obj` will return the actual object (but make a call to the server) param [Array<String>] properties to retrieve

Returns:

  • (Array<RbVmomi::VIM::ObjectContent>)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/chef/knife/search_helper.rb', line 9

def get_all_vm_objects(properties = ['name'])
  pc = vim_connection.serviceInstance.content.propertyCollector
  viewmgr = vim_connection.serviceInstance.content.viewManager
  root_folder = vim_connection.serviceInstance.content.rootFolder
  vmview = viewmgr.CreateContainerView(container: root_folder,
                                       type: ['VirtualMachine'],
                                       recursive: true)

  filter_spec = RbVmomi::VIM.PropertyFilterSpec(
    objectSet: [
      obj: vmview,
      skip: true,
      selectSet: [
        RbVmomi::VIM.TraversalSpec(
          name: 'traverseEntities',
          type: 'ContainerView',
          path: 'view',
          skip: false
        )
      ]
    ],
    propSet: [
      { type: 'VirtualMachine', pathSet: properties }
    ]
  )
  pc.RetrieveProperties(specSet: [filter_spec])
end

#get_vm_by_name(vmname) ⇒ Object



37
38
39
40
# File 'lib/chef/knife/search_helper.rb', line 37

def get_vm_by_name(vmname)
  vm = get_all_vm_objects.detect { |r| r['name'] == vmname }
  vm ? vm.obj : nil
end