Skip to content

Commit

Permalink
Ensure find_vm returns nil when pool configuration or datacenter cann…
Browse files Browse the repository at this point in the history
…ot be found. Document expected return values. Remove underscore preface from pool_name where the variable is used.
  • Loading branch information
mattkirby committed May 11, 2018
1 parent 342d08f commit 7f1bb9a
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions lib/vmpooler/providers/vsphere.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,11 @@ def vm_in_target?(pool_name, parent_host, architecture, target)
return false
end

def get_vm(_pool_name, vm_name)
def get_vm(pool_name, vm_name)
vm_hash = nil
@connection_pool.with_metrics do |pool_object|
connection = ensured_vsphere_connection(pool_object)
vm_object = find_vm(_pool_name, vm_name, connection)
vm_object = find_vm(pool_name, vm_name, connection)
return vm_hash if vm_object.nil?

vm_folder_path = get_vm_folder_path(vm_object)
Expand Down Expand Up @@ -324,10 +324,10 @@ def revert_snapshot(pool_name, vm_name, snapshot_name)
true
end

def destroy_vm(_pool_name, vm_name)
def destroy_vm(pool_name, vm_name)
@connection_pool.with_metrics do |pool_object|
connection = ensured_vsphere_connection(pool_object)
vm_object = find_vm(_pool_name, vm_name, connection)
vm_object = find_vm(pool_name, vm_name, connection)
# If a VM doesn't exist then it is effectively deleted
return true if vm_object.nil?

Expand Down Expand Up @@ -793,9 +793,14 @@ def build_propSpecs(datacenter, folder, vmname)
end

def find_vm(pool_name, vmname, connection)
# Find a VM by its inventory path and return the VM object
# Returns nil when a VM, or pool configuration, cannot be found
pool_configuration = pool_config(pool_name)
return nil if pool_configuration.nil?
folder = pool_configuration['folder']
datacenter = get_target_datacenter_from_config(pool_name)
return nil if datacenter.nil?

propSpecs = {
:entity => self,
:inventoryPath => "#{datacenter}/vm/#{folder}/#{vmname}"
Expand Down

0 comments on commit 7f1bb9a

Please sign in to comment.