VMware {code} Community
complex
Contributor
Contributor

creating vm in a resource pool - vijava and jruby

I am using the SDK and VIJava with Jruby to access a vCenter and trying to create a VM in a ResourcePool.

It is failing with "The operation is not supported on the object". The vCenter logs show "[2011-02-15 11:30:00.991 01880 error 'App' opID=270500b0] [VpxdVAppUtil] Cannot create a vm inside a resource pool with no folder."

But I'm not quite sure how to set the folder. On what object do I set this?

My code is as follows:

require 'java'
require 'dom4j-1.6.1.jar'
require 'vijava2120100824.jar'
import java.net.URL
module VIJava
        include_package "com.vmware.vim25.mo"
end
module Vim
        include_package "com.vmware.vim25"
end
si = VIJava::ServiceInstance.new(URL.new("https://vcenter-sys.company.net/sdk"), "complex", "password", true)
rootFolder = si.getRootFolder()
qarp = VIJava::InventoryNavigator.new(rootFolder).searchManagedEntity("ResourcePool", "QA-CM")
dc = VIJava::InventoryNavigator.new(rootFolder).searchManagedEntity("Datacenter", "DC-A")
vmFolder = dc.get_vm_folder
vmspec = Vim::VirtualMachineConfigSpec.new
vmspec.setName("macguffin")
vmspec.setAnnotation("macguffin test machine")
vmspec.setMemoryMB(512)
vmspec.setNumCPUs(1)
vmspec.setLocationId("vms")
cKey = 1000 ## just some key that is unique for the machine we are building - can be reused.
## time to create the virtual devices that are part of the virtual machine config specification
## scsi controller create
scsiSpec = Vim::VirtualDeviceConfigSpec.new
scsiSpec.setOperation(Vim::VirtualDeviceConfigSpecOperation.add)
scsiCtrl = Vim::VirtualLsiLogicController.new
scsiCtrl.setKey(cKey)
scsiCtrl.setBusNumber(0)
scsiCtrl.setSharedBus(Vim::VirtualSCSISharing.noSharing)
scsiSpec.setDevice(scsiCtrl)
## disk create
diskSpec = Vim::VirtualDeviceConfigSpec.new
diskSpec.setOperation(Vim::VirtualDeviceConfigSpecOperation.add)
diskSpec.setFileOperation(Vim::VirtualDeviceConfigSpecFileOperation.create)
vd = Vim::VirtualDisk.new
vd.setCapacityInKB(1000000)
diskSpec.setDevice(vd)
vd.setKey(0); vd.setUnitNumber(0)
vd.setControllerKey(cKey)
diskfileBacking = Vim::VirtualDiskFlatVer2BackingInfo.new
diskfileBacking.setFileName("[aligntempvol]")
diskfileBacking.setDiskMode("persistent")
diskfileBacking.setThinProvisioned(true)
vd.setBacking(diskfileBacking)
## nic create
nicSpec = Vim::VirtualDeviceConfigSpec.new
nicSpec.setOperation(Vim::VirtualDeviceConfigSpecOperation.add)
nic = Vim::VirtualPCNet32.new
nicBacking = Vim::VirtualEthernetCardNetworkBackingInfo.new
nicBacking.setDeviceName("VLAN_700")
nicInfo = Vim::Description.new
nicInfo.setLabel("Network Adapter 0")


nicInfo.setSummary("VLAN_700")

nic.setDeviceInfo(nicInfo)

nic.setAddressType("generated")

nic.setBacking(nicBacking)

nic.setKey(0)

nicSpec.setDevice(nic)


vdcsarray = Vim::VirtualMachineConfigSpec.new

shitty = Array[scsiSpec, diskSpec, nicSpec]

vmspec.setDeviceChange(shitty)

vmfi = Vim::VirtualMachineFileInfo.new

vmfi.setVmPathName("[aligntempvol]")

vmspec.setFiles(vmfi)


qarp.create_child_vm_task(vmspec, nil)


si.getServerConnection().logout()

Tags (2)
0 Kudos
5 Replies
complex
Contributor
Contributor

I should say that this code is adapted from Steve Jin's very good "VMware VI and vSphere SDK" book.

0 Kudos
Steve_Jin
Expert
Expert

Hi Complex,

Thanks for supporting my book VMware VI and vSphere SDK!

According to the API reference of create_child_vm_task() method on ResourcePool type, "This method supports creating a virtual machine directly in a vApp. A   virtual  machine in a vApp is not associated with a VM folder and therefore   cannot be created using the method on a Folder. This method can only be called directly on a vApp or on a resource pool that is a child of a vApp."

Now back to your code, I think qarp is a top ResourcePool, not a child ResoucePool of a vApp. You may want to change your code accordingly.

Hope it helps.

Steve Jin (http://www.doublecloud.org)

Steve JIN Author of VMware VI and vSphere SDK; Creator of open source VI Java API (http://vijava.sf.net); Blogger at http://www.doublecloud.org
complex
Contributor
Contributor

You are correct, the qarp Resource Pool is at the root of a cluster, not in a vApp. OK, I understand that. However, in my Windows VI Client I can right-click on this Resource Pool and create a VM. Is there a way to accomplish the same thing using vijava/jruby?

0 Kudos
BenN
Enthusiast
Enthusiast

When you right-click on a ResourcePool to create a VM, though, the first thing it asks for you is a Folder. I don't know this for sure, but I suspect it's calling the Folder method CreateVM_Task, just as if you'd right-clicked on a Folder to create the VM (though there the wizard would prompt you for a resource pool).

Since you have a folder (vmfolder), why not call that method (dunno what the Ruby name is), passing qarp as the pool argument?

complex
Contributor
Contributor

Ah, Steve, you are correct. I should have examined the documentation closer to see the vApp restrictions on the ResourcePool object.

And Ben, your plan of attack seems sound. Yes, in Ruby they are called methods. I will attack the problem by specifying the Folder and then CreateVM_Task on that object.

Thanks to you both.

0 Kudos