1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00
fog--fog/lib/fog/vsphere/requests/compute/find_vm_by_ref.rb
Jeff McCune 743882f032 Refactor requests to return simple hashes and add unit tests
This massive commit refactors all of the request methods on the
Fog::Compute[:vsphere] instance to return simple hashes.  The behavior
before this commit returned full vmware object references which was a
problem because it was difficult to unit test.

With this patch, it is much easier to add and maintain Mock
implementations of the request methods.  This makes adding behavior
tests for the server model much easier.

In addition, test coverage using Shindo has been added.  Previously
there was little test coverage of the behavior.

To run the tests:

    shindont tests/vsphere/
2011-09-10 15:11:18 -07:00

41 lines
1.4 KiB
Ruby

module Fog
module Compute
class Vsphere
module Shared
# REVISIT: This is a naive implementation and not very efficient since
# we find ALL VM's and then iterate over them looking for the managed object
# reference id... There should be an easier way to obtain a reference to a
# VM using only the name or the _ref. This request is primarily intended to
# reload the attributes of a cloning VM which does not yet have an instance_uuid
def find_vm_by_ref(options = {})
raise ArgumentError, "Must pass a vm_ref option" unless options['vm_ref']
# This is the inefficient call
all_vm_attributes = list_virtual_machines['virtual_machines']
# Find the VM attributes of the reference
if vm_attributes = all_vm_attributes.find { |vm| vm['mo_ref'] == options['vm_ref'] }
response = { 'virtual_machine' => vm_attributes }
else
raise Fog::Compute::Vsphere::NotFound, "VirtualMachine with Managed Object Reference #{options['vm_ref']} could not be found."
end
response
end
end
# The Real and Mock classes share the same method
# because list_virtual_machines will be properly mocked for us
class Real
include Shared
end
class Mock
include Shared
end
end
end
end