Search in sources :

Example 56 with VolumeObjectTO

use of com.cloud.storage.to.VolumeObjectTO in project cosmic by MissionCriticalCloud.

the class NotAValidCommand method testRevertToVMSnapshotCommand.

@Test
public void testRevertToVMSnapshotCommand() {
    final Connection conn = Mockito.mock(Connection.class);
    final VMSnapshotTO snapshotTO = Mockito.mock(VMSnapshotTO.class);
    final List<VolumeObjectTO> volumeTOs = new ArrayList<>();
    final RevertToVMSnapshotCommand vmSnapshot = new RevertToVMSnapshotCommand("Test", "uuid", snapshotTO, volumeTOs, "Debian");
    final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
    assertNotNull(wrapper);
    when(citrixResourceBase.getConnection()).thenReturn(conn);
    final Answer answer = wrapper.execute(vmSnapshot, citrixResourceBase);
    verify(citrixResourceBase, times(1)).getConnection();
    assertFalse(answer.getResult());
}
Also used : RebootAnswer(com.cloud.agent.api.RebootAnswer) CreateAnswer(com.cloud.agent.api.storage.CreateAnswer) Answer(com.cloud.agent.api.Answer) AttachAnswer(com.cloud.storage.command.AttachAnswer) VMSnapshotTO(com.cloud.agent.api.VMSnapshotTO) RevertToVMSnapshotCommand(com.cloud.agent.api.RevertToVMSnapshotCommand) Connection(com.xensource.xenapi.Connection) ArrayList(java.util.ArrayList) VolumeObjectTO(com.cloud.storage.to.VolumeObjectTO) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 57 with VolumeObjectTO

use of com.cloud.storage.to.VolumeObjectTO in project cosmic by MissionCriticalCloud.

the class NotAValidCommand method testCreateVMSnapshotCommand.

@Test
public void testCreateVMSnapshotCommand() {
    final Connection conn = Mockito.mock(Connection.class);
    final VMSnapshotTO snapshotTO = Mockito.mock(VMSnapshotTO.class);
    final List<VolumeObjectTO> volumeTOs = new ArrayList<>();
    final CreateVMSnapshotCommand vmSnapshot = new CreateVMSnapshotCommand("Test", "uuid", snapshotTO, volumeTOs, "Debian");
    final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
    assertNotNull(wrapper);
    when(citrixResourceBase.getConnection()).thenReturn(conn);
    final Answer answer = wrapper.execute(vmSnapshot, citrixResourceBase);
    verify(citrixResourceBase, times(1)).getConnection();
    assertTrue(answer.getResult());
}
Also used : RebootAnswer(com.cloud.agent.api.RebootAnswer) CreateAnswer(com.cloud.agent.api.storage.CreateAnswer) Answer(com.cloud.agent.api.Answer) AttachAnswer(com.cloud.storage.command.AttachAnswer) VMSnapshotTO(com.cloud.agent.api.VMSnapshotTO) Connection(com.xensource.xenapi.Connection) ArrayList(java.util.ArrayList) VolumeObjectTO(com.cloud.storage.to.VolumeObjectTO) CreateVMSnapshotCommand(com.cloud.agent.api.CreateVMSnapshotCommand) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 58 with VolumeObjectTO

use of com.cloud.storage.to.VolumeObjectTO in project cosmic by MissionCriticalCloud.

the class CitrixRevertToVMSnapshotCommandWrapper method execute.

@Override
public Answer execute(final RevertToVMSnapshotCommand command, final CitrixResourceBase citrixResourceBase) {
    final String vmName = command.getVmName();
    final List<VolumeObjectTO> listVolumeTo = command.getVolumeTOs();
    final VMSnapshot.Type vmSnapshotType = command.getTarget().getType();
    final Boolean snapshotMemory = vmSnapshotType == VMSnapshot.Type.DiskAndMemory;
    final Connection conn = citrixResourceBase.getConnection();
    PowerState vmState = null;
    VM vm = null;
    try {
        final Set<VM> vmSnapshots = VM.getByNameLabel(conn, command.getTarget().getSnapshotName());
        if (vmSnapshots == null || vmSnapshots.size() == 0) {
            return new RevertToVMSnapshotAnswer(command, false, "Cannot find vmSnapshot with name: " + command.getTarget().getSnapshotName());
        }
        final VM vmSnapshot = vmSnapshots.iterator().next();
        // find target VM or creating a work VM
        try {
            vm = citrixResourceBase.getVM(conn, vmName);
        } catch (final Exception e) {
            vm = citrixResourceBase.createWorkingVM(conn, vmName, command.getGuestOSType(), command.getPlatformEmulator(), listVolumeTo);
        }
        if (vm == null) {
            return new RevertToVMSnapshotAnswer(command, false, "Revert to VM Snapshot Failed due to can not find vm: " + vmName);
        }
        // call plugin to execute revert
        citrixResourceBase.revertToSnapshot(conn, vmSnapshot, vmName, vm.getUuid(conn), snapshotMemory, citrixResourceBase.getHost().getUuid());
        vm = citrixResourceBase.getVM(conn, vmName);
        final Set<VBD> vbds = vm.getVBDs(conn);
        final Map<String, VDI> vdiMap = new HashMap<>();
        // get vdi:vbdr to a map
        for (final VBD vbd : vbds) {
            final VBD.Record vbdr = vbd.getRecord(conn);
            if (vbdr.type == Types.VbdType.DISK) {
                final VDI vdi = vbdr.VDI;
                vdiMap.put(vbdr.userdevice, vdi);
            }
        }
        if (!snapshotMemory) {
            vm.destroy(conn);
            vmState = PowerState.PowerOff;
        } else {
            vmState = PowerState.PowerOn;
        }
        // after revert, VM's volumes path have been changed, need to report to manager
        for (final VolumeObjectTO volumeTo : listVolumeTo) {
            final Long deviceId = volumeTo.getDeviceId();
            final VDI vdi = vdiMap.get(deviceId.toString());
            volumeTo.setPath(vdi.getUuid(conn));
        }
        return new RevertToVMSnapshotAnswer(command, listVolumeTo, vmState);
    } catch (final Exception e) {
        s_logger.error("revert vm " + vmName + " to snapshot " + command.getTarget().getSnapshotName() + " failed due to " + e.getMessage());
        return new RevertToVMSnapshotAnswer(command, false, e.getMessage());
    }
}
Also used : RevertToVMSnapshotAnswer(com.cloud.agent.api.RevertToVMSnapshotAnswer) HashMap(java.util.HashMap) Connection(com.xensource.xenapi.Connection) VMSnapshot(com.cloud.vm.snapshot.VMSnapshot) VM(com.xensource.xenapi.VM) VolumeObjectTO(com.cloud.storage.to.VolumeObjectTO) VBD(com.xensource.xenapi.VBD) VDI(com.xensource.xenapi.VDI) PowerState(com.cloud.vm.VirtualMachine.PowerState)

Aggregations

VolumeObjectTO (com.cloud.storage.to.VolumeObjectTO)58 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)36 PrimaryDataStoreTO (com.cloud.storage.to.PrimaryDataStoreTO)22 CopyCmdAnswer (com.cloud.storage.command.CopyCmdAnswer)21 Connection (com.xensource.xenapi.Connection)19 InternalErrorException (com.cloud.exception.InternalErrorException)18 VDI (com.xensource.xenapi.VDI)17 NfsTO (com.cloud.agent.api.to.NfsTO)16 DataTO (com.cloud.agent.api.to.DataTO)15 DataStoreTO (com.cloud.agent.api.to.DataStoreTO)14 ArrayList (java.util.ArrayList)14 SR (com.xensource.xenapi.SR)13 XenAPIException (com.xensource.xenapi.Types.XenAPIException)13 XmlRpcException (org.apache.xmlrpc.XmlRpcException)13 VMSnapshotTO (com.cloud.agent.api.VMSnapshotTO)10 IOException (java.io.IOException)10 LibvirtException (org.libvirt.LibvirtException)10 VolumeVO (com.cloud.storage.VolumeVO)9 URI (java.net.URI)9 HashMap (java.util.HashMap)8