Module: VagrantPlugins::VSphere::Util::VimHelpers

Included in:
Action::Clone, Action::Destroy, Action::GetSshInfo, Action::GetState, Action::PowerOff, Action::PowerOn
Defined in:
lib/vSphere/util/vim_helpers.rb

Instance Method Summary collapse

Instance Method Details

#find_clustercompute_or_compute_resource(datacenter, path) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/vSphere/util/vim_helpers.rb', line 31

def find_clustercompute_or_compute_resource(datacenter, path)
  if path.is_a? String
    es = path.split('/').reject(&:empty?)
  elsif path.is_a? Enumerable
    es = path
  else
    fail "unexpected path class #{path.class}"
  end
  return datacenter.hostFolder if es.empty?
  final = es.pop

  p = es.inject(datacenter.hostFolder) do |f, e|
    f.find(e, RbVmomi::VIM::Folder) || return
  end

  begin
    if (x = p.find(final, RbVmomi::VIM::ComputeResource))
      x
    elsif (x = p.find(final, RbVmomi::VIM::ClusterComputeResource))
      x
    end
  rescue Exception
    # When looking for the ClusterComputeResource there seems to be some parser error in RbVmomi Folder.find, try this instead
    x = p.childEntity.find { |x2| x2.name == final }
    if x.is_a?(RbVmomi::VIM::ClusterComputeResource) || x.is_a?(RbVmomi::VIM::ComputeResource)
      x
    else
      puts 'ex unknown type ' + x.to_json
      nil
    end
  end
end

#get_compute_resource(datacenter, machine) ⇒ Object



25
26
27
28
29
# File 'lib/vSphere/util/vim_helpers.rb', line 25

def get_compute_resource(datacenter, machine)
  cr = find_clustercompute_or_compute_resource(datacenter, machine.provider_config.compute_resource_name)
  fail Errors::VSphereError, :missing_compute_resource if cr.nil?
  cr
end

#get_customization_spec_info_by_name(connection, machine) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/vSphere/util/vim_helpers.rb', line 64

def get_customization_spec_info_by_name(connection, machine)
  name = machine.provider_config.customization_spec_name
  return if name.nil? || name.empty?

  manager = connection.serviceContent.customizationSpecManager
  fail Errors::VSphereError, :null_configuration_spec_manager if manager.nil?

  spec = manager.GetCustomizationSpec(name: name)
  fail Errors::VSphereError, :missing_configuration_spec if spec.nil?

  spec
end

#get_datacenter(connection, machine) ⇒ Object



7
8
9
# File 'lib/vSphere/util/vim_helpers.rb', line 7

def get_datacenter(connection, machine)
  connection.serviceInstance.find_datacenter(machine.provider_config.data_center_name) || fail(Errors::VSphereError, :missing_datacenter)
end

#get_datastore(datacenter, machine) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/vSphere/util/vim_helpers.rb', line 77

def get_datastore(datacenter, machine)
  name = machine.provider_config.data_store_name
  return if name.nil? || name.empty?

  # find_datastore uses folder datastore that only lists Datastore and not StoragePod, if not found also try datastoreFolder which contains StoragePod(s)
  datacenter.find_datastore(name) || datacenter.datastoreFolder.traverse(name) || fail(Errors::VSphereError, :missing_datastore)
end

#get_network_by_name(dc, name) ⇒ Object



85
86
87
# File 'lib/vSphere/util/vim_helpers.rb', line 85

def get_network_by_name(dc, name)
  dc.network.find { |f| f.name == name } || fail(Errors::VSphereError, :missing_vlan)
end

#get_resource_pool(datacenter, machine) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/vSphere/util/vim_helpers.rb', line 15

def get_resource_pool(datacenter, machine)
  compute_resource = get_compute_resource(datacenter, machine)
  rp = compute_resource.resourcePool
  unless machine.provider_config.resource_pool_name.nil?
    rp = compute_resource.resourcePool.find(machine.provider_config.resource_pool_name)
    fail Errors::VSphereError, :missing_resource_pool if rp.nil?
  end
  rp
end

#get_vm_by_uuid(connection, machine) ⇒ Object



11
12
13
# File 'lib/vSphere/util/vim_helpers.rb', line 11

def get_vm_by_uuid(connection, machine)
  get_datacenter(connection, machine).vmFolder.findByUuid machine.id
end