use of com.vmware.vim25.ArrayOfVirtualDevice in project photon-model by vmware.
the class ClientUtils method attachDiskToVM.
public static String attachDiskToVM(ArrayOfVirtualDevice devices, ManagedObjectReference vm, DiskService.DiskStateExpanded diskState, ManagedObjectReference diskDatastore, Connection connection, VimPortType vimPort) throws Exception {
String diskPath = VimUtils.uriToDatastorePath(diskState.sourceImageReference);
String diskFullPath = CustomProperties.of(diskState).getString(DISK_FULL_PATH, null);
Boolean insertCdRom = CustomProperties.of(diskState).getBoolean(INSERT_CDROM, false);
VirtualDeviceConfigSpec deviceConfigSpec = null;
if (diskState.type == DiskService.DiskType.HDD) {
VirtualSCSIController scsiController = getFirstScsiController(devices);
// Get available free unit numbers for the given scsi controller.
Integer[] scsiUnits = findFreeScsiUnit(scsiController, devices.getVirtualDevice());
List<VirtualMachineDefinedProfileSpec> pbmSpec = getPbmProfileSpec(diskState);
deviceConfigSpec = createHdd(scsiController.getKey(), scsiUnits[0], diskState, diskFullPath, diskDatastore, pbmSpec, false);
} else if (diskState.type == DiskService.DiskType.CDROM) {
if (insertCdRom) {
if (diskPath == null) {
throw new IllegalStateException(String.format("Cannot insert empty iso file into CD-ROM"));
}
// Find first available CD ROM to insert the iso file
VirtualCdrom cdrom = devices.getVirtualDevice().stream().filter(d -> d instanceof VirtualCdrom).map(d -> (VirtualCdrom) d).findFirst().orElse(null);
if (cdrom == null) {
throw new IllegalStateException(String.format("Could not find Virtual CD ROM to insert %s.", diskPath));
}
insertCdrom(cdrom, diskPath);
deviceConfigSpec = new VirtualDeviceConfigSpec();
deviceConfigSpec.setDevice(cdrom);
deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.EDIT);
} else {
VirtualDevice ideController = getFirstIdeController(devices);
int ideUnit = findFreeUnit(ideController, devices.getVirtualDevice());
int availableUnitNumber = nextUnitNumber(ideUnit);
deviceConfigSpec = createCdrom(ideController, availableUnitNumber);
fillInControllerUnitNumber(diskState, availableUnitNumber);
if (diskPath != null) {
// mount iso image
insertCdrom((VirtualCdrom) deviceConfigSpec.getDevice(), diskPath);
}
// Live add of cd-rom is not possible. Hence it needs to be powered off
// Power off is needed to ADD cd-rom
powerOffVm(connection, vimPort, vm);
}
} else if (diskState.type == DiskService.DiskType.FLOPPY) {
VirtualDevice sioController = getFirstSioController(devices);
int sioUnit = findFreeUnit(sioController, devices.getVirtualDevice());
int availableUnitNumber = nextUnitNumber(sioUnit);
deviceConfigSpec = createFloppy(sioController, availableUnitNumber);
fillInControllerUnitNumber(diskState, availableUnitNumber);
if (diskPath != null) {
insertFloppy((VirtualFloppy) deviceConfigSpec.getDevice(), diskPath);
}
// Power off is needed to ADD floppy
powerOffVm(connection, vimPort, vm);
}
VirtualMachineConfigSpec spec = new VirtualMachineConfigSpec();
spec.getDeviceChange().add(deviceConfigSpec);
ManagedObjectReference reconfigureTask = vimPort.reconfigVMTask(vm, spec);
TaskInfo info = VimUtils.waitTaskEnd(connection, reconfigureTask);
if (info.getState() == TaskInfoState.ERROR) {
VimUtils.rethrow(info.getError());
}
if (!insertCdRom && diskState.type != DiskService.DiskType.HDD) {
// This means it is CDROM or Floppy. Hence power on the VM as it is powered off to
// perform the operation
powerOnVM(connection, vimPort, vm);
}
return diskFullPath;
}
use of com.vmware.vim25.ArrayOfVirtualDevice in project photon-model by vmware.
the class InstanceDiskClient method detachDiskFromVM.
public void detachDiskFromVM() throws Exception {
ArrayOfVirtualDevice devices = this.get.entityProp(this.vm, VimPath.vm_config_hardware_device);
VirtualDisk vd = (VirtualDisk) findMatchingVirtualDevice(getListOfVirtualDisk(devices), this.diskState);
if (vd == null) {
throw new IllegalStateException(String.format("Matching Virtual Disk is not for disk %s.", this.diskState.documentSelfLink));
}
// Detach the disk from VM.
detachDisk(this.connection, vd, this.vm, getVimPort());
}
use of com.vmware.vim25.ArrayOfVirtualDevice in project photon-model by vmware.
the class InstanceDiskClient method attachDiskToVM.
public DiskService.DiskState attachDiskToVM() throws Exception {
ArrayOfVirtualDevice devices = this.get.entityProp(this.vm, VimPath.vm_config_hardware_device);
String diskDatastoreName = CustomProperties.of(this.diskState).getString(DISK_DATASTORE_NAME);
ManagedObjectReference diskDsMoref = null;
if (diskDatastoreName != null) {
diskDsMoref = this.finder.datastore(diskDatastoreName).object;
}
String diskFullPath = ClientUtils.attachDiskToVM(devices, this.vm, this.diskState, diskDsMoref, this.connection, this.getVimPort());
devices = this.get.entityProp(this.vm, VimPath.vm_config_hardware_device);
// Update the diskState
if (this.diskState.type == DiskService.DiskType.HDD) {
this.diskState.sourceImageReference = VimUtils.datastorePathToUri(diskFullPath);
VirtualDisk vd = (VirtualDisk) findMatchingVirtualDevice(getListOfVirtualDisk(devices), this.diskState);
if (vd != null) {
updateDiskStateFromVirtualDisk(vd, this.diskState);
}
} else if (this.diskState.type == DiskService.DiskType.CDROM) {
Boolean insertCdRom = CustomProperties.of(this.diskState).getBoolean(INSERT_CDROM, false);
VirtualDevice virtualDevice = findMatchingVirtualDevice(getListOfVirtualCdRom(devices), this.diskState);
if (virtualDevice != null) {
if (insertCdRom) {
DiskService.DiskStateExpanded matchedDs = findMatchingDiskState(virtualDevice, this.context.computeDiskStates);
if (matchedDs.customProperties == null) {
matchedDs.customProperties = new HashMap<>();
}
matchedDs.customProperties.putAll(this.diskState.customProperties);
updateDiskStateFromVirtualDevice(virtualDevice, matchedDs, virtualDevice.getBacking(), CustomProperties.of(this.diskState).getString(CustomProperties.DATACENTER_SELF_LINK));
CustomProperties.of(matchedDs).put(TEMPLATE_DISK_LINK, this.diskState.documentSelfLink);
return matchedDs;
} else {
updateDiskStateFromVirtualDevice(virtualDevice, this.diskState, virtualDevice.getBacking(), CustomProperties.of(this.diskState).getString(CustomProperties.DATACENTER_SELF_LINK));
}
}
} else {
// This would be Floppy
VirtualDevice virtualDevice = findMatchingVirtualDevice(getListOfVirtualFloppy(devices), this.diskState);
if (virtualDevice != null) {
updateDiskStateFromVirtualDevice(virtualDevice, this.diskState, virtualDevice.getBacking(), CustomProperties.of(this.diskState).getString(CustomProperties.DATACENTER_SELF_LINK));
}
}
return this.diskState;
}
use of com.vmware.vim25.ArrayOfVirtualDevice in project photon-model by vmware.
the class InstanceClient method customizeAfterClone.
private void customizeAfterClone() throws Exception {
VirtualMachineConfigSpec spec = new VirtualMachineConfigSpec();
// is takes precedence
if (this.ctx.child.description.cpuCount > 0) {
spec.setNumCPUs((int) this.ctx.child.description.cpuCount);
}
String gt = CustomProperties.of(this.ctx.child).getString(CustomProperties.GUEST_ID, null);
if (gt != null) {
spec.setGuestId(gt);
}
if (this.ctx.child.description.totalMemoryBytes > 0) {
spec.setMemoryMB(toMemoryMb(this.ctx.child.description.totalMemoryBytes));
}
recordTimestamp(spec.getExtraConfig());
// set ovf environment
ArrayOfVAppPropertyInfo infos = this.get.entityProp(this.vm, VimPath.vm_config_vAppConfig_property);
populateCloudConfig(spec, infos);
// remove nics and attach to proper networks if nics are configured
ArrayOfVirtualDevice devices = null;
if (this.ctx.nics != null && this.ctx.nics.size() > 0) {
devices = this.get.entityProp(this.vm, VimPath.vm_config_hardware_device);
devices.getVirtualDevice().stream().filter(d -> d instanceof VirtualEthernetCard).forEach(nic -> {
VirtualDeviceConfigSpec removeNicChange = new VirtualDeviceConfigSpec();
removeNicChange.setOperation(VirtualDeviceConfigSpecOperation.REMOVE);
removeNicChange.setDevice(nic);
spec.getDeviceChange().add(removeNicChange);
});
for (NetworkInterfaceStateWithDetails niState : this.ctx.nics) {
VirtualDevice nic = createNic(niState, null);
addDeviceToVm(spec, nic);
}
}
// Find whether it has HDD disk
if (this.imageDisks != null && !this.imageDisks.isEmpty()) {
// Iterate over each of the VirtualDisk and reconfigure if needed
if (devices == null) {
devices = this.get.entityProp(this.vm, VimPath.vm_config_hardware_device);
}
spec.getDeviceChange().addAll(getCustomizationConfigSpecs(devices, this.imageDisks));
}
// set the maximum snapshot limit if specified
final String snapshotLimit = CustomProperties.of(this.ctx.child).getString(CustomProperties.SNAPSHOT_MAXIMUM_LIMIT);
recordSnapshotLimit(spec.getExtraConfig(), snapshotLimit);
ManagedObjectReference task = getVimPort().reconfigVMTask(this.vm, spec);
TaskInfo info = waitTaskEnd(task);
if (info.getState() == TaskInfoState.ERROR) {
VimUtils.rethrow(info.getError());
}
// If there are any data disks then attach then to the VM
if (this.dataDisks != null && !this.dataDisks.isEmpty()) {
attachDisks(this.dataDisks, false);
}
// If there are any external disks, then attach then to the VM
attachExternalDisks();
}
use of com.vmware.vim25.ArrayOfVirtualDevice in project photon-model by vmware.
the class InstanceClient method getCustomizationConfigSpecs.
/**
* Get customization config spec for all the image disks if any
*/
private List<VirtualDeviceConfigSpec> getCustomizationConfigSpecs(ArrayOfVirtualDevice devices, List<DiskStateExpanded> diskStates) throws FinderException, InvalidPropertyFaultMsg, RuntimeFaultFaultMsg {
List<VirtualDeviceConfigSpec> specs = new ArrayList<>();
List<VirtualDisk> virtualDisks = devices.getVirtualDevice().stream().filter(d -> d instanceof VirtualDisk).map(d -> (VirtualDisk) d).collect(Collectors.toList());
for (VirtualDisk vd : virtualDisks) {
VirtualDeviceConfigSpec diskSpec = getBootDiskCustomizeConfigSpec(findMatchingImageDiskState(vd, diskStates), vd);
if (diskSpec != null) {
specs.add(diskSpec);
}
}
return specs;
}
Aggregations