Module: SearchHelper

Defined in:
lib/chef/knife/helpers/search_helper.rb

Overview

Some helpers for faster searching of the inventory

Instance Method Summary collapse

Instance Method Details

#get_all_objects(opts = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/chef/knife/helpers/search_helper.rb', line 13

def get_all_objects(opts = {})
  pc = vim_connection.serviceInstance.content.propertyCollector
  viewmgr = vim_connection.serviceInstance.content.viewManager
  folder = if opts[:folder]
             find_folder(opts[:folder])
           else
             vim_connection.serviceInstance.content.rootFolder
           end
  vmview = viewmgr.CreateContainerView(container: folder,
                                       type: [opts[:type]],
                                       recursive: true)

  opts[:properties] ||= ["name"]

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

#get_all_vm_objects(opts = {}) ⇒ 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
# File 'lib/chef/knife/helpers/search_helper.rb', line 9

def get_all_vm_objects(opts = {})
  get_all_objects(opts.merge(type: "VirtualMachine"))
end

#get_vm_by_name(vmname, folder = nil) ⇒ Object



47
48
49
50
# File 'lib/chef/knife/helpers/search_helper.rb', line 47

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

#get_vm_host_by_name(name, folder = nil) ⇒ Object



52
53
54
55
# File 'lib/chef/knife/helpers/search_helper.rb', line 52

def get_vm_host_by_name(name, folder = nil)
  host = get_all_objects(type: "HostSystem", folder: folder).detect { |r| r["name"] == name }
  host ? host.obj : nil
end