Search in sources :

Example 21 with VBD

use of com.xensource.xenapi.VBD in project cloudstack by apache.

the class CitrixDeleteVMSnapshotCommandWrapper method execute.

@Override
public Answer execute(final DeleteVMSnapshotCommand command, final CitrixResourceBase citrixResourceBase) {
    final String snapshotName = command.getTarget().getSnapshotName();
    final Connection conn = citrixResourceBase.getConnection();
    try {
        final List<VDI> vdiList = new ArrayList<VDI>();
        final Set<VM> snapshots = VM.getByNameLabel(conn, snapshotName);
        if (snapshots == null || snapshots.size() == 0) {
            s_logger.warn("VM snapshot with name " + snapshotName + " does not exist, assume it is already deleted");
            return new DeleteVMSnapshotAnswer(command, command.getVolumeTOs());
        }
        final VM snapshot = snapshots.iterator().next();
        final Set<VBD> vbds = snapshot.getVBDs(conn);
        for (final VBD vbd : vbds) {
            if (vbd.getType(conn) == Types.VbdType.DISK) {
                final VDI vdi = vbd.getVDI(conn);
                vdiList.add(vdi);
            }
        }
        if (command.getTarget().getType() == VMSnapshot.Type.DiskAndMemory) {
            vdiList.add(snapshot.getSuspendVDI(conn));
        }
        snapshot.destroy(conn);
        for (final VDI vdi : vdiList) {
            vdi.destroy(conn);
        }
        try {
            Thread.sleep(5000);
        } catch (final InterruptedException ex) {
        }
        // re-calculate used capacify for this VM snapshot
        for (final VolumeObjectTO volumeTo : command.getVolumeTOs()) {
            final long size = citrixResourceBase.getVMSnapshotChainSize(conn, volumeTo, command.getVmName());
            volumeTo.setSize(size);
        }
        return new DeleteVMSnapshotAnswer(command, command.getVolumeTOs());
    } catch (final Exception e) {
        s_logger.warn("Catch Exception: " + e.getClass().toString() + " due to " + e.toString(), e);
        return new DeleteVMSnapshotAnswer(command, false, e.getMessage());
    }
}
Also used : Connection(com.xensource.xenapi.Connection) ArrayList(java.util.ArrayList) VM(com.xensource.xenapi.VM) VBD(com.xensource.xenapi.VBD) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) VDI(com.xensource.xenapi.VDI) DeleteVMSnapshotAnswer(com.cloud.agent.api.DeleteVMSnapshotAnswer)

Aggregations

VBD (com.xensource.xenapi.VBD)21 VDI (com.xensource.xenapi.VDI)17 VM (com.xensource.xenapi.VM)15 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)12 Connection (com.xensource.xenapi.Connection)12 XenAPIException (com.xensource.xenapi.Types.XenAPIException)12 XmlRpcException (org.apache.xmlrpc.XmlRpcException)12 InternalErrorException (com.cloud.exception.InternalErrorException)8 SR (com.xensource.xenapi.SR)7 DiskTO (com.cloud.agent.api.to.DiskTO)5 Types (com.xensource.xenapi.Types)5 DataTO (com.cloud.agent.api.to.DataTO)4 IOException (java.io.IOException)4 MalformedURLException (java.net.MalformedURLException)4 URISyntaxException (java.net.URISyntaxException)4 TimeoutException (java.util.concurrent.TimeoutException)4 ConfigurationException (javax.naming.ConfigurationException)4 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)4 VolumeObjectTO (org.apache.cloudstack.storage.to.VolumeObjectTO)4 Answer (com.cloud.agent.api.Answer)3