use of com.vmware.vim25.VirtualMachineDefinedProfileSpec in project photon-model by vmware.
the class InstanceClient method createVm.
/**
* Creates a VM in vsphere. This method will block until the CreateVM_Task completes. The path
* to the .vmx file is explicitly set and its existence is iterpreted as if the VM has been
* successfully created and returns null.
*
* @return
* @throws FinderException
* @throws Exception
*/
private ManagedObjectReference createVm() throws Exception {
ManagedObjectReference folder = getVmFolder();
List<VirtualMachineDefinedProfileSpec> pbmSpec = getPbmProfileSpec(this.bootDisk);
ManagedObjectReference datastore = getDataStoreForDisk(this.bootDisk, pbmSpec);
ManagedObjectReference resourcePool = getResourcePool();
ManagedObjectReference host = getHost();
// datastore from that.
if (datastore == null) {
ManagedObjectReference dsFromSp = getDatastoreFromStoragePolicy(this.connection, pbmSpec);
datastore = dsFromSp == null ? getDatastore() : dsFromSp;
}
String datastoreName = this.get.entityProp(datastore, "name");
VirtualMachineConfigSpec spec = buildVirtualMachineConfigSpec(datastoreName);
String gt = CustomProperties.of(this.ctx.child).getString(CustomProperties.GUEST_ID, null);
if (gt != null) {
try {
gt = VirtualMachineGuestOsIdentifier.valueOf(gt).value();
} catch (IllegalArgumentException e) {
// silently default to generic 64 bit guest.
gt = DEFAULT_GUEST_ID.value();
}
spec.setGuestId(gt);
}
populateCloudConfig(spec, null);
recordTimestamp(spec.getExtraConfig());
// 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 vmTask = getVimPort().createVMTask(folder, spec, resourcePool, host);
TaskInfo info = waitTaskEnd(vmTask);
if (info.getState() == TaskInfoState.ERROR) {
MethodFault fault = info.getError().getFault();
if (fault instanceof FileAlreadyExists) {
// a .vmx file already exists, assume someone won the race to create the vm
return null;
} else {
return VimUtils.rethrow(info.getError());
}
}
return (ManagedObjectReference) info.getResult();
}
use of com.vmware.vim25.VirtualMachineDefinedProfileSpec in project photon-model by vmware.
the class ClientUtils method getPbmProfileSpec.
/**
* Construct storage policy profile spec for a profile Id
*/
public static List<VirtualMachineDefinedProfileSpec> getPbmProfileSpec(DiskService.DiskStateExpanded diskState) {
if (diskState == null || diskState.resourceGroupStates == null || diskState.resourceGroupStates.isEmpty()) {
return null;
}
List<VirtualMachineDefinedProfileSpec> profileSpecs = diskState.resourceGroupStates.stream().map(rg -> {
if (StringUtils.isNotEmpty(rg.id)) {
VirtualMachineDefinedProfileSpec spbmProfile = new VirtualMachineDefinedProfileSpec();
spbmProfile.setProfileId(rg.id);
return spbmProfile;
}
return null;
}).filter(Objects::nonNull).collect(Collectors.toList());
return profileSpecs;
}
use of com.vmware.vim25.VirtualMachineDefinedProfileSpec in project photon-model by vmware.
the class ClientUtils method createHdd.
/**
* Creates HDD virtual disk
*/
public static VirtualDeviceConfigSpec createHdd(Integer controllerKey, int unitNumber, DiskService.DiskStateExpanded ds, String diskName, ManagedObjectReference datastore, List<VirtualMachineDefinedProfileSpec> pbmSpec, boolean isCreateFile) throws FinderException, InvalidPropertyFaultMsg, RuntimeFaultFaultMsg {
VirtualDiskFlatVer2BackingInfo backing = new VirtualDiskFlatVer2BackingInfo();
backing.setDiskMode(getDiskMode(ds));
VirtualDiskType provisionType = getDiskProvisioningType(ds);
if (provisionType != null) {
backing.setThinProvisioned(provisionType == VirtualDiskType.THIN);
backing.setEagerlyScrub(provisionType == VirtualDiskType.EAGER_ZEROED_THICK);
}
backing.setFileName(diskName);
backing.setDatastore(datastore);
VirtualDisk disk = new VirtualDisk();
disk.setCapacityInKB(toKb(ds.capacityMBytes));
disk.setBacking(backing);
disk.setStorageIOAllocation(getStorageIOAllocationInfo(ds));
disk.setControllerKey(controllerKey);
disk.setUnitNumber(unitNumber);
fillInControllerUnitNumber(ds, unitNumber);
disk.setKey(-1);
VirtualDeviceConfigSpec change = new VirtualDeviceConfigSpec();
change.setDevice(disk);
if (pbmSpec != null) {
// Add storage policy spec
pbmSpec.stream().forEach(sp -> {
change.getProfile().add(sp);
});
}
change.setOperation(VirtualDeviceConfigSpecOperation.ADD);
if (isCreateFile) {
change.setFileOperation(VirtualDeviceConfigSpecFileOperation.CREATE);
}
return change;
}
use of com.vmware.vim25.VirtualMachineDefinedProfileSpec in project photon-model by vmware.
the class DiskClient method createVirtualDisk.
/**
* Create Virtual Disk
*/
public void createVirtualDisk() throws Exception {
ManagedObjectReference diskManager = this.connection.getServiceContent().getVirtualDiskManager();
List<VirtualMachineDefinedProfileSpec> pbmSpec = getPbmProfileSpec(this.diskState);
String dsName = this.diskContext.datastoreName != null && !this.diskContext.datastoreName.isEmpty() ? this.diskContext.datastoreName : ClientUtils.getDefaultDatastore(this.finder);
String diskName = getUniqueDiskName();
// Create the parent folder before creation of the disk file
String parentDir = String.format(VM_PATH_FORMAT, dsName, diskName);
createParentFolder(parentDir);
String diskFullPath = constructDiskFullPath(dsName, diskName);
ManagedObjectReference createTask = getVimPort().createVirtualDiskTask(diskManager, diskFullPath, this.diskContext.datacenterMoRef, createVirtualDiskSpec(this.diskState, pbmSpec));
TaskInfo info = waitTaskEnd(createTask);
if (info.getState() == TaskInfoState.ERROR) {
VimUtils.rethrow(info.getError());
}
// Update the details of the disk
CustomProperties.of(this.diskState).put(DISK_FULL_PATH, diskFullPath).put(DISK_PARENT_DIRECTORY, parentDir).put(DISK_DATASTORE_NAME, dsName);
this.diskState.status = DiskService.DiskStatus.AVAILABLE;
this.diskState.id = diskName;
this.diskState.regionId = VimUtils.convertMoRefToString(this.diskContext.datacenterMoRef);
AdapterUtils.addToEndpointLinks(this.diskState, this.diskState.endpointLink);
}
use of com.vmware.vim25.VirtualMachineDefinedProfileSpec in project photon-model by vmware.
the class DiskClient method createVirtualDiskSpec.
/**
* Create virtual disk spec for creating the virtual disk
*/
private FileBackedVirtualDiskSpec createVirtualDiskSpec(DiskStateExpanded diskStateExpanded, List<VirtualMachineDefinedProfileSpec> pbmSpec) {
if (diskStateExpanded.capacityMBytes <= 0) {
throw new IllegalArgumentException("Capacity of disk should be greater than 0");
}
if (diskStateExpanded.name == null || diskStateExpanded.name.isEmpty()) {
throw new IllegalArgumentException("Disk name should not be empty");
}
FileBackedVirtualDiskSpec diskSpec = new FileBackedVirtualDiskSpec();
diskSpec.setCapacityKb(toKb(diskStateExpanded.capacityMBytes));
VirtualDiskType provisionType = getDiskProvisioningType(diskStateExpanded);
if (provisionType != null) {
diskSpec.setDiskType(provisionType.value());
} else {
// Default it to THIN
diskSpec.setDiskType(VirtualDiskType.THIN.value());
}
diskSpec.setAdapterType(VirtualDiskAdapterType.LSI_LOGIC.value());
if (pbmSpec != null) {
diskSpec.getProfile().addAll(pbmSpec);
}
return diskSpec;
}
Aggregations