String vmGuestOsId, VmDisk[] vmDisks,
VmNic[] vmNics, String vmDataCenterName, String vmDataStoreName, String vmClusterName ) throws Exception {
// ManagedEntity[] mes = new InventoryNavigator(rootFolder).searchManagedEntities("VirtualMachine");
System.out.println("Datacenter:"+vmDataCenterName);
Datacenter dc = (Datacenter) new InventoryNavigator(rootFolder).searchManagedEntity("Datacenter", vmDataCenterName);
//Now find the correct resourcepool to create the vm
ManagedEntity[] resourcepools = new InventoryNavigator(rootFolder).searchManagedEntities("ResourcePool");
ResourcePool rp = null;
System.out.println("Cluster:"+vmClusterName);
for(int i=0; i<resourcepools.length; i++)
{
ResourcePool resourcepool = (ResourcePool) resourcepools[i];
if (resourcepools[i].getParent().getName().equals(vmClusterName)) {
System.out.println("using cluster/resourcepool "+vmClusterName);
rp= (ResourcePool)resourcepools[i];
}
}
Folder vmFolder = dc.getVmFolder();
// create vm config spec
VirtualMachineConfigSpec vmSpec =
new VirtualMachineConfigSpec();
vmSpec.setName(vmName);
vmSpec.setAnnotation("Created by JvsphereControl");
vmSpec.setMemoryMB(vmMemorySize);
vmSpec.setNumCPUs(vmCpuCount);
vmSpec.setGuestId(vmGuestOsId);
System.out.println("Vmname:"+vmName);
System.out.println("Memorysize:"+vmMemorySize);
System.out.println("GuestOsId:"+vmGuestOsId);
//We create one scsi controller
VirtualDeviceConfigSpec machineSpecs[]= new VirtualDeviceConfigSpec[vmNics.length+1+vmDisks.length];
int cKey = 1000;
VirtualDeviceConfigSpec scsiSpec = VsphereUtils.createScsiSpec(cKey);
machineSpecs[0]=scsiSpec;
// Associate the virtual disks with the scsi controller
for (int i=0; i< vmDisks.length; i++) {
VirtualDeviceConfigSpec diskSpec = VsphereUtils.createDiskSpec(
vmDataStoreName, cKey, vmDisks[i].getSize(), vmDisks[i].getMode(),i);
System.out.println("Disk Datastore:"+vmDataStoreName);
System.out.println("Ckey:"+cKey);
System.out.println("DiskSize:"+vmDisks[i].getSize());
System.out.println("DiskMode:"+vmDisks[i].getMode());
machineSpecs[i+1]=diskSpec;
}
//virtual network interfaces
for (int i=0; i< vmNics.length; i++ ) {
machineSpecs[vmDisks.length+1+i]= VsphereUtils.createNicSpec(
vmNics[i].getName(), vmNics[i].getNetwork(),vmNics[i].isStartConnected(),vmNics[i].isConnected(),vmNics[i].getType());
System.out.println("NicName:"+vmNics[i].getName());
System.out.println("NicNetwork:"+vmNics[i].getNetwork());
System.out.println("NicStartConnected:"+vmNics[i].isStartConnected());
System.out.println("NicConnected:"+vmNics[i].isConnected());
System.out.println("NicType:"+vmNics[i].getType());
}
vmSpec.setDeviceChange(machineSpecs);
// create vm file info for the vmx file
VirtualMachineFileInfo vmfi = new VirtualMachineFileInfo();
vmfi.setVmPathName("["+ vmDataStoreName +"]");
System.out.println("Vm DatastoreName"+vmDataStoreName);
vmSpec.setFiles(vmfi);
// call the createVM_Task method on the vm folder
// TOO: null, host ?
Task task = vmFolder.createVM_Task(vmSpec, rp, null);
String result= task.waitForTask();
// String result = task.waitForMe();
VirtualMachine newVm=null;
if(result == Task.SUCCESS)
{
System.out.println("VM Created Sucessfully");
//Search for it and start it if requested
//TODO powerOnVM_Task (null) =!! null = host , maar was is die host?
newVm = (VirtualMachine) new InventoryNavigator(rootFolder).searchManagedEntity("VirtualMachine",vmName);
} else
{
System.out.println("VM could not be created. Error:");
if (task.getTaskInfo() != null && task.getTaskInfo().getError() != null)
{