1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

(#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.
This commit is contained in:
Jeff McCune 2011-09-06 16:46:42 -07:00
parent 856eb7e033
commit ef2833cb20
2 changed files with 43 additions and 0 deletions

View file

@ -18,6 +18,7 @@ module Fog
request :vm_power_off
request :vm_power_on
request :vm_reboot
request :vm_clone
request :vm_destroy
request :find_all_by_uuid
request :find_all_by_instance_uuid

View file

@ -0,0 +1,42 @@
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