Search in sources :

Example 11 with VirtualMachineDefinedProfileSpec

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();
}
Also used : TaskInfo(com.vmware.vim25.TaskInfo) MethodFault(com.vmware.vim25.MethodFault) VirtualMachineConfigSpec(com.vmware.vim25.VirtualMachineConfigSpec) FileAlreadyExists(com.vmware.vim25.FileAlreadyExists) VirtualMachineDefinedProfileSpec(com.vmware.vim25.VirtualMachineDefinedProfileSpec) ArrayOfManagedObjectReference(com.vmware.vim25.ArrayOfManagedObjectReference) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 12 with VirtualMachineDefinedProfileSpec

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;
}
Also used : VirtualMachineDefinedProfileSpec(com.vmware.vim25.VirtualMachineDefinedProfileSpec)

Example 13 with VirtualMachineDefinedProfileSpec

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;
}
Also used : VirtualDiskFlatVer2BackingInfo(com.vmware.vim25.VirtualDiskFlatVer2BackingInfo) VirtualDeviceConfigSpec(com.vmware.vim25.VirtualDeviceConfigSpec) VirtualDiskType(com.vmware.vim25.VirtualDiskType) VirtualDisk(com.vmware.vim25.VirtualDisk)

Example 14 with VirtualMachineDefinedProfileSpec

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);
}
Also used : TaskInfo(com.vmware.vim25.TaskInfo) VirtualMachineDefinedProfileSpec(com.vmware.vim25.VirtualMachineDefinedProfileSpec) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 15 with VirtualMachineDefinedProfileSpec

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;
}
Also used : VirtualDiskType(com.vmware.vim25.VirtualDiskType) FileBackedVirtualDiskSpec(com.vmware.vim25.FileBackedVirtualDiskSpec)

Aggregations

ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)12 VirtualMachineDefinedProfileSpec (com.vmware.vim25.VirtualMachineDefinedProfileSpec)10 ArrayOfManagedObjectReference (com.vmware.vim25.ArrayOfManagedObjectReference)9 VirtualDiskType (com.vmware.vim25.VirtualDiskType)8 FinderException (com.vmware.photon.controller.model.adapters.vsphere.util.finders.FinderException)7 TaskInfo (com.vmware.vim25.TaskInfo)7 VirtualDiskFlatVer2BackingInfo (com.vmware.vim25.VirtualDiskFlatVer2BackingInfo)7 VirtualDeviceConfigSpec (com.vmware.vim25.VirtualDeviceConfigSpec)6 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)5 ArrayOfVirtualDevice (com.vmware.vim25.ArrayOfVirtualDevice)5 MethodFault (com.vmware.vim25.MethodFault)5 VirtualDisk (com.vmware.vim25.VirtualDisk)5 VirtualMachineConfigSpec (com.vmware.vim25.VirtualMachineConfigSpec)5 Lock (java.util.concurrent.locks.Lock)5 ReentrantLock (java.util.concurrent.locks.ReentrantLock)5 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)4 ClientUtils.findMatchingVirtualDevice (com.vmware.photon.controller.model.adapters.vsphere.ClientUtils.findMatchingVirtualDevice)4 ClientUtils.getListOfVirtualDisk (com.vmware.photon.controller.model.adapters.vsphere.ClientUtils.getListOfVirtualDisk)4 OvfDeployer (com.vmware.photon.controller.model.adapters.vsphere.ovf.OvfDeployer)4 OvfParser (com.vmware.photon.controller.model.adapters.vsphere.ovf.OvfParser)4