Search in sources :

Example 1 with DuplicateName

use of com.vmware.vim25.DuplicateName in project photon-model by vmware.

the class VimUtilsTest method convertToFault.

@Test
public void convertToFault() {
    DuplicateName fault = new DuplicateName();
    String msg = "msg";
    Exception e = new DuplicateNameFaultMsg(msg, fault);
    LocalizedMethodFault lmf = VimUtils.convertExceptionToFault(e);
    assertSame(fault, lmf.getFault());
    assertSame(msg, lmf.getLocalizedMessage());
}
Also used : LocalizedMethodFault(com.vmware.vim25.LocalizedMethodFault) DuplicateName(com.vmware.vim25.DuplicateName) DuplicateNameFaultMsg(com.vmware.vim25.DuplicateNameFaultMsg) IOException(java.io.IOException) Test(org.junit.Test)

Example 2 with DuplicateName

use of com.vmware.vim25.DuplicateName in project photon-model by vmware.

the class VimUtilsTest method retrhowKnownException.

@Test
public void retrhowKnownException() {
    DuplicateName dn = new DuplicateName();
    LocalizedMethodFault lmf = new LocalizedMethodFault();
    lmf.setLocalizedMessage("msg");
    lmf.setFault(dn);
    try {
        VimUtils.rethrow(lmf);
        fail();
    } catch (DuplicateNameFaultMsg msg) {
        assertSame(dn, msg.getFaultInfo());
        assertSame(lmf.getLocalizedMessage(), msg.getMessage());
    } catch (Exception e) {
        fail();
    }
}
Also used : LocalizedMethodFault(com.vmware.vim25.LocalizedMethodFault) DuplicateName(com.vmware.vim25.DuplicateName) DuplicateNameFaultMsg(com.vmware.vim25.DuplicateNameFaultMsg) IOException(java.io.IOException) Test(org.junit.Test)

Example 3 with DuplicateName

use of com.vmware.vim25.DuplicateName in project photon-model by vmware.

the class InstanceClient method replicateVMTemplate.

private ManagedObjectReference replicateVMTemplate(ManagedObjectReference resourcePool, ManagedObjectReference datastore, List<VirtualMachineDefinedProfileSpec> pbmSpec, ManagedObjectReference vmFolder, String vmName, ManagedObjectReference vm, GetMoRef get) throws Exception {
    logger.info("Template lives on a different datastore, looking for a local copy of: {}.", vmName);
    String replicatedName = vmName;
    if (datastore != null) {
        replicatedName = replicatedName + "_" + datastore.getValue();
    }
    ManagedObjectReference repVm = findTemplateByName(replicatedName, get);
    if (repVm != null) {
        return repVm;
    }
    logger.info("Replicating {} ({}) to {}", vmName, vm.getValue(), replicatedName);
    Lock lock = getLock(replicatedName);
    lock.lock();
    try {
        VirtualMachineRelocateSpec spec = new VirtualMachineRelocateSpec();
        spec.setPool(resourcePool);
        if (datastore != null) {
            spec.setDatastore(datastore);
        }
        if (pbmSpec != null) {
            pbmSpec.stream().forEach(sp -> {
                spec.getProfile().add(sp);
            });
        }
        spec.setFolder(vmFolder);
        spec.setDiskMoveType(VirtualMachineRelocateDiskMoveOptions.MOVE_ALL_DISK_BACKINGS_AND_DISALLOW_SHARING.value());
        VirtualMachineCloneSpec cloneSpec = new VirtualMachineCloneSpec();
        cloneSpec.setLocation(spec);
        cloneSpec.setTemplate(false);
        cloneSpec.setPowerOn(false);
        ManagedObjectReference cloneTask = getVimPort().cloneVMTask(vm, vmFolder, replicatedName, cloneSpec);
        TaskInfo info = VimUtils.waitTaskEnd(this.connection, cloneTask);
        if (info.getState() == TaskInfoState.ERROR) {
            MethodFault fault = info.getError().getFault();
            if (fault instanceof DuplicateName) {
                logger.info("Template is being replicated by another thread, waiting for {} to be ready", replicatedName);
                return awaitVM(replicatedName, vmFolder, get);
            } else {
                return VimUtils.rethrow(info.getError());
            }
        }
        ManagedObjectReference rvm = (ManagedObjectReference) info.getResult();
        logger.info("Replicated {} ({}) to {} ({})", vmName, vm.getValue(), replicatedName, rvm.getValue());
        logger.info("Creating initial snapshot for linked clones on {}", rvm.getValue());
        ManagedObjectReference snapshotTask = getVimPort().createSnapshotTask(rvm, "initial", null, false, false);
        VimUtils.waitTaskEnd(this.connection, snapshotTask);
        logger.info("Created initial snapshot for linked clones on {}", rvm.getValue());
        return rvm;
    } finally {
        lock.unlock();
    }
}
Also used : TaskInfo(com.vmware.vim25.TaskInfo) MethodFault(com.vmware.vim25.MethodFault) VirtualMachineRelocateSpec(com.vmware.vim25.VirtualMachineRelocateSpec) VirtualMachineCloneSpec(com.vmware.vim25.VirtualMachineCloneSpec) DuplicateName(com.vmware.vim25.DuplicateName) ArrayOfManagedObjectReference(com.vmware.vim25.ArrayOfManagedObjectReference) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) Lock(java.util.concurrent.locks.Lock) ReentrantLock(java.util.concurrent.locks.ReentrantLock)

Aggregations

DuplicateName (com.vmware.vim25.DuplicateName)3 DuplicateNameFaultMsg (com.vmware.vim25.DuplicateNameFaultMsg)2 LocalizedMethodFault (com.vmware.vim25.LocalizedMethodFault)2 IOException (java.io.IOException)2 Test (org.junit.Test)2 ArrayOfManagedObjectReference (com.vmware.vim25.ArrayOfManagedObjectReference)1 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)1 MethodFault (com.vmware.vim25.MethodFault)1 TaskInfo (com.vmware.vim25.TaskInfo)1 VirtualMachineCloneSpec (com.vmware.vim25.VirtualMachineCloneSpec)1 VirtualMachineRelocateSpec (com.vmware.vim25.VirtualMachineRelocateSpec)1 Lock (java.util.concurrent.locks.Lock)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1