use of com.vmware.photon.controller.model.resources.DiskService.DiskType in project photon-model by vmware.
the class InstanceClient method attachDisks.
/**
* Creates disks and attaches them to the vm created by {@link #createInstance()}. The given
* diskStates are enriched with data from vSphere and can be patched back to xenon.
*/
public void attachDisks(List<DiskStateExpanded> diskStates, boolean isImageDisks) throws Exception {
if (this.vm == null) {
throw new IllegalStateException("Cannot attach diskStates if VM is not created");
}
EnumSet<DiskType> notSupportedTypes = EnumSet.of(DiskType.SSD, DiskType.NETWORK);
List<DiskStateExpanded> unsupportedDisks = diskStates.stream().filter(d -> notSupportedTypes.contains(d.type)).collect(Collectors.toList());
if (!unsupportedDisks.isEmpty()) {
throw new IllegalStateException("Some diskStates cannot be created: " + unsupportedDisks.stream().map(d -> d.documentSelfLink).collect(Collectors.toList()));
}
// the path to folder holding all vm files
String dir = this.get.entityProp(this.vm, VimPath.vm_config_files_vmPathName);
dir = Paths.get(dir).getParent().toString();
ArrayOfVirtualDevice devices = this.get.entityProp(this.vm, VimPath.vm_config_hardware_device);
VirtualSCSIController scsiController = getFirstScsiController(devices);
// Get available free unit numbers for the given scsi controller.
Integer[] scsiUnits = findFreeScsiUnit(scsiController, devices.getVirtualDevice());
VirtualDevice ideController = getFirstIdeController(devices);
int ideUnit = findFreeUnit(ideController, devices.getVirtualDevice());
VirtualDevice sioController = getFirstSioController(devices);
int sioUnit = findFreeUnit(sioController, devices.getVirtualDevice());
List<VirtualDeviceConfigSpec> newDisks = new ArrayList<>();
boolean cdromAdded = false;
List<DiskStateExpanded> disksToBeCustomized = null;
int scsiUnitIndex = 0;
for (DiskStateExpanded ds : diskStates) {
String diskPath = VimUtils.uriToDatastorePath(ds.sourceImageReference);
if (ds.type == DiskType.HDD) {
// Find if there is a storage policy defined for this disk
List<VirtualMachineDefinedProfileSpec> pbmSpec = getPbmProfileSpec(ds);
VirtualDeviceConfigSpec hdd;
if (diskPath != null) {
// create full clone of given disk
hdd = createFullCloneAndAttach(diskPath, ds, dir, scsiController, scsiUnits[scsiUnitIndex], pbmSpec);
newDisks.add(hdd);
// When it is through clone, customize after the clone is complete.
if (disksToBeCustomized == null) {
disksToBeCustomized = new ArrayList<>(diskStates.size());
}
if (isImageDisks) {
disksToBeCustomized.add(ds);
}
} else {
String dsDirForDisk = getDatastorePathForDisk(ds, dir);
String diskName = makePathToVmdkFile(ds.name, dsDirForDisk);
hdd = createHdd(scsiController.getKey(), scsiUnits[scsiUnitIndex], ds, diskName, getDataStoreForDisk(ds, pbmSpec), pbmSpec);
newDisks.add(hdd);
}
scsiUnitIndex++;
}
if (ds.type == DiskType.CDROM) {
VirtualDeviceConfigSpec cdrom = createCdrom(ideController, ideUnit);
fillInControllerUnitNumber(ds, ideUnit);
ideUnit = nextUnitNumber(ideUnit);
if (diskPath != null) {
// mount iso image
insertCdrom((VirtualCdrom) cdrom.getDevice(), diskPath);
}
newDisks.add(cdrom);
cdromAdded = true;
}
if (ds.type == DiskType.FLOPPY) {
VirtualDeviceConfigSpec floppy = createFloppy(sioController, sioUnit);
fillInControllerUnitNumber(ds, sioUnit);
sioUnit = nextUnitNumber(sioUnit);
if (diskPath != null) {
// mount iso image
insertFloppy((VirtualFloppy) floppy.getDevice(), diskPath);
}
newDisks.add(floppy);
}
// mark disk as attached
ds.status = DiskStatus.ATTACHED;
}
// add a cdrom so that ovf transport works
if (!cdromAdded && isImageDisks) {
VirtualDeviceConfigSpec cdrom = createCdrom(ideController, ideUnit);
newDisks.add(cdrom);
}
// add disks one at a time
for (VirtualDeviceConfigSpec newDisk : newDisks) {
VirtualMachineConfigSpec spec = new VirtualMachineConfigSpec();
spec.getDeviceChange().add(newDisk);
ManagedObjectReference reconfigureTask = getVimPort().reconfigVMTask(this.vm, spec);
TaskInfo info = waitTaskEnd(reconfigureTask);
if (info.getState() == TaskInfoState.ERROR) {
VimUtils.rethrow(info.getError());
}
}
// If disks are created through full clone, then reconfigure
if (disksToBeCustomized != null && !disksToBeCustomized.isEmpty()) {
// Get the hardware devices once again as they are reconfigured
devices = this.get.entityProp(this.vm, VimPath.vm_config_hardware_device);
reconfigureBootDisk(this.vm, getCustomizationConfigSpecs(devices, disksToBeCustomized));
}
}
Aggregations