The first question I have is it possible to add a disk using a different datastoreCluster than was used when the VM was cloned from template? Does it require a particular affinity to be set up?
Basically I'm getting the following: java.rmi.RemoteException: VI SDK invoke exception:com.vmware.vim25.InvalidRequest
Here is most of my code, any help would be appreciated. Ultimately I would like to create a number of different disks on different pods for this one vm if that is possible.
// Create a StoragePlacementSpec
StoragePlacementSpec storageSpec = new StoragePlacementSpec();
// Set type to reconfigure
storageSpec.setType(StoragePlacementSpecPlacementType.reconfigure.name());
// Add the virtual machine to the storage spec
storageSpec.setVm(vm.getMOR());
// Create a pod selection spec
StorageDrsPodSelectionSpec podSelectionSpec = new StorageDrsPodSelectionSpec();
// Create a VirtualMachineConfigSpec
VirtualMachineConfigSpec configSpec = new VirtualMachineConfigSpec();
// Code for configuring the new disks.
VirtualDeviceConfigSpec[] deviceChange = new VirtualDeviceConfigSpec[virtualDisks.size()];
for (int i=0; i<virtualDisks.size(); i++) {
VirtualDisk virtualDisk = virtualDisks.get(i);
Integer diskSizeGB = virtualDisk.getSizeGB();
// Create a VirtualDeviceConfigSpec
VirtualDeviceConfigSpec diskSpec = new VirtualDeviceConfigSpec();
// set this disk Spec into the array of changes
deviceChange[i] = diskSpec;
// Create a backing object
VirtualDiskFlatVer2BackingInfo backing = new VirtualDiskFlatVer2BackingInfo();
// Set the Disk mode to persistent
backing.setDiskMode(VirtualDiskMode.persistent.name());
// Create a new vmware VirtualDisk
com.vmware.vim25.VirtualDisk vDisk = new com.vmware.vim25.VirtualDisk();
// Set the backing object
vDisk.setBacking(backing);
// Set the capacity
vDisk.setCapacityInKB(diskSizeGB*1024L*1024L);
// Find and set the Virtual Disk key
int key;
key = getNextKey(usedKeys);
vDisk.setKey(key);
virtualDisk.setDeviceId(key);
// Set the Virtual Disk as the device
diskSpec.setDevice(vDisk);
// Set Operation and File Operation as add and create
diskSpec.setOperation(VirtualDeviceConfigSpecOperation.add);
diskSpec.setFileOperation(VirtualDeviceConfigSpecFileOperation.create);
}
// Add the changed devices to the VirtualMachineConfigSpec
configSpec.setDeviceChange(deviceChange);
// Add the VirtualMachineConfigSpec to the StoragePlacementSpec
storageSpec.setConfigSpec(configSpec);
// Create a array for Initial VM Config, not sure if this is necessary or not
VmPodConfigForPlacement[] initialVmConfigArray = new VmPodConfigForPlacement[dsClusterMap.size()];
int i = 0;
// For each datastoreCluster
for (String datastoreClusterName:dsClusterMap.keySet()) {
// Create a VmPodCOnfigForPlacement object and assign it to the array.
VmPodConfigForPlacement initialVmConfig = new VmPodConfigForPlacement();
initialVmConfigArray[i++] = initialVmConfig;
// Set the storagePod for this disk
initialVmConfig.storagePod = dsClusterMap.get(datastoreClusterName).getMOR();
int numberOfDisks = dsClusterToDiskMap.get(datastoreClusterName).size();
// Create a array of PodDiskLocator objects for each disk targeting this storagePod
PodDiskLocator[] disks = new PodDiskLocator[numberOfDisks];
int j=0;
// For each disk, create a PodDiskLocator, add to the array and set the DiskId, VirtualDisk key
for (VirtualDisk virtualDisk:dsClusterToDiskMap.get(datastoreClusterName)) {
PodDiskLocator disk = new PodDiskLocator();
disks[j++] = disk;
disk.setDiskId(virtualDisk.getDeviceId());
disk.setDiskMoveType("");
}
}
podSelectionSpec.initialVmConfig = initialVmConfigArray;
// Set the podSelectionSpec.storagePod to the pod of the first disk
String datastoreClusterName = virtualDisks.get(0).getDatastoreCluster();
StoragePod storagePod = dsClusterMap.get(datastoreClusterName);
podSelectionSpec.setStoragePod(storagePod.getMOR());
// Add the podSelectionSpec to the StoragePlacementSpec.
storageSpec.setPodSelectionSpec(podSelectionSpec);
StoragePlacementResult storPlaceResult = null;
// End: RecommendDatastores
try
{
storageSpec.setFolder(datacenter.getVmFolder().getMOR());
storPlaceResult = srm.recommendDatastores(storageSpec);