Skip to content

Commit

Permalink
Adding test files
Browse files Browse the repository at this point in the history
  • Loading branch information
sacx13 committed Feb 20, 2013
1 parent 102669b commit 17a1edd
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
12 changes: 12 additions & 0 deletions test1.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'rbvmomi'

vim = RbVmomi::VIM.connect host: 'hostname',user: 'user', password: 'password', insecure: true
vm=vim.serviceInstance.find_datacenter.find_vm("Folder/tmpl-debian-6-64") or abort ("VM Not Found!")

VIM = RbVmomi::VIM
relocateSpec = RbVmomi::VIM.VirtualMachineRelocateSpec

spec = VIM.VirtualMachineCloneSpec(:location => relocateSpec, :powerOn => false, :template => false)
task = vm.CloneVM_Task(:folder => vm.parent, :name => "test-clone", :spec => spec)
print "Cloning ..."
task.wait_for_completion
43 changes: 43 additions & 0 deletions test2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#
#
# Simple example to clone a machine from an template with spec created from pool
# Author:: Adrian Stanila
#
#
require 'rbvmomi'

#Code from knife-vsphere BaseVsphereCommand.rb, modified for current needs.
#Code also can be simplified to look only for poolName directly, but this is not the point in this example
def find_pool(vim,poolName)
dcname = nil
dc = vim.serviceInstance.find_datacenter(dcname) or abort "datacenter not found"
baseEntity = dc.hostFolder
entityArray = poolName.split('/')
entityArray.each do |entityArrItem|
if entityArrItem != ''
if baseEntity.is_a? RbVmomi::VIM::Folder
baseEntity = baseEntity.childEntity.find { |f| f.name == entityArrItem } or abort "no such pool #{poolName} while looking for #{entityArrItem}"
elsif baseEntity.is_a? RbVmomi::VIM::ClusterComputeResource
baseEntity = baseEntity.resourcePool.resourcePool.find { |f| f.name == entityArrItem } or abort "no such pool #{poolName} while looking for #{entityArrItem}"
elsif baseEntity.is_a? RbVmomi::VIM::ResourcePool
baseEntity = baseEntity.resourcePool.find { |f| f.name == entityArrItem } or abort "no such pool #{poolName} while looking for #{entityArrItem}"
else
abort "Unexpected Object type encountered #{baseEntity.type} while finding resourcePool"
end
end
end

baseEntity = baseEntity.resourcePool if not baseEntity.is_a?(RbVmomi::VIM::ResourcePool) and baseEntity.respond_to?(:resourcePool)
baseEntity
end


vim = RbVmomi::VIM.connect host: 'hostname',user: 'user', password: 'password', insecure: true
vm=vim.serviceInstance.find_datacenter.find_vm("Folder/tmpl-debian-6-64") or abort ("VM Not Found!")

relocateSpec = RbVmomi::VIM.VirtualMachineRelocateSpec(:pool => find_pool(vim,'TEST'))
spec = RbVmomi::VIM.VirtualMachineCloneSpec(:location => relocateSpec, :powerOn => false, :template => false)

task = vm.CloneVM_Task(:folder => vm.parent, :name => "test-clone", :spec => spec)
print "Cloning ..."
task.wait_for_completion

0 comments on commit 17a1edd

Please sign in to comment.