use of com.cloud.vm.snapshot.VMSnapshot in project cloudstack by apache.
the class LibvirtRevertToVMSnapshotCommandWrapper method execute.
@Override
public Answer execute(final RevertToVMSnapshotCommand cmd, final LibvirtComputingResource libvirtComputingResource) {
String vmName = cmd.getVmName();
List<VolumeObjectTO> listVolumeTo = cmd.getVolumeTOs();
VMSnapshot.Type vmSnapshotType = cmd.getTarget().getType();
Boolean snapshotMemory = vmSnapshotType == VMSnapshot.Type.DiskAndMemory;
VirtualMachine.PowerState vmState = null;
Domain dm = null;
try {
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
Connect conn = libvirtUtilitiesHelper.getConnection();
dm = libvirtComputingResource.getDomain(conn, vmName);
if (dm == null) {
return new RevertToVMSnapshotAnswer(cmd, false, "Revert to VM Snapshot Failed due to can not find vm: " + vmName);
}
DomainSnapshot snapshot = dm.snapshotLookupByName(cmd.getTarget().getSnapshotName());
if (snapshot == null)
return new RevertToVMSnapshotAnswer(cmd, false, "Cannot find vmSnapshot with name: " + cmd.getTarget().getSnapshotName());
dm.revertToSnapshot(snapshot);
snapshot.free();
if (!snapshotMemory) {
dm.destroy();
if (dm.isPersistent() == 1)
dm.undefine();
vmState = VirtualMachine.PowerState.PowerOff;
} else {
vmState = VirtualMachine.PowerState.PowerOn;
}
return new RevertToVMSnapshotAnswer(cmd, listVolumeTo, vmState);
} catch (LibvirtException e) {
String msg = " Revert to VM snapshot failed due to " + e.toString();
s_logger.warn(msg, e);
return new RevertToVMSnapshotAnswer(cmd, false, msg);
} finally {
if (dm != null) {
try {
dm.free();
} catch (LibvirtException l) {
s_logger.trace("Ignoring libvirt error.", l);
}
;
}
}
}
use of com.cloud.vm.snapshot.VMSnapshot in project cosmic by MissionCriticalCloud.
the class CreateSnapshotFromVMSnapshotCmd method getHostId.
private Long getHostId() {
final VMSnapshot vmsnapshot = _entityMgr.findById(VMSnapshot.class, getVMSnapshotId());
if (vmsnapshot == null) {
throw new InvalidParameterValueException("Unable to find vm snapshot by id=" + getVMSnapshotId());
}
final UserVm vm = _entityMgr.findById(UserVm.class, vmsnapshot.getVmId());
if (vm != null) {
if (vm.getHostId() != null) {
return vm.getHostId();
} else if (vm.getLastHostId() != null) {
return vm.getLastHostId();
}
}
return null;
}
use of com.cloud.vm.snapshot.VMSnapshot in project cosmic by MissionCriticalCloud.
the class CreateSnapshotFromVMSnapshotCmd method getVmId.
private Long getVmId() {
final VMSnapshot vmsnapshot = _entityMgr.findById(VMSnapshot.class, getVMSnapshotId());
if (vmsnapshot == null) {
throw new InvalidParameterValueException("Unable to find vm snapshot by id=" + getVMSnapshotId());
}
final UserVm vm = _entityMgr.findById(UserVm.class, vmsnapshot.getVmId());
if (vm == null) {
throw new InvalidParameterValueException("Unable to find vm by vm snapshot id=" + getVMSnapshotId());
}
return vm.getId();
}
use of com.cloud.vm.snapshot.VMSnapshot in project cosmic by MissionCriticalCloud.
the class CreateSnapshotFromVMSnapshotCmd method getEntityOwnerId.
@Override
public long getEntityOwnerId() {
final VMSnapshot vmsnapshot = _entityMgr.findById(VMSnapshot.class, getVMSnapshotId());
if (vmsnapshot == null) {
throw new InvalidParameterValueException("Unable to find vmsnapshot by id=" + getVMSnapshotId());
}
final Account account = _accountService.getAccount(vmsnapshot.getAccountId());
// Can create templates for enabled projects/accounts only
if (account.getType() == Account.ACCOUNT_TYPE_PROJECT) {
final Project project = _projectService.findByProjectAccountId(vmsnapshot.getAccountId());
if (project == null) {
throw new InvalidParameterValueException("Unable to find project by account id=" + account.getUuid());
}
if (project.getState() != Project.State.Active) {
throw new PermissionDeniedException("Can't add resources to the project id=" + project.getUuid() + " in state=" + project.getState() + " as it's no longer active");
}
} else if (account.getState() == Account.State.disabled) {
throw new PermissionDeniedException("The owner of template is disabled: " + account);
}
return vmsnapshot.getAccountId();
}
use of com.cloud.vm.snapshot.VMSnapshot in project cosmic by MissionCriticalCloud.
the class ListVMSnapshotCmd method execute.
@Override
public void execute() {
final List<? extends VMSnapshot> result = _vmSnapshotService.listVMSnapshots(this);
final ListResponse<VMSnapshotResponse> response = new ListResponse<>();
final List<VMSnapshotResponse> snapshotResponses = new ArrayList<>();
for (final VMSnapshot r : result) {
final VMSnapshotResponse vmSnapshotResponse = _responseGenerator.createVMSnapshotResponse(r);
vmSnapshotResponse.setObjectName("vmSnapshot");
snapshotResponses.add(vmSnapshotResponse);
}
response.setResponses(snapshotResponses);
response.setResponseName(getCommandName());
setResponseObject(response);
}
Aggregations