use of com.cloud.storage.VMTemplateVO in project cloudstack by apache.
the class ConsoleProxyManagerImpl method isZoneReady.
public boolean isZoneReady(Map<Long, ZoneHostInfo> zoneHostInfoMap, long dataCenterId) {
ZoneHostInfo zoneHostInfo = zoneHostInfoMap.get(dataCenterId);
if (zoneHostInfo != null && isZoneHostReady(zoneHostInfo)) {
VMTemplateVO template = _templateDao.findSystemVMReadyTemplate(dataCenterId, HypervisorType.Any);
if (template == null) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("System vm template is not ready at data center " + dataCenterId + ", wait until it is ready to launch console proxy vm");
}
return false;
}
TemplateDataStoreVO templateHostRef = _vmTemplateStoreDao.findByTemplateZoneDownloadStatus(template.getId(), dataCenterId, Status.DOWNLOADED);
if (templateHostRef != null) {
boolean useLocalStorage = false;
Boolean useLocal = ConfigurationManagerImpl.SystemVMUseLocalStorage.valueIn(dataCenterId);
if (useLocal != null) {
useLocalStorage = useLocal.booleanValue();
}
List<Pair<Long, Integer>> l = _consoleProxyDao.getDatacenterStoragePoolHostInfo(dataCenterId, useLocalStorage);
if (l != null && l.size() > 0 && l.get(0).second().intValue() > 0) {
return true;
} else {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Primary storage is not ready, wait until it is ready to launch console proxy");
}
}
} else {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Zone host is ready, but console proxy template: " + template.getId() + " is not ready on secondary storage.");
}
}
}
return false;
}
use of com.cloud.storage.VMTemplateVO in project cloudstack by apache.
the class QueryManagerImpl method searchForTemplatesInternal.
private Pair<List<TemplateJoinVO>, Integer> searchForTemplatesInternal(Long templateId, String name, String keyword, TemplateFilter templateFilter, boolean isIso, Boolean bootable, Long pageSize, Long startIndex, Long zoneId, HypervisorType hyperType, boolean showDomr, boolean onlyReady, List<Account> permittedAccounts, Account caller, ListProjectResourcesCriteria listProjectResourcesCriteria, Map<String, String> tags, boolean showRemovedTmpl, List<Long> ids) {
// check if zone is configured, if not, just return empty list
List<HypervisorType> hypers = null;
if (!isIso) {
hypers = _resourceMgr.listAvailHypervisorInZone(null, null);
if (hypers == null || hypers.isEmpty()) {
return new Pair<List<TemplateJoinVO>, Integer>(new ArrayList<TemplateJoinVO>(), 0);
}
}
VMTemplateVO template = null;
Boolean isAscending = Boolean.parseBoolean(_configDao.getValue("sortkey.algorithm"));
isAscending = (isAscending == null ? Boolean.TRUE : isAscending);
Filter searchFilter = new Filter(TemplateJoinVO.class, "sortKey", isAscending, startIndex, pageSize);
searchFilter.addOrderBy(TemplateJoinVO.class, "tempZonePair", isAscending);
SearchBuilder<TemplateJoinVO> sb = _templateJoinDao.createSearchBuilder();
// select distinct (templateId, zoneId) pair
sb.select(null, Func.DISTINCT, sb.entity().getTempZonePair());
if (ids != null && !ids.isEmpty()) {
sb.and("idIN", sb.entity().getId(), SearchCriteria.Op.IN);
}
SearchCriteria<TemplateJoinVO> sc = sb.create();
// verify templateId parameter and specially handle it
if (templateId != null) {
// Done for backward compatibility - Bug-5221
template = _templateDao.findByIdIncludingRemoved(templateId);
if (template == null) {
throw new InvalidParameterValueException("Please specify a valid template ID.");
}
// If ISO requested then it should be ISO.
if (isIso && template.getFormat() != ImageFormat.ISO) {
s_logger.error("Template Id " + templateId + " is not an ISO");
InvalidParameterValueException ex = new InvalidParameterValueException("Specified Template Id is not an ISO");
ex.addProxyObject(template.getUuid(), "templateId");
throw ex;
}
// If ISO not requested then it shouldn't be an ISO.
if (!isIso && template.getFormat() == ImageFormat.ISO) {
s_logger.error("Incorrect format of the template id " + templateId);
InvalidParameterValueException ex = new InvalidParameterValueException("Incorrect format " + template.getFormat() + " of the specified template id");
ex.addProxyObject(template.getUuid(), "templateId");
throw ex;
}
if (caller.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN) {
Account template_acc = _accountMgr.getAccount(template.getAccountId());
DomainVO domain = _domainDao.findById(template_acc.getDomainId());
_accountMgr.checkAccess(caller, domain);
} else // if template is not public, perform permission check here
if (!template.isPublicTemplate() && caller.getType() != Account.ACCOUNT_TYPE_ADMIN) {
_accountMgr.checkAccess(caller, null, false, template);
}
// if templateId is specified, then we will just use the id to
// search and ignore other query parameters
sc.addAnd("id", SearchCriteria.Op.EQ, templateId);
} else {
DomainVO domain = null;
if (!permittedAccounts.isEmpty()) {
domain = _domainDao.findById(permittedAccounts.get(0).getDomainId());
} else {
domain = _domainDao.findById(Domain.ROOT_DOMAIN);
}
// List<HypervisorType> hypers = null;
// if (!isIso) {
// hypers = _resourceMgr.listAvailHypervisorInZone(null, null);
// }
setIdsListToSearchCriteria(sc, ids);
// add criteria for project or not
if (listProjectResourcesCriteria == ListProjectResourcesCriteria.SkipProjectResources) {
sc.addAnd("accountType", SearchCriteria.Op.NEQ, Account.ACCOUNT_TYPE_PROJECT);
} else if (listProjectResourcesCriteria == ListProjectResourcesCriteria.ListProjectResourcesOnly) {
sc.addAnd("accountType", SearchCriteria.Op.EQ, Account.ACCOUNT_TYPE_PROJECT);
}
// add criteria for domain path in case of domain admin
if ((templateFilter == TemplateFilter.self || templateFilter == TemplateFilter.selfexecutable) && (caller.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN || caller.getType() == Account.ACCOUNT_TYPE_RESOURCE_DOMAIN_ADMIN)) {
sc.addAnd("domainPath", SearchCriteria.Op.LIKE, domain.getPath() + "%");
}
List<Long> relatedDomainIds = new ArrayList<Long>();
List<Long> permittedAccountIds = new ArrayList<Long>();
if (!permittedAccounts.isEmpty()) {
for (Account account : permittedAccounts) {
permittedAccountIds.add(account.getId());
boolean publicTemplates = (templateFilter == TemplateFilter.featured || templateFilter == TemplateFilter.community);
// get all parent domain ID's all the way till root domain
DomainVO domainTreeNode = null;
//if template filter is featured, or community, all child domains should be included in search
if (publicTemplates) {
domainTreeNode = _domainDao.findById(Domain.ROOT_DOMAIN);
} else {
domainTreeNode = _domainDao.findById(account.getDomainId());
}
relatedDomainIds.add(domainTreeNode.getId());
while (domainTreeNode.getParent() != null) {
domainTreeNode = _domainDao.findById(domainTreeNode.getParent());
relatedDomainIds.add(domainTreeNode.getId());
}
// get all child domain ID's
if (_accountMgr.isAdmin(account.getId()) || publicTemplates) {
List<DomainVO> allChildDomains = _domainDao.findAllChildren(domainTreeNode.getPath(), domainTreeNode.getId());
for (DomainVO childDomain : allChildDomains) {
relatedDomainIds.add(childDomain.getId());
}
}
}
}
if (!isIso) {
// add hypervisor criteria for template case
if (hypers != null && !hypers.isEmpty()) {
String[] relatedHypers = new String[hypers.size()];
for (int i = 0; i < hypers.size(); i++) {
relatedHypers[i] = hypers.get(i).toString();
}
sc.addAnd("hypervisorType", SearchCriteria.Op.IN, relatedHypers);
}
}
// control different template filters
if (templateFilter == TemplateFilter.featured || templateFilter == TemplateFilter.community) {
sc.addAnd("publicTemplate", SearchCriteria.Op.EQ, true);
if (templateFilter == TemplateFilter.featured) {
sc.addAnd("featured", SearchCriteria.Op.EQ, true);
} else {
sc.addAnd("featured", SearchCriteria.Op.EQ, false);
}
if (!permittedAccounts.isEmpty()) {
SearchCriteria<TemplateJoinVO> scc = _templateJoinDao.createSearchCriteria();
scc.addOr("domainId", SearchCriteria.Op.IN, relatedDomainIds.toArray());
scc.addOr("domainId", SearchCriteria.Op.NULL);
sc.addAnd("domainId", SearchCriteria.Op.SC, scc);
}
} else if (templateFilter == TemplateFilter.self || templateFilter == TemplateFilter.selfexecutable) {
if (!permittedAccounts.isEmpty()) {
sc.addAnd("accountId", SearchCriteria.Op.IN, permittedAccountIds.toArray());
}
} else if (templateFilter == TemplateFilter.sharedexecutable || templateFilter == TemplateFilter.shared) {
// only show templates shared by others
sc.addAnd("sharedAccountId", SearchCriteria.Op.IN, permittedAccountIds.toArray());
} else if (templateFilter == TemplateFilter.executable) {
SearchCriteria<TemplateJoinVO> scc = _templateJoinDao.createSearchCriteria();
scc.addOr("publicTemplate", SearchCriteria.Op.EQ, true);
if (!permittedAccounts.isEmpty()) {
scc.addOr("accountId", SearchCriteria.Op.IN, permittedAccountIds.toArray());
}
sc.addAnd("publicTemplate", SearchCriteria.Op.SC, scc);
} else if (templateFilter == TemplateFilter.all && caller.getType() != Account.ACCOUNT_TYPE_ADMIN) {
SearchCriteria<TemplateJoinVO> scc = _templateJoinDao.createSearchCriteria();
scc.addOr("publicTemplate", SearchCriteria.Op.EQ, true);
if (listProjectResourcesCriteria == ListProjectResourcesCriteria.SkipProjectResources) {
scc.addOr("domainPath", SearchCriteria.Op.LIKE, _domainDao.findById(caller.getDomainId()).getPath() + "%");
} else {
if (!permittedAccounts.isEmpty()) {
scc.addOr("accountId", SearchCriteria.Op.IN, permittedAccountIds.toArray());
scc.addOr("sharedAccountId", SearchCriteria.Op.IN, permittedAccountIds.toArray());
}
}
sc.addAnd("publicTemplate", SearchCriteria.Op.SC, scc);
}
// add tags criteria
if (tags != null && !tags.isEmpty()) {
SearchCriteria<TemplateJoinVO> scc = _templateJoinDao.createSearchCriteria();
for (Map.Entry<String, String> entry : tags.entrySet()) {
SearchCriteria<TemplateJoinVO> scTag = _templateJoinDao.createSearchCriteria();
scTag.addAnd("tagKey", SearchCriteria.Op.EQ, entry.getKey());
scTag.addAnd("tagValue", SearchCriteria.Op.EQ, entry.getValue());
if (isIso) {
scTag.addAnd("tagResourceType", SearchCriteria.Op.EQ, ResourceObjectType.ISO);
} else {
scTag.addAnd("tagResourceType", SearchCriteria.Op.EQ, ResourceObjectType.Template);
}
scc.addOr("tagKey", SearchCriteria.Op.SC, scTag);
}
sc.addAnd("tagKey", SearchCriteria.Op.SC, scc);
}
if (keyword != null) {
sc.addAnd("name", SearchCriteria.Op.LIKE, "%" + keyword + "%");
} else if (name != null) {
sc.addAnd("name", SearchCriteria.Op.EQ, name);
}
if (isIso) {
sc.addAnd("format", SearchCriteria.Op.EQ, "ISO");
} else {
sc.addAnd("format", SearchCriteria.Op.NEQ, "ISO");
}
if (!hyperType.equals(HypervisorType.None)) {
sc.addAnd("hypervisorType", SearchCriteria.Op.EQ, hyperType);
}
if (bootable != null) {
sc.addAnd("bootable", SearchCriteria.Op.EQ, bootable);
}
if (onlyReady) {
SearchCriteria<TemplateJoinVO> readySc = _templateJoinDao.createSearchCriteria();
readySc.addOr("state", SearchCriteria.Op.EQ, TemplateState.Ready);
readySc.addOr("format", SearchCriteria.Op.EQ, ImageFormat.BAREMETAL);
SearchCriteria<TemplateJoinVO> isoPerhostSc = _templateJoinDao.createSearchCriteria();
isoPerhostSc.addAnd("format", SearchCriteria.Op.EQ, ImageFormat.ISO);
isoPerhostSc.addAnd("templateType", SearchCriteria.Op.EQ, TemplateType.PERHOST);
readySc.addOr("templateType", SearchCriteria.Op.SC, isoPerhostSc);
sc.addAnd("state", SearchCriteria.Op.SC, readySc);
}
if (!showDomr) {
// excluding system template
sc.addAnd("templateType", SearchCriteria.Op.NEQ, Storage.TemplateType.SYSTEM);
}
}
if (zoneId != null) {
SearchCriteria<TemplateJoinVO> zoneSc = _templateJoinDao.createSearchCriteria();
zoneSc.addOr("dataCenterId", SearchCriteria.Op.EQ, zoneId);
zoneSc.addOr("dataStoreScope", SearchCriteria.Op.EQ, ScopeType.REGION);
// handle the case where xs-tools.iso and vmware-tools.iso do not
// have data_center information in template_view
SearchCriteria<TemplateJoinVO> isoPerhostSc = _templateJoinDao.createSearchCriteria();
isoPerhostSc.addAnd("format", SearchCriteria.Op.EQ, ImageFormat.ISO);
isoPerhostSc.addAnd("templateType", SearchCriteria.Op.EQ, TemplateType.PERHOST);
zoneSc.addOr("templateType", SearchCriteria.Op.SC, isoPerhostSc);
sc.addAnd("dataCenterId", SearchCriteria.Op.SC, zoneSc);
}
// don't return removed template, this should not be needed since we
// changed annotation for removed field in TemplateJoinVO.
// sc.addAnd("removed", SearchCriteria.Op.NULL);
// search unique templates and find details by Ids
Pair<List<TemplateJoinVO>, Integer> uniqueTmplPair = null;
if (showRemovedTmpl) {
uniqueTmplPair = _templateJoinDao.searchIncludingRemovedAndCount(sc, searchFilter);
} else {
sc.addAnd("templateState", SearchCriteria.Op.IN, new State[] { State.Active, State.NotUploaded, State.UploadInProgress });
uniqueTmplPair = _templateJoinDao.searchAndCount(sc, searchFilter);
}
Integer count = uniqueTmplPair.second();
if (count.intValue() == 0) {
// empty result
return uniqueTmplPair;
}
List<TemplateJoinVO> uniqueTmpls = uniqueTmplPair.first();
String[] tzIds = new String[uniqueTmpls.size()];
int i = 0;
for (TemplateJoinVO v : uniqueTmpls) {
tzIds[i++] = v.getTempZonePair();
}
List<TemplateJoinVO> vrs = _templateJoinDao.searchByTemplateZonePair(showRemovedTmpl, tzIds);
return new Pair<List<TemplateJoinVO>, Integer>(vrs, count);
// TODO: revisit the special logic for iso search in
// VMTemplateDaoImpl.searchForTemplates and understand why we need to
// specially handle ISO. The original logic is very twisted and no idea
// about what the code was doing.
}
use of com.cloud.storage.VMTemplateVO in project cloudstack by apache.
the class UserVmManagerTest method testValidateRootDiskResize.
@Test
public void testValidateRootDiskResize() {
HypervisorType hypervisorType = HypervisorType.Any;
Long rootDiskSize = Long.valueOf(10);
UserVmVO vm = Mockito.mock(UserVmVO.class);
VMTemplateVO templateVO = Mockito.mock(VMTemplateVO.class);
Map<String, String> customParameters = new HashMap<String, String>();
Map<String, String> vmDetals = new HashMap<String, String>();
vmDetals.put("rootDiskController", "ide");
when(vm.getDetails()).thenReturn(vmDetals);
when(templateVO.getSize()).thenReturn((rootDiskSize << 30) + 1);
//Case 1: >
try {
_userVmMgr.validateRootDiskResize(hypervisorType, rootDiskSize, templateVO, vm, customParameters);
Assert.fail("Function should throw InvalidParameterValueException");
} catch (Exception e) {
assertThat(e, instanceOf(InvalidParameterValueException.class));
}
//Case 2: =
when(templateVO.getSize()).thenReturn((rootDiskSize << 30));
customParameters.put("rootdisksize", "10");
_userVmMgr.validateRootDiskResize(hypervisorType, rootDiskSize, templateVO, vm, customParameters);
assert (!customParameters.containsKey("rootdisksize"));
when(templateVO.getSize()).thenReturn((rootDiskSize << 30) - 1);
//Case 3: <
//Case 3.1: HypervisorType!=VMware
_userVmMgr.validateRootDiskResize(hypervisorType, rootDiskSize, templateVO, vm, customParameters);
hypervisorType = HypervisorType.VMware;
//Case 3.2: 0->(rootDiskController!=scsi)
try {
_userVmMgr.validateRootDiskResize(hypervisorType, rootDiskSize, templateVO, vm, customParameters);
Assert.fail("Function should throw InvalidParameterValueException");
} catch (Exception e) {
assertThat(e, instanceOf(InvalidParameterValueException.class));
}
//Case 3.3: 1->(rootDiskController==scsi)
vmDetals.put("rootDiskController", "scsi");
_userVmMgr.validateRootDiskResize(hypervisorType, rootDiskSize, templateVO, vm, customParameters);
}
use of com.cloud.storage.VMTemplateVO in project cloudstack by apache.
the class ApiResponseHelper method createUsageResponse.
@Override
public UsageRecordResponse createUsageResponse(Usage usageRecord) {
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());
if (usageRecord.getVmInstanceId() != null) {
VMInstanceVO vm = _entityMgr.findByIdIncludingRemoved(VMInstanceVO.class, usageRecord.getVmInstanceId());
if (vm != null) {
usageRecResponse.setVirtualMachineId(vm.getUuid());
}
}
usageRecResponse.setVmName(usageRecord.getVmName());
if (usageRecord.getTemplateId() != null) {
VMTemplateVO template = ApiDBUtils.findTemplateById(usageRecord.getTemplateId());
if (template != null) {
usageRecResponse.setTemplateId(template.getUuid());
}
}
if (usageRecord.getUsageType() == UsageTypes.RUNNING_VM || usageRecord.getUsageType() == UsageTypes.ALLOCATED_VM) {
ServiceOfferingVO svcOffering = _entityMgr.findByIdIncludingRemoved(ServiceOfferingVO.class, usageRecord.getOfferingId().toString());
//Service Offering Id
usageRecResponse.setOfferingId(svcOffering.getUuid());
//VM Instance ID
VMInstanceVO vm = _entityMgr.findByIdIncludingRemoved(VMInstanceVO.class, usageRecord.getUsageId().toString());
if (vm != null) {
usageRecResponse.setUsageId(vm.getUuid());
}
//Hypervisor Type
usageRecResponse.setType(usageRecord.getType());
//Dynamic compute offerings details
usageRecResponse.setCpuNumber(usageRecord.getCpuCores());
usageRecResponse.setCpuSpeed(usageRecord.getCpuSpeed());
usageRecResponse.setMemory(usageRecord.getMemory());
} else if (usageRecord.getUsageType() == UsageTypes.IP_ADDRESS) {
//isSourceNAT
usageRecResponse.setSourceNat((usageRecord.getType().equals("SourceNat")) ? true : false);
//isSystem
usageRecResponse.setSystem((usageRecord.getSize() == 1) ? true : false);
//IP Address ID
IPAddressVO ip = _entityMgr.findByIdIncludingRemoved(IPAddressVO.class, usageRecord.getUsageId().toString());
if (ip != null) {
usageRecResponse.setUsageId(ip.getUuid());
}
} else if (usageRecord.getUsageType() == UsageTypes.NETWORK_BYTES_SENT || usageRecord.getUsageType() == UsageTypes.NETWORK_BYTES_RECEIVED) {
//Device Type
usageRecResponse.setType(usageRecord.getType());
if (usageRecord.getType().equals("DomainRouter")) {
//Domain Router Id
VMInstanceVO vm = _entityMgr.findByIdIncludingRemoved(VMInstanceVO.class, usageRecord.getUsageId().toString());
if (vm != null) {
usageRecResponse.setUsageId(vm.getUuid());
}
} else {
//External Device Host Id
HostVO host = _entityMgr.findByIdIncludingRemoved(HostVO.class, usageRecord.getUsageId().toString());
if (host != null) {
usageRecResponse.setUsageId(host.getUuid());
}
}
//Network ID
if ((usageRecord.getNetworkId() != null) && (usageRecord.getNetworkId() != 0)) {
NetworkVO network = _entityMgr.findByIdIncludingRemoved(NetworkVO.class, usageRecord.getNetworkId().toString());
if (network != null) {
usageRecResponse.setNetworkId(network.getUuid());
}
}
} 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());
//VM Instance Id
VMInstanceVO vm = _entityMgr.findByIdIncludingRemoved(VMInstanceVO.class, usageRecord.getVmInstanceId().toString());
if (vm != null) {
usageRecResponse.setVirtualMachineId(vm.getUuid());
}
//Volume ID
VolumeVO volume = _entityMgr.findByIdIncludingRemoved(VolumeVO.class, usageRecord.getUsageId().toString());
if (volume != null) {
usageRecResponse.setUsageId(volume.getUuid());
}
} else if (usageRecord.getUsageType() == UsageTypes.VOLUME) {
//Volume ID
VolumeVO volume = _entityMgr.findByIdIncludingRemoved(VolumeVO.class, usageRecord.getUsageId().toString());
if (volume != null) {
usageRecResponse.setUsageId(volume.getUuid());
}
//Volume Size
usageRecResponse.setSize(usageRecord.getSize());
//Disk Offering Id
if (usageRecord.getOfferingId() != null) {
DiskOfferingVO diskOff = _entityMgr.findByIdIncludingRemoved(DiskOfferingVO.class, usageRecord.getOfferingId().toString());
usageRecResponse.setOfferingId(diskOff.getUuid());
}
} 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());
}
//Template/ISO Size
usageRecResponse.setSize(usageRecord.getSize());
if (usageRecord.getUsageType() == UsageTypes.ISO) {
usageRecResponse.setVirtualSize(usageRecord.getSize());
} else {
usageRecResponse.setVirtualSize(usageRecord.getVirtualSize());
}
} else if (usageRecord.getUsageType() == UsageTypes.SNAPSHOT) {
//Snapshot ID
SnapshotVO snap = _entityMgr.findByIdIncludingRemoved(SnapshotVO.class, usageRecord.getUsageId().toString());
if (snap != null) {
usageRecResponse.setUsageId(snap.getUuid());
}
//Snapshot Size
usageRecResponse.setSize(usageRecord.getSize());
} else if (usageRecord.getUsageType() == UsageTypes.LOAD_BALANCER_POLICY) {
//Load Balancer Policy ID
LoadBalancerVO lb = _entityMgr.findByIdIncludingRemoved(LoadBalancerVO.class, usageRecord.getUsageId().toString());
if (lb != null) {
usageRecResponse.setUsageId(lb.getUuid());
}
} else if (usageRecord.getUsageType() == UsageTypes.PORT_FORWARDING_RULE) {
//Port Forwarding Rule ID
PortForwardingRuleVO pf = _entityMgr.findByIdIncludingRemoved(PortForwardingRuleVO.class, usageRecord.getUsageId().toString());
if (pf != null) {
usageRecResponse.setUsageId(pf.getUuid());
}
} 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) ? true : false);
} 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());
}
} else if (usageRecord.getUsageType() == UsageTypes.SECURITY_GROUP) {
//Security Group Id
SecurityGroupVO sg = _entityMgr.findByIdIncludingRemoved(SecurityGroupVO.class, usageRecord.getUsageId().toString());
if (sg != null) {
usageRecResponse.setUsageId(sg.getUuid());
}
} else if (usageRecord.getUsageType() == UsageTypes.VM_SNAPSHOT) {
VMInstanceVO vm = _entityMgr.findByIdIncludingRemoved(VMInstanceVO.class, usageRecord.getVmInstanceId().toString());
if (vm != null) {
usageRecResponse.setVmName(vm.getInstanceName());
usageRecResponse.setUsageId(vm.getUuid());
}
usageRecResponse.setSize(usageRecord.getSize());
if (usageRecord.getOfferingId() != null) {
usageRecResponse.setOfferingId(usageRecord.getOfferingId().toString());
}
}
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.storage.VMTemplateVO in project cloudstack by apache.
the class SecondaryStorageDiscoverer method associateTemplatesToZone.
private void associateTemplatesToZone(long hostId, long dcId) {
VMTemplateZoneVO tmpltZone;
List<VMTemplateVO> allTemplates = _vmTemplateDao.listAll();
for (VMTemplateVO vt : allTemplates) {
if (vt.isCrossZones()) {
tmpltZone = _vmTemplateZoneDao.findByZoneTemplate(dcId, vt.getId());
if (tmpltZone == null) {
VMTemplateZoneVO vmTemplateZone = new VMTemplateZoneVO(dcId, vt.getId(), new Date());
_vmTemplateZoneDao.persist(vmTemplateZone);
}
}
}
}
Aggregations