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/vm_clone.rb
Jeff McCune ef2833cb20 (#9241) Add vm_clone API request
Without this patch we have no way to clone a new VM
from a template.  This patch adds the vm_clone request
which takes an instance uuid as the source template
to clone from an a name parameter which is the new VM's
name.

The clone operation is handled asynchronously as a vSphere
task object.  The clone request does not return a handle
for this task, which may make it difficult to monitor
the progress of the clone operation from Fog.  A future
enhancement may be to clone and return the task object
itself to monitor progress.
2011-09-10 13:32:58 -07:00

42 lines
1.6 KiB
Ruby

module Fog
module Compute
class Vsphere
class Real
def vm_clone(params = {})
raise ArgumentError, ":instance_uuid and :name are required" if params.empty?
# First, find the Managed Object of the template VM
vm = find_template_by_instance_uuid(params[:instance_uuid])
# We need to locate the datacenter object to find the
# default resource pool.
container = vm.parent
until container.kind_of? RbVmomi::VIM::Datacenter
container = container.parent
end
dc = container
# With the Datacenter Object we can obtain the resource pool
resource_pool = dc.hostFolder.children.first.resourcePool
# Next, create a Relocation Spec instance
relocation_spec = RbVmomi::VIM.VirtualMachineRelocateSpec(:pool => resource_pool,
:transform => 'sparse')
# And the clone specification
clone_spec = RbVmomi::VIM.VirtualMachineCloneSpec(:location => relocation_spec,
:powerOn => true,
:template => false)
task = vm.CloneVM_Task(:folder => vm.parent, :name => params[:name], :spec => clone_spec)
# REVISIT: We may want to return an identifier for the asyncronous task
task.info.state
end
end
class Mock
def vm_clone(params = {})
"running"
end
end
end
end
end