use of com.cloud.vm.snapshot.VMSnapshotVO in project cosmic by MissionCriticalCloud.
the class VMSnapshotStrategyTest method testDeleteVMSnapshot.
@Test
public void testDeleteVMSnapshot() throws AgentUnavailableException, OperationTimedoutException {
final Long hostId = 1L;
final Long vmId = 1L;
final Long guestOsId = 1L;
final HypervisorType hypervisorType = HypervisorType.Any;
final String hypervisorVersion = "default";
final String guestOsName = "Other";
final List<VolumeObjectTO> volumeObjectTOs = new ArrayList<>();
final VMSnapshotVO vmSnapshot = Mockito.mock(VMSnapshotVO.class);
final UserVmVO userVmVO = Mockito.mock(UserVmVO.class);
Mockito.when(userVmVO.getGuestOSId()).thenReturn(guestOsId);
Mockito.when(vmSnapshot.getVmId()).thenReturn(vmId);
Mockito.when(vmSnapshotHelper.pickRunningHost(Matchers.anyLong())).thenReturn(hostId);
Mockito.when(vmSnapshotHelper.getVolumeTOList(Matchers.anyLong())).thenReturn(volumeObjectTOs);
Mockito.when(userVmDao.findById(Matchers.anyLong())).thenReturn(userVmVO);
final GuestOSVO guestOSVO = Mockito.mock(GuestOSVO.class);
Mockito.when(guestOSDao.findById(Matchers.anyLong())).thenReturn(guestOSVO);
final GuestOSHypervisorVO guestOSHypervisorVO = Mockito.mock(GuestOSHypervisorVO.class);
Mockito.when(guestOSHypervisorVO.getGuestOsName()).thenReturn(guestOsName);
Mockito.when(guestOsHypervisorDao.findById(Matchers.anyLong())).thenReturn(guestOSHypervisorVO);
Mockito.when(guestOsHypervisorDao.findByOsIdAndHypervisor(Matchers.anyLong(), Matchers.anyString(), Matchers.anyString())).thenReturn(guestOSHypervisorVO);
final VMSnapshotTO vmSnapshotTO = Mockito.mock(VMSnapshotTO.class);
Mockito.when(vmSnapshotHelper.getSnapshotWithParents(Matchers.any(VMSnapshotVO.class))).thenReturn(vmSnapshotTO);
Mockito.when(vmSnapshotDao.findById(Matchers.anyLong())).thenReturn(vmSnapshot);
Mockito.when(vmSnapshot.getId()).thenReturn(1L);
Mockito.when(vmSnapshot.getCreated()).thenReturn(new Date());
Mockito.when(agentMgr.send(Matchers.anyLong(), Matchers.any(Command.class))).thenReturn(null);
final HostVO hostVO = Mockito.mock(HostVO.class);
Mockito.when(hostDao.findById(Matchers.anyLong())).thenReturn(hostVO);
Mockito.when(hostVO.getHypervisorType()).thenReturn(hypervisorType);
Mockito.when(hostVO.getHypervisorVersion()).thenReturn(hypervisorVersion);
Exception e = null;
try {
vmSnapshotStrategy.deleteVMSnapshot(vmSnapshot);
} catch (final CloudRuntimeException e1) {
e = e1;
}
assertNotNull(e);
final DeleteVMSnapshotAnswer answer = Mockito.mock(DeleteVMSnapshotAnswer.class);
Mockito.when(answer.getResult()).thenReturn(true);
Mockito.when(agentMgr.send(Matchers.anyLong(), Matchers.any(Command.class))).thenReturn(answer);
final boolean result = vmSnapshotStrategy.deleteVMSnapshot(vmSnapshot);
assertTrue(result);
}
use of com.cloud.vm.snapshot.VMSnapshotVO in project cloudstack by apache.
the class ApiResponseHelper method createUsageResponse.
@Override
public UsageRecordResponse createUsageResponse(Usage usageRecord, Map<String, Set<ResourceTagResponse>> resourceTagResponseMap, boolean oldFormat) {
UsageRecordResponse usageRecResponse = new UsageRecordResponse();
Account account = ApiDBUtils.findAccountById(usageRecord.getAccountId());
if (account.getType() == Account.ACCOUNT_TYPE_PROJECT) {
// find the project
Project project = ApiDBUtils.findProjectByProjectAccountIdIncludingRemoved(account.getId());
if (project != null) {
usageRecResponse.setProjectId(project.getUuid());
usageRecResponse.setProjectName(project.getName());
}
} else {
usageRecResponse.setAccountId(account.getUuid());
usageRecResponse.setAccountName(account.getAccountName());
}
Domain domain = ApiDBUtils.findDomainById(usageRecord.getDomainId());
if (domain != null) {
usageRecResponse.setDomainId(domain.getUuid());
usageRecResponse.setDomainName(domain.getName());
}
if (usageRecord.getZoneId() != null) {
DataCenter zone = ApiDBUtils.findZoneById(usageRecord.getZoneId());
if (zone != null) {
usageRecResponse.setZoneId(zone.getUuid());
}
}
usageRecResponse.setDescription(usageRecord.getDescription());
usageRecResponse.setUsage(usageRecord.getUsageDisplay());
usageRecResponse.setUsageType(usageRecord.getUsageType());
VMInstanceVO vmInstance = null;
if (usageRecord.getVmInstanceId() != null) {
vmInstance = _entityMgr.findByIdIncludingRemoved(VMInstanceVO.class, usageRecord.getVmInstanceId());
if (vmInstance != null) {
usageRecResponse.setVirtualMachineId(vmInstance.getUuid());
}
}
usageRecResponse.setResourceName(usageRecord.getVmName());
VMTemplateVO template = null;
if (usageRecord.getTemplateId() != null) {
template = ApiDBUtils.findTemplateById(usageRecord.getTemplateId());
if (template != null) {
usageRecResponse.setTemplateId(template.getUuid());
}
}
ResourceTag.ResourceObjectType resourceType = null;
Long resourceId = null;
if (usageRecord.getUsageType() == UsageTypes.RUNNING_VM || usageRecord.getUsageType() == UsageTypes.ALLOCATED_VM) {
ServiceOfferingVO svcOffering = _entityMgr.findByIdIncludingRemoved(ServiceOfferingVO.class, usageRecord.getOfferingId().toString());
// Service Offering Id
if (svcOffering != null) {
usageRecResponse.setOfferingId(svcOffering.getUuid());
}
// VM Instance ID
VMInstanceVO vm = null;
if (usageRecord.getUsageId() != null && usageRecord.getUsageId().equals(usageRecord.getVmInstanceId())) {
vm = vmInstance;
} else {
vm = _entityMgr.findByIdIncludingRemoved(VMInstanceVO.class, usageRecord.getUsageId().toString());
}
if (vm != null) {
resourceType = ResourceTag.ResourceObjectType.UserVm;
usageRecResponse.setUsageId(vm.getUuid());
resourceId = vm.getId();
final GuestOS guestOS = _guestOsDao.findById(vm.getGuestOSId());
if (guestOS != null) {
usageRecResponse.setOsTypeId(guestOS.getUuid());
usageRecResponse.setOsDisplayName(guestOS.getDisplayName());
final GuestOsCategory guestOsCategory = _guestOsCategoryDao.findById(guestOS.getCategoryId());
if (guestOsCategory != null) {
usageRecResponse.setOsCategoryId(guestOsCategory.getUuid());
usageRecResponse.setOsCategoryName(guestOsCategory.getName());
}
}
}
// Hypervisor Type
usageRecResponse.setType(usageRecord.getType());
// Dynamic compute offerings details
if (usageRecord.getCpuCores() != null) {
usageRecResponse.setCpuNumber(usageRecord.getCpuCores());
} else if (svcOffering.getCpu() != null) {
usageRecResponse.setCpuNumber(svcOffering.getCpu().longValue());
}
if (usageRecord.getCpuSpeed() != null) {
usageRecResponse.setCpuSpeed(usageRecord.getCpuSpeed());
} else if (svcOffering.getSpeed() != null) {
usageRecResponse.setCpuSpeed(svcOffering.getSpeed().longValue());
}
if (usageRecord.getMemory() != null) {
usageRecResponse.setMemory(usageRecord.getMemory());
} else if (svcOffering.getRamSize() != null) {
usageRecResponse.setMemory(svcOffering.getRamSize().longValue());
}
if (!oldFormat) {
final StringBuilder builder = new StringBuilder();
if (usageRecord.getUsageType() == UsageTypes.RUNNING_VM) {
builder.append("Running VM usage ");
} else if (usageRecord.getUsageType() == UsageTypes.ALLOCATED_VM) {
builder.append("Allocated VM usage ");
}
if (vm != null) {
builder.append("for ").append(vm.getHostName()).append(" (").append(vm.getInstanceName()).append(") (").append(vm.getUuid()).append(") ");
}
if (svcOffering != null) {
builder.append("using service offering ").append(svcOffering.getName()).append(" (").append(svcOffering.getUuid()).append(") ");
}
if (template != null) {
builder.append("and template ").append(template.getName()).append(" (").append(template.getUuid()).append(")");
}
usageRecResponse.setDescription(builder.toString());
}
} else if (usageRecord.getUsageType() == UsageTypes.IP_ADDRESS) {
// IP Address ID
IPAddressVO ip = _entityMgr.findByIdIncludingRemoved(IPAddressVO.class, usageRecord.getUsageId().toString());
if (ip != null) {
Long networkId = ip.getAssociatedWithNetworkId();
if (networkId == null) {
networkId = ip.getSourceNetworkId();
}
resourceType = ResourceObjectType.PublicIpAddress;
resourceId = ip.getId();
usageRecResponse.setUsageId(ip.getUuid());
}
// isSourceNAT
usageRecResponse.setSourceNat((usageRecord.getType().equals("SourceNat")) ? true : false);
// isSystem
usageRecResponse.setSystem((usageRecord.getSize() == 1) ? true : false);
} else if (usageRecord.getUsageType() == UsageTypes.NETWORK_BYTES_SENT || usageRecord.getUsageType() == UsageTypes.NETWORK_BYTES_RECEIVED) {
// Device Type
resourceType = ResourceObjectType.UserVm;
usageRecResponse.setType(usageRecord.getType());
VMInstanceVO vm = null;
HostVO host = null;
if (usageRecord.getType().equals("DomainRouter") || usageRecord.getType().equals("UserVm")) {
// Domain Router Id
vm = _entityMgr.findByIdIncludingRemoved(VMInstanceVO.class, usageRecord.getUsageId().toString());
if (vm != null) {
resourceId = vm.getId();
usageRecResponse.setUsageId(vm.getUuid());
}
} else {
// External Device Host Id
host = _entityMgr.findByIdIncludingRemoved(HostVO.class, usageRecord.getUsageId().toString());
if (host != null) {
usageRecResponse.setUsageId(host.getUuid());
}
}
// Network ID
NetworkVO network = null;
if ((usageRecord.getNetworkId() != null) && (usageRecord.getNetworkId() != 0)) {
network = _entityMgr.findByIdIncludingRemoved(NetworkVO.class, usageRecord.getNetworkId().toString());
if (network != null) {
resourceType = ResourceObjectType.Network;
if (network.getTrafficType() == TrafficType.Public) {
VirtualRouter router = ApiDBUtils.findDomainRouterById(usageRecord.getUsageId());
Vpc vpc = ApiDBUtils.findVpcByIdIncludingRemoved(router.getVpcId());
usageRecResponse.setVpcId(vpc.getUuid());
resourceId = vpc.getId();
} else {
usageRecResponse.setNetworkId(network.getUuid());
resourceId = network.getId();
}
usageRecResponse.setResourceName(network.getName());
}
}
if (!oldFormat) {
final StringBuilder builder = new StringBuilder();
if (usageRecord.getUsageType() == UsageTypes.NETWORK_BYTES_SENT) {
builder.append("Bytes sent by network ");
} else if (usageRecord.getUsageType() == UsageTypes.NETWORK_BYTES_RECEIVED) {
builder.append("Bytes received by network ");
}
if (network != null) {
if (network.getName() != null) {
builder.append(network.getName());
}
if (network.getUuid() != null) {
builder.append(" (").append(network.getUuid()).append(") ");
}
builder.append(" " + toHumanReadableSize(usageRecord.getRawUsage().longValue()) + " ");
}
if (vm != null) {
builder.append("using router ").append(vm.getInstanceName()).append(" (").append(vm.getUuid()).append(")");
} else if (host != null) {
builder.append("using host ").append(host.getName()).append(" (").append(host.getUuid()).append(")");
}
usageRecResponse.setDescription(builder.toString());
}
} else if (usageRecord.getUsageType() == UsageTypes.VM_DISK_IO_READ || usageRecord.getUsageType() == UsageTypes.VM_DISK_IO_WRITE || usageRecord.getUsageType() == UsageTypes.VM_DISK_BYTES_READ || usageRecord.getUsageType() == UsageTypes.VM_DISK_BYTES_WRITE) {
// Device Type
usageRecResponse.setType(usageRecord.getType());
resourceType = ResourceObjectType.Volume;
// Volume ID
VolumeVO volume = _entityMgr.findByIdIncludingRemoved(VolumeVO.class, usageRecord.getUsageId().toString());
if (volume != null) {
usageRecResponse.setUsageId(volume.getUuid());
resourceId = volume.getId();
}
if (!oldFormat) {
final StringBuilder builder = new StringBuilder();
if (usageRecord.getUsageType() == UsageTypes.VM_DISK_IO_READ) {
builder.append("Disk I/O read requests");
} else if (usageRecord.getUsageType() == UsageTypes.VM_DISK_IO_WRITE) {
builder.append("Disk I/O write requests");
} else if (usageRecord.getUsageType() == UsageTypes.VM_DISK_BYTES_READ) {
builder.append("Disk I/O read bytes");
} else if (usageRecord.getUsageType() == UsageTypes.VM_DISK_BYTES_WRITE) {
builder.append("Disk I/O write bytes");
}
if (vmInstance != null) {
builder.append(" for VM ").append(vmInstance.getHostName()).append(" (").append(vmInstance.getUuid()).append(")");
}
if (volume != null) {
builder.append(" and volume ").append(volume.getName()).append(" (").append(volume.getUuid()).append(")");
}
if (usageRecord.getRawUsage() != null) {
builder.append(" " + toHumanReadableSize(usageRecord.getRawUsage().longValue()));
}
usageRecResponse.setDescription(builder.toString());
}
} else if (usageRecord.getUsageType() == UsageTypes.VOLUME) {
// Volume ID
VolumeVO volume = _entityMgr.findByIdIncludingRemoved(VolumeVO.class, usageRecord.getUsageId().toString());
resourceType = ResourceObjectType.Volume;
if (volume != null) {
usageRecResponse.setUsageId(volume.getUuid());
resourceId = volume.getId();
}
// Volume Size
usageRecResponse.setSize(usageRecord.getSize());
// Disk Offering Id
DiskOfferingVO diskOff = null;
if (usageRecord.getOfferingId() != null) {
diskOff = _entityMgr.findByIdIncludingRemoved(DiskOfferingVO.class, usageRecord.getOfferingId().toString());
usageRecResponse.setOfferingId(diskOff.getUuid());
}
if (!oldFormat) {
final StringBuilder builder = new StringBuilder();
builder.append("Volume usage ");
if (volume != null) {
builder.append("for ").append(volume.getName()).append(" (").append(volume.getUuid()).append(")");
}
if (diskOff != null) {
builder.append(" with disk offering ").append(diskOff.getName()).append(" (").append(diskOff.getUuid()).append(")");
}
if (template != null) {
builder.append(" and template ").append(template.getName()).append(" (").append(template.getUuid()).append(")");
}
if (usageRecord.getSize() != null) {
builder.append(" and size " + toHumanReadableSize(usageRecord.getSize()));
}
usageRecResponse.setDescription(builder.toString());
}
} else if (usageRecord.getUsageType() == UsageTypes.TEMPLATE || usageRecord.getUsageType() == UsageTypes.ISO) {
// Template/ISO ID
VMTemplateVO tmpl = _entityMgr.findByIdIncludingRemoved(VMTemplateVO.class, usageRecord.getUsageId().toString());
if (tmpl != null) {
usageRecResponse.setUsageId(tmpl.getUuid());
resourceId = tmpl.getId();
}
// Template/ISO Size
usageRecResponse.setSize(usageRecord.getSize());
if (usageRecord.getUsageType() == UsageTypes.ISO) {
usageRecResponse.setVirtualSize(usageRecord.getSize());
resourceType = ResourceObjectType.ISO;
} else {
usageRecResponse.setVirtualSize(usageRecord.getVirtualSize());
resourceType = ResourceObjectType.Template;
}
if (!oldFormat) {
final StringBuilder builder = new StringBuilder();
if (usageRecord.getUsageType() == UsageTypes.TEMPLATE) {
builder.append("Template usage");
} else if (usageRecord.getUsageType() == UsageTypes.ISO) {
builder.append("ISO usage");
}
if (tmpl != null) {
builder.append(" for ").append(tmpl.getName()).append(" (").append(tmpl.getUuid()).append(") ").append("with size ").append(toHumanReadableSize(usageRecord.getSize())).append(" and virtual size ").append(toHumanReadableSize(usageRecord.getVirtualSize()));
}
usageRecResponse.setDescription(builder.toString());
}
} else if (usageRecord.getUsageType() == UsageTypes.SNAPSHOT) {
// Snapshot ID
SnapshotVO snap = _entityMgr.findByIdIncludingRemoved(SnapshotVO.class, usageRecord.getUsageId().toString());
resourceType = ResourceObjectType.Snapshot;
if (snap != null) {
usageRecResponse.setUsageId(snap.getUuid());
resourceId = snap.getId();
}
// Snapshot Size
usageRecResponse.setSize(usageRecord.getSize());
if (!oldFormat) {
final StringBuilder builder = new StringBuilder();
builder.append("Snapshot usage ");
if (snap != null) {
builder.append("for ").append(snap.getName()).append(" (").append(snap.getUuid()).append(") ").append("with size ").append(toHumanReadableSize(usageRecord.getSize()));
}
usageRecResponse.setDescription(builder.toString());
}
} else if (usageRecord.getUsageType() == UsageTypes.LOAD_BALANCER_POLICY) {
// Load Balancer Policy ID
LoadBalancerVO lb = _entityMgr.findByIdIncludingRemoved(LoadBalancerVO.class, usageRecord.getUsageId().toString());
resourceType = ResourceObjectType.LoadBalancer;
if (lb != null) {
usageRecResponse.setUsageId(lb.getUuid());
resourceId = lb.getId();
}
if (!oldFormat) {
final StringBuilder builder = new StringBuilder();
builder.append("Loadbalancer policy usage ");
if (lb != null) {
builder.append(lb.getName()).append(" (").append(lb.getUuid()).append(")");
}
usageRecResponse.setDescription(builder.toString());
}
} else if (usageRecord.getUsageType() == UsageTypes.PORT_FORWARDING_RULE) {
// Port Forwarding Rule ID
PortForwardingRuleVO pf = _entityMgr.findByIdIncludingRemoved(PortForwardingRuleVO.class, usageRecord.getUsageId().toString());
resourceType = ResourceObjectType.PortForwardingRule;
if (pf != null) {
usageRecResponse.setUsageId(pf.getUuid());
resourceId = pf.getId();
}
if (!oldFormat) {
final StringBuilder builder = new StringBuilder();
builder.append("Port forwarding rule usage");
if (pf != null) {
builder.append(" (").append(pf.getUuid()).append(")");
}
usageRecResponse.setDescription(builder.toString());
}
} else if (usageRecord.getUsageType() == UsageTypes.NETWORK_OFFERING) {
// Network Offering Id
NetworkOfferingVO netOff = _entityMgr.findByIdIncludingRemoved(NetworkOfferingVO.class, usageRecord.getOfferingId().toString());
usageRecResponse.setOfferingId(netOff.getUuid());
// is Default
usageRecResponse.setDefault(usageRecord.getUsageId() == 1);
if (!oldFormat) {
final StringBuilder builder = new StringBuilder();
builder.append("Network offering ");
if (netOff != null) {
builder.append(netOff.getName()).append(" (").append(netOff.getUuid()).append(") usage ");
}
if (vmInstance != null) {
builder.append("for VM ").append(vmInstance.getHostName()).append(" (").append(vmInstance.getUuid()).append(") ");
}
usageRecResponse.setDescription(builder.toString());
}
} else if (usageRecord.getUsageType() == UsageTypes.VPN_USERS) {
// VPN User ID
VpnUserVO vpnUser = _entityMgr.findByIdIncludingRemoved(VpnUserVO.class, usageRecord.getUsageId().toString());
if (vpnUser != null) {
usageRecResponse.setUsageId(vpnUser.getUuid());
}
if (!oldFormat) {
final StringBuilder builder = new StringBuilder();
builder.append("VPN usage ");
if (vpnUser != null) {
builder.append("for user ").append(vpnUser.getUsername()).append(" (").append(vpnUser.getUuid()).append(")");
}
usageRecResponse.setDescription(builder.toString());
}
} else if (usageRecord.getUsageType() == UsageTypes.SECURITY_GROUP) {
// Security Group Id
SecurityGroupVO sg = _entityMgr.findByIdIncludingRemoved(SecurityGroupVO.class, usageRecord.getUsageId().toString());
resourceType = ResourceObjectType.SecurityGroup;
if (sg != null) {
resourceId = sg.getId();
usageRecResponse.setUsageId(sg.getUuid());
}
if (!oldFormat) {
final StringBuilder builder = new StringBuilder();
builder.append("Security group");
if (sg != null) {
builder.append(" ").append(sg.getName()).append(" (").append(sg.getUuid()).append(") usage");
}
if (vmInstance != null) {
builder.append(" for VM ").append(vmInstance.getHostName()).append(" (").append(vmInstance.getUuid()).append(")");
}
usageRecResponse.setDescription(builder.toString());
}
} else if (usageRecord.getUsageType() == UsageTypes.BACKUP) {
resourceType = ResourceObjectType.Backup;
final StringBuilder builder = new StringBuilder();
builder.append("Backup usage of size ").append(usageRecord.getUsageDisplay());
if (vmInstance != null) {
resourceId = vmInstance.getId();
usageRecResponse.setResourceName(vmInstance.getInstanceName());
usageRecResponse.setUsageId(vmInstance.getUuid());
builder.append(" for VM ").append(vmInstance.getHostName()).append(" (").append(vmInstance.getUuid()).append(")");
final BackupOffering backupOffering = backupOfferingDao.findByIdIncludingRemoved(usageRecord.getOfferingId());
if (backupOffering != null) {
builder.append(" and backup offering ").append(backupOffering.getName()).append(" (").append(backupOffering.getUuid()).append(", user ad-hoc/scheduled backup allowed: ").append(backupOffering.isUserDrivenBackupAllowed()).append(")");
}
}
usageRecResponse.setDescription(builder.toString());
} else if (usageRecord.getUsageType() == UsageTypes.VM_SNAPSHOT) {
resourceType = ResourceObjectType.VMSnapshot;
VMSnapshotVO vmSnapshotVO = null;
if (usageRecord.getUsageId() != null) {
vmSnapshotVO = vmSnapshotDao.findByIdIncludingRemoved(usageRecord.getUsageId());
if (vmSnapshotVO != null) {
resourceId = vmSnapshotVO.getId();
usageRecResponse.setResourceName(vmSnapshotVO.getDisplayName());
usageRecResponse.setUsageId(vmSnapshotVO.getUuid());
}
}
usageRecResponse.setSize(usageRecord.getSize());
if (usageRecord.getVirtualSize() != null) {
usageRecResponse.setVirtualSize(usageRecord.getVirtualSize());
}
if (usageRecord.getOfferingId() != null) {
usageRecResponse.setOfferingId(usageRecord.getOfferingId().toString());
}
if (!oldFormat) {
VolumeVO volume = null;
if (vmSnapshotVO == null && usageRecord.getUsageId() != null) {
volume = _entityMgr.findByIdIncludingRemoved(VolumeVO.class, usageRecord.getUsageId().toString());
}
DiskOfferingVO diskOff = null;
if (usageRecord.getOfferingId() != null) {
diskOff = _entityMgr.findByIdIncludingRemoved(DiskOfferingVO.class, usageRecord.getOfferingId());
}
final StringBuilder builder = new StringBuilder();
builder.append("VMSnapshot usage");
if (vmSnapshotVO != null) {
builder.append(" Id: ").append(vmSnapshotVO.getUuid());
}
if (vmInstance != null) {
builder.append(" for VM ").append(vmInstance.getHostName()).append(" (").append(vmInstance.getUuid()).append(")");
}
if (volume != null) {
builder.append(" with volume ").append(volume.getName()).append(" (").append(volume.getUuid()).append(")");
}
if (diskOff != null) {
builder.append(" using disk offering ").append(diskOff.getName()).append(" (").append(diskOff.getUuid()).append(")");
}
if (usageRecord.getSize() != null) {
builder.append(" and size " + toHumanReadableSize(usageRecord.getSize()));
}
usageRecResponse.setDescription(builder.toString());
}
} else if (usageRecord.getUsageType() == UsageTypes.VOLUME_SECONDARY) {
VolumeVO volume = _entityMgr.findByIdIncludingRemoved(VolumeVO.class, usageRecord.getUsageId().toString());
if (!oldFormat) {
final StringBuilder builder = new StringBuilder();
builder.append("Volume on secondary storage usage");
if (volume != null) {
builder.append(" for ").append(volume.getName()).append(" (").append(volume.getUuid()).append(") ").append("with size ").append(toHumanReadableSize(usageRecord.getSize()));
}
usageRecResponse.setDescription(builder.toString());
}
} else if (usageRecord.getUsageType() == UsageTypes.VM_SNAPSHOT_ON_PRIMARY) {
resourceType = ResourceObjectType.VMSnapshot;
VMSnapshotVO vmSnapshotVO = null;
if (usageRecord.getUsageId() != null) {
vmSnapshotVO = vmSnapshotDao.findByIdIncludingRemoved(usageRecord.getUsageId());
if (vmSnapshotVO != null) {
resourceId = vmSnapshotVO.getId();
usageRecResponse.setResourceName(vmSnapshotVO.getDisplayName());
usageRecResponse.setUsageId(vmSnapshotVO.getUuid());
}
}
usageRecResponse.setSize(usageRecord.getVirtualSize());
if (!oldFormat) {
final StringBuilder builder = new StringBuilder();
builder.append("VMSnapshot on primary storage usage");
if (vmSnapshotVO != null) {
builder.append(" Id: ").append(vmSnapshotVO.getUuid());
}
if (vmInstance != null) {
builder.append(" for VM ").append(vmInstance.getHostName()).append(" (").append(vmInstance.getUuid()).append(") ").append("with size ").append(toHumanReadableSize(usageRecord.getVirtualSize()));
}
usageRecResponse.setDescription(builder.toString());
}
}
if (resourceTagResponseMap != null && resourceTagResponseMap.get(resourceId + ":" + resourceType) != null) {
usageRecResponse.setTags(resourceTagResponseMap.get(resourceId + ":" + resourceType));
}
if (usageRecord.getRawUsage() != null) {
DecimalFormat decimalFormat = new DecimalFormat("###########.######");
usageRecResponse.setRawUsage(decimalFormat.format(usageRecord.getRawUsage()));
}
if (usageRecord.getStartDate() != null) {
usageRecResponse.setStartDate(getDateStringInternal(usageRecord.getStartDate()));
}
if (usageRecord.getEndDate() != null) {
usageRecResponse.setEndDate(getDateStringInternal(usageRecord.getEndDate()));
}
return usageRecResponse;
}
use of com.cloud.vm.snapshot.VMSnapshotVO in project cloudstack by apache.
the class SnapshotManagerImpl method backupSnapshotFromVmSnapshot.
@Override
public Snapshot backupSnapshotFromVmSnapshot(Long snapshotId, Long vmId, Long volumeId, Long vmSnapshotId) {
VMInstanceVO vm = _vmDao.findById(vmId);
if (vm == null) {
throw new InvalidParameterValueException("Creating snapshot failed due to vm:" + vmId + " doesn't exist");
}
if (!HypervisorType.KVM.equals(vm.getHypervisorType())) {
throw new InvalidParameterValueException("Unsupported hypervisor type " + vm.getHypervisorType() + ". This supports KVM only");
}
VMSnapshotVO vmSnapshot = _vmSnapshotDao.findById(vmSnapshotId);
if (vmSnapshot == null) {
throw new InvalidParameterValueException("Creating snapshot failed due to vmSnapshot:" + vmSnapshotId + " doesn't exist");
}
// check vmsnapshot permissions
Account caller = CallContext.current().getCallingAccount();
_accountMgr.checkAccess(caller, null, true, vmSnapshot);
SnapshotVO snapshot = _snapshotDao.findById(snapshotId);
if (snapshot == null) {
throw new InvalidParameterValueException("Creating snapshot failed due to snapshot:" + snapshotId + " doesn't exist");
}
VolumeInfo volume = volFactory.getVolume(volumeId);
if (volume == null) {
throw new InvalidParameterValueException("Creating snapshot failed due to volume:" + volumeId + " doesn't exist");
}
if (volume.getState() != Volume.State.Ready) {
throw new InvalidParameterValueException("VolumeId: " + volumeId + " is not in " + Volume.State.Ready + " state but " + volume.getState() + ". Cannot take snapshot.");
}
DataStore store = volume.getDataStore();
SnapshotDataStoreVO parentSnapshotDataStoreVO = _snapshotStoreDao.findParent(store.getRole(), store.getId(), volumeId);
if (parentSnapshotDataStoreVO != null) {
// Double check the snapshot is removed or not
SnapshotVO parentSnap = _snapshotDao.findById(parentSnapshotDataStoreVO.getSnapshotId());
if (parentSnap != null && parentSnapshotDataStoreVO.getInstallPath() != null && parentSnapshotDataStoreVO.getInstallPath().equals(vmSnapshot.getName())) {
throw new InvalidParameterValueException("Creating snapshot failed due to snapshot : " + parentSnap.getUuid() + " is created from the same vm snapshot");
}
}
SnapshotInfo snapshotInfo = this.snapshotFactory.getSnapshot(snapshotId, store);
snapshotInfo = (SnapshotInfo) store.create(snapshotInfo);
SnapshotDataStoreVO snapshotOnPrimaryStore = this._snapshotStoreDao.findByStoreSnapshot(store.getRole(), store.getId(), snapshot.getId());
snapshotOnPrimaryStore.setState(ObjectInDataStoreStateMachine.State.Ready);
snapshotOnPrimaryStore.setInstallPath(vmSnapshot.getName());
_snapshotStoreDao.update(snapshotOnPrimaryStore.getId(), snapshotOnPrimaryStore);
snapshot.setState(Snapshot.State.CreatedOnPrimary);
_snapshotDao.update(snapshot.getId(), snapshot);
snapshotInfo = this.snapshotFactory.getSnapshot(snapshotId, store);
Long snapshotOwnerId = vm.getAccountId();
try {
SnapshotStrategy snapshotStrategy = _storageStrategyFactory.getSnapshotStrategy(snapshot, SnapshotOperation.BACKUP);
if (snapshotStrategy == null) {
throw new CloudRuntimeException("Unable to find snaphot strategy to handle snapshot with id '" + snapshotId + "'");
}
snapshotInfo = snapshotStrategy.backupSnapshot(snapshotInfo);
} catch (Exception e) {
s_logger.debug("Failed to backup snapshot from vm snapshot", e);
_resourceLimitMgr.decrementResourceCount(snapshotOwnerId, ResourceType.snapshot);
_resourceLimitMgr.decrementResourceCount(snapshotOwnerId, ResourceType.secondary_storage, new Long(volume.getSize()));
throw new CloudRuntimeException("Failed to backup snapshot from vm snapshot", e);
}
return snapshotInfo;
}
use of com.cloud.vm.snapshot.VMSnapshotVO in project cloudstack by apache.
the class SnapshotManagerImpl method supportedByHypervisor.
private boolean supportedByHypervisor(VolumeInfo volume) {
HypervisorType hypervisorType;
StoragePoolVO storagePool = _storagePoolDao.findById(volume.getDataStore().getId());
ScopeType scope = storagePool.getScope();
if (scope.equals(ScopeType.ZONE)) {
hypervisorType = storagePool.getHypervisor();
} else {
hypervisorType = volume.getHypervisorType();
}
if (hypervisorType.equals(HypervisorType.Ovm)) {
throw new InvalidParameterValueException("Ovm won't support taking snapshot");
}
if (hypervisorType.equals(HypervisorType.KVM)) {
List<HostVO> hosts = null;
if (scope.equals(ScopeType.CLUSTER)) {
ClusterVO cluster = _clusterDao.findById(storagePool.getClusterId());
hosts = _resourceMgr.listAllHostsInCluster(cluster.getId());
} else if (scope.equals(ScopeType.ZONE)) {
hosts = _resourceMgr.listAllUpAndEnabledHostsInOneZoneByHypervisor(hypervisorType, volume.getDataCenterId());
}
if (hosts != null && !hosts.isEmpty()) {
HostVO host = hosts.get(0);
if (!hostSupportSnapsthotForVolume(host, volume)) {
throw new CloudRuntimeException("KVM Snapshot is not supported for Running VMs. It is disabled by default due to a possible volume corruption in certain cases. To enable it set global settings kvm.snapshot.enabled to True. See the documentation for more details.");
}
}
}
// if volume is attached to a vm in destroyed or expunging state; disallow
if (volume.getInstanceId() != null) {
UserVmVO userVm = _vmDao.findById(volume.getInstanceId());
if (userVm != null) {
if (userVm.getState().equals(State.Destroyed) || userVm.getState().equals(State.Expunging)) {
throw new CloudRuntimeException("Creating snapshot failed due to volume:" + volume.getId() + " is associated with vm:" + userVm.getInstanceName() + " is in " + userVm.getState().toString() + " state");
}
if (userVm.getHypervisorType() == HypervisorType.VMware || userVm.getHypervisorType() == HypervisorType.KVM) {
List<SnapshotVO> activeSnapshots = _snapshotDao.listByInstanceId(volume.getInstanceId(), Snapshot.State.Creating, Snapshot.State.CreatedOnPrimary, Snapshot.State.BackingUp);
if (activeSnapshots.size() > 0) {
throw new InvalidParameterValueException("There is other active snapshot tasks on the instance to which the volume is attached, please try again later");
}
}
List<VMSnapshotVO> activeVMSnapshots = _vmSnapshotDao.listByInstanceId(userVm.getId(), VMSnapshot.State.Creating, VMSnapshot.State.Reverting, VMSnapshot.State.Expunging);
if (activeVMSnapshots.size() > 0) {
throw new CloudRuntimeException("There is other active vm snapshot tasks on the instance to which the volume is attached, please try again later");
}
}
}
return true;
}
use of com.cloud.vm.snapshot.VMSnapshotVO in project cloudstack by apache.
the class SnapshotManagerImpl method revertSnapshot.
@Override
public Snapshot revertSnapshot(Long snapshotId) {
SnapshotVO snapshot = _snapshotDao.findById(snapshotId);
if (snapshot == null) {
throw new InvalidParameterValueException("No such snapshot");
}
VolumeVO volume = _volsDao.findById(snapshot.getVolumeId());
if (volume.getState() != Volume.State.Ready) {
throw new InvalidParameterValueException("The volume is not in Ready state.");
}
Long instanceId = volume.getInstanceId();
// in order to revert the volume
if (instanceId != null) {
UserVmVO vm = _vmDao.findById(instanceId);
if (vm.getState() != State.Stopped && vm.getState() != State.Shutdown) {
throw new InvalidParameterValueException("The VM the specified disk is attached to is not in the shutdown state.");
}
// If target VM has associated VM snapshots then don't allow to revert from snapshot
List<VMSnapshotVO> vmSnapshots = _vmSnapshotDao.findByVm(instanceId);
if (vmSnapshots.size() > 0) {
throw new InvalidParameterValueException("Unable to revert snapshot for VM, please remove VM snapshots before reverting VM from snapshot");
}
}
DataStoreRole dataStoreRole = getDataStoreRole(snapshot);
SnapshotInfo snapshotInfo = snapshotFactory.getSnapshot(snapshotId, dataStoreRole);
if (snapshotInfo == null) {
throw new CloudRuntimeException(String.format("snapshot %s [%s] does not exists in data store", snapshot.getName(), snapshot.getUuid()));
}
SnapshotStrategy snapshotStrategy = _storageStrategyFactory.getSnapshotStrategy(snapshot, SnapshotOperation.REVERT);
if (snapshotStrategy == null) {
s_logger.error("Unable to find snaphot strategy to handle snapshot with id '" + snapshotId + "'");
String errorMsg = String.format("Revert snapshot command failed for snapshot with id %d, because this command is supported only for KVM hypervisor", snapshotId);
throw new CloudRuntimeException(errorMsg);
}
boolean result = snapshotStrategy.revertSnapshot(snapshotInfo);
if (result) {
// update volume size and primary storage count
_resourceLimitMgr.decrementResourceCount(snapshot.getAccountId(), ResourceType.primary_storage, new Long(volume.getSize() - snapshot.getSize()));
volume.setSize(snapshot.getSize());
_volsDao.update(volume.getId(), volume);
return snapshotInfo;
}
return null;
}
Aggregations