diff --git a/server/src/com/cloud/api/commands/ListIsosCmd.java b/server/src/com/cloud/api/commands/ListIsosCmd.java index 9ee0a7f4159..09fa3dc906f 100644 --- a/server/src/com/cloud/api/commands/ListIsosCmd.java +++ b/server/src/com/cloud/api/commands/ListIsosCmd.java @@ -26,43 +26,30 @@ import java.util.Map; import org.apache.log4j.Logger; import com.cloud.api.BaseCmd; +import com.cloud.api.BaseListCmd; +import com.cloud.api.Implementation; import com.cloud.api.Parameter; import com.cloud.api.ServerApiException; +import com.cloud.api.response.TemplateResponse; import com.cloud.async.AsyncJobVO; import com.cloud.dc.DataCenterVO; import com.cloud.domain.DomainVO; import com.cloud.host.HostVO; +import com.cloud.serializer.SerializerHelper; import com.cloud.storage.GuestOS; import com.cloud.storage.VMTemplateHostVO; import com.cloud.storage.VMTemplateStorageResourceAssoc.Status; import com.cloud.storage.VMTemplateVO; import com.cloud.storage.dao.VMTemplateDao.TemplateFilter; import com.cloud.user.Account; +import com.cloud.user.UserContext; import com.cloud.utils.Pair; -public class ListIsosCmd extends BaseCmd { +@Implementation(method="listTemplates") +public class ListIsosCmd extends BaseListCmd { public static final Logger s_logger = Logger.getLogger(ListIsosCmd.class.getName()); private static final String s_name = "listisosresponse"; - private static final List> s_properties = new ArrayList>(); - - static { - s_properties.add(new Pair(BaseCmd.Properties.ACCOUNT, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.BOOTABLE, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.DOMAIN_ID, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.ID, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.IS_PUBLIC, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.IS_READY, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.ISO_FILTER, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.NAME, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.ZONE_ID, Boolean.FALSE)); - - s_properties.add(new Pair(BaseCmd.Properties.ACCOUNT_OBJ, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.KEYWORD, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.PAGE, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.PAGESIZE, Boolean.FALSE)); - - } ///////////////////////////////////////////////////// //////////////// API parameters ///////////////////// @@ -87,7 +74,7 @@ public class ListIsosCmd extends BaseCmd { private Boolean ready; @Parameter(name="isofilter", type=CommandType.STRING) - private String isoFilter; + private String isoFilter = TemplateFilter.selfexecutable.toString(); @Parameter(name="name", type=CommandType.STRING) private String isoName; @@ -144,97 +131,49 @@ public class ListIsosCmd extends BaseCmd { public String getName() { return s_name; } - @Override - public List> getProperties() { - return s_properties; - } - @Override - public List> execute(Map params) { - String accountName = (String)params.get(BaseCmd.Properties.ACCOUNT.getName()); - Long domainId = (Long)params.get(BaseCmd.Properties.DOMAIN_ID.getName()); - Long id = (Long) params.get(BaseCmd.Properties.ID.getName()); - String name = (String) params.get(BaseCmd.Properties.NAME.getName()); - Account account = (Account) params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName()); - Integer page = (Integer)params.get(BaseCmd.Properties.PAGE.getName()); - Integer pageSize = (Integer)params.get(BaseCmd.Properties.PAGESIZE.getName()); - String isoFilterString = (String) params.get(BaseCmd.Properties.ISO_FILTER.getName()); - Boolean bootable = (Boolean)params.get(BaseCmd.Properties.BOOTABLE.getName()); - String keyword = (String)params.get(BaseCmd.Properties.KEYWORD.getName()); - Long zoneId = (Long)params.get(BaseCmd.Properties.ZONE_ID.getName()); - - boolean isAdmin = false; - - TemplateFilter isoFilter; + @Override @SuppressWarnings("unchecked") + public String getResponse() { + TemplateFilter isoFilterObj = null; try { - if (isoFilterString == null) { - isoFilter = TemplateFilter.selfexecutable; - } else { - isoFilter = TemplateFilter.valueOf(isoFilterString); - } - } catch (IllegalArgumentException e) { - throw new ServerApiException(BaseCmd.PARAM_ERROR, "Please specify a valid template filter."); - } - - Long accountId = null; - if ((account == null) || (account.getType() == Account.ACCOUNT_TYPE_ADMIN)) { - isAdmin = true; - // validate domainId before proceeding - if (domainId != null) { - if ((account != null) && !getManagementServer().isChildDomain(account.getDomainId(), domainId)) { - throw new ServerApiException(BaseCmd.PARAM_ERROR, "Invalid domain id (" + domainId + ") given, unable to list events."); - } - if (accountName != null) { - Account userAccount = getManagementServer().findAccountByName(accountName, domainId); - if (userAccount != null) { - accountId = userAccount.getId(); - } else { - throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "Unable to find account " + accountName + " in domain " + domainId); - } - } + if (isoFilter == null) { + isoFilterObj = TemplateFilter.selfexecutable; } else { - domainId = ((account == null) ? DomainVO.ROOT_DOMAIN : account.getDomainId()); + isoFilterObj = TemplateFilter.valueOf(isoFilter); } - } else { - accountId = account.getId(); - accountName = account.getAccountName(); - domainId = account.getDomainId(); + } catch (IllegalArgumentException e) { + // how did we get this far? The request should've been rejected already before the response stage... + isoFilterObj = TemplateFilter.selfexecutable; } - - Long startIndex = Long.valueOf(0); - int pageSizeNum = 50; - if (pageSize != null) { - pageSizeNum = pageSize.intValue(); - } - if (page != null) { - int pageNum = page.intValue(); - if (pageNum > 0) { - startIndex = Long.valueOf(pageSizeNum * (pageNum-1)); + + boolean isAdmin = false; + boolean isAccountSpecific = true; + Account account = (Account)UserContext.current().getAccountObject(); + if ((account == null) || (account.getType() == Account.ACCOUNT_TYPE_ADMIN) || (account.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN)) { + isAdmin = true; + if ((accountName == null) || (domainId == null)) { + isAccountSpecific = false; } } - - boolean onlyReady = (isoFilter == TemplateFilter.featured) || - (isoFilter == TemplateFilter.selfexecutable) || - (isoFilter == TemplateFilter.sharedexecutable) || - (isoFilter == TemplateFilter.executable && accountId != null) || - (isoFilter == TemplateFilter.community); - - List isos = null; - try { - isos = getManagementServer().listTemplates(id, name, keyword, isoFilter, true, bootable, accountId, pageSize, startIndex, zoneId); - } catch (Exception e) { - throw new ServerApiException(BaseCmd.INTERNAL_ERROR, e.getMessage()); - } - - int numTags = 0; + + boolean onlyReady = (isoFilterObj == TemplateFilter.featured) || + (isoFilterObj == TemplateFilter.selfexecutable) || + (isoFilterObj == TemplateFilter.sharedexecutable) || + (isoFilterObj == TemplateFilter.executable && isAccountSpecific) || + (isoFilterObj == TemplateFilter.community); + + List isos = (List)getResponseObject(); + Map> isoHostsMap = new HashMap>(); for (VMTemplateVO iso : isos) { - List isoHosts = getManagementServer().listTemplateHostBy(iso.getId(), zoneId); + // TODO: implement + List isoHosts = getManagementServer().listTemplateHostBy(iso.getId(), zoneId); if (iso.getName().equals("xs-tools.iso")) { List xstoolsZones = new ArrayList(); // the xs-tools.iso is a special case since it will be available on every computing host in the zone and we want to return it once per zone List xstoolsHosts = new ArrayList(); for (VMTemplateHostVO isoHost : isoHosts) { + // TODO: implement HostVO host = getManagementServer().getHostBy(isoHost.getHostId()); if (!xstoolsZones.contains(Long.valueOf(host.getDataCenterId()))) { xstoolsZones.add(Long.valueOf(host.getDataCenterId())); @@ -242,102 +181,93 @@ public class ListIsosCmd extends BaseCmd { } } isoHostsMap.put(iso.getId(), xstoolsHosts); - numTags += xstoolsHosts.size(); } else { isoHostsMap.put(iso.getId(), isoHosts); - numTags += isoHosts.size(); } } - List isoTagList = new ArrayList(); - List> isoTags = new ArrayList>(); + List response = new ArrayList(); for (VMTemplateVO iso : isos) { - List isoHosts = isoHostsMap.get(iso.getId()); - for (VMTemplateHostVO isoHost : isoHosts) { - if (onlyReady && isoHost.getDownloadState() != Status.DOWNLOADED) { - continue; - } - - List> isoData = new ArrayList>(); - isoData.add(new Pair(BaseCmd.Properties.ID.getName(), iso.getId().toString())); - isoData.add(new Pair(BaseCmd.Properties.NAME.getName(), iso.getName())); - isoData.add(new Pair(BaseCmd.Properties.DISPLAY_TEXT.getName(), iso.getDisplayText())); - isoData.add(new Pair(BaseCmd.Properties.IS_PUBLIC.getName(), Boolean.valueOf(iso.isPublicTemplate()).toString())); - isoData.add(new Pair(BaseCmd.Properties.CREATED.getName(), getDateString(isoHost.getCreated()))); - isoData.add(new Pair(BaseCmd.Properties.IS_READY.getName(), Boolean.valueOf(isoHost.getDownloadState()==Status.DOWNLOADED).toString())); - isoData.add(new Pair(BaseCmd.Properties.BOOTABLE.getName(), Boolean.valueOf(iso.isBootable()).toString())); - isoData.add(new Pair(BaseCmd.Properties.IS_FEATURED.getName(), Boolean.valueOf(iso.isFeatured()).toString())); - isoData.add(new Pair(BaseCmd.Properties.CROSS_ZONES.getName(), Boolean.valueOf(iso.isCrossZones()).toString())); - - GuestOS os = getManagementServer().findGuestOSById(iso.getGuestOSId()); - if(os != null) { - isoData.add(new Pair(BaseCmd.Properties.OS_TYPE_ID.getName(), os.getId())); - isoData.add(new Pair(BaseCmd.Properties.OS_TYPE_NAME.getName(), os.getDisplayName())); - } else { - isoData.add(new Pair(BaseCmd.Properties.OS_TYPE_ID.getName(), -1)); - isoData.add(new Pair(BaseCmd.Properties.OS_TYPE_NAME.getName(), "")); - } - - // add account ID and name - Account owner = getManagementServer().findAccountById(iso.getAccountId()); - if (owner != null) { - isoData.add(new Pair(BaseCmd.Properties.ACCOUNT.getName(), owner.getAccountName())); - isoData.add(new Pair(BaseCmd.Properties.DOMAIN_ID.getName(), owner.getDomainId())); - isoData.add(new Pair(BaseCmd.Properties.DOMAIN.getName(), getManagementServer().findDomainIdById(owner.getDomainId()).getName())); - } - - // Add the zone ID - HostVO host = getManagementServer().getHostBy(isoHost.getHostId()); - isoData.add(new Pair(BaseCmd.Properties.ZONE_ID.getName(), host.getDataCenterId())); + List isoHosts = isoHostsMap.get(iso.getId()); + for (VMTemplateHostVO isoHost : isoHosts) { + if (onlyReady && isoHost.getDownloadState() != Status.DOWNLOADED) { + continue; + } + TemplateResponse isoResponse = new TemplateResponse(); + isoResponse.setId(iso.getId()); + isoResponse.setName(iso.getName()); + isoResponse.setDisplayText(iso.getDisplayText()); + isoResponse.setPublic(iso.isPublicTemplate()); + isoResponse.setCreated(isoHost.getCreated()); + isoResponse.setReady(isoHost.getDownloadState() == Status.DOWNLOADED); + isoResponse.setBootable(iso.isBootable()); + isoResponse.setFeatured(iso.isFeatured()); + isoResponse.setCrossZones(iso.isCrossZones()); + + // TODO: implement + GuestOS os = getManagementServer().findGuestOSById(iso.getGuestOSId()); + if (os != null) { + isoResponse.setOsTypeId(os.getId()); + isoResponse.setOsTypeName(os.getDisplayName()); + } else { + isoResponse.setOsTypeId(-1L); + isoResponse.setOsTypeName(""); + } + + // add account ID and name + Account owner = getManagementServer().findAccountById(iso.getAccountId()); + if (owner != null) { + isoResponse.setAccount(owner.getAccountName()); + isoResponse.setDomainId(owner.getDomainId()); + // TODO: implement + isoResponse.setDomainName(getManagementServer().findDomainIdById(owner.getDomainId()).getName()); + } + + // Add the zone ID + // TODO: implement + HostVO host = getManagementServer().getHostBy(isoHost.getHostId()); DataCenterVO datacenter = getManagementServer().getDataCenterBy(host.getDataCenterId()); - isoData.add(new Pair(BaseCmd.Properties.ZONE_NAME.getName(), datacenter.getName())); - + isoResponse.setZoneId(host.getDataCenterId()); + isoResponse.setZoneName(datacenter.getName()); + // If the user is an admin, add the template download status if (isAdmin || account.getId().longValue() == iso.getAccountId()) { - // add download status - if (isoHost.getDownloadState()!=Status.DOWNLOADED) { - String isoStatus = "Processing"; - if (isoHost.getDownloadState() == VMTemplateHostVO.Status.DOWNLOADED) { - isoStatus = "Download Complete"; - } else if (isoHost.getDownloadState() == VMTemplateHostVO.Status.DOWNLOAD_IN_PROGRESS) { - if (isoHost.getDownloadPercent() == 100) { - isoStatus = "Installing ISO"; - } else { - isoStatus = isoHost.getDownloadPercent() + "% Downloaded"; - } - } else { - isoStatus = isoHost.getErrorString(); - } - isoData.add(new Pair(BaseCmd.Properties.ISO_STATUS.getName(), isoStatus)); - } else { - isoData.add(new Pair(BaseCmd.Properties.ISO_STATUS.getName(), "Successfully Installed")); - } + // add download status + if (isoHost.getDownloadState()!=Status.DOWNLOADED) { + String isoStatus = "Processing"; + if (isoHost.getDownloadState() == VMTemplateHostVO.Status.DOWNLOADED) { + isoStatus = "Download Complete"; + } else if (isoHost.getDownloadState() == VMTemplateHostVO.Status.DOWNLOAD_IN_PROGRESS) { + if (isoHost.getDownloadPercent() == 100) { + isoStatus = "Installing ISO"; + } else { + isoStatus = isoHost.getDownloadPercent() + "% Downloaded"; + } + } else { + isoStatus = isoHost.getErrorString(); + } + isoResponse.setStatus(isoStatus); + } else { + isoResponse.setStatus("Successfully Installed"); + } } long isoSize = isoHost.getSize(); if (isoSize > 0) { - isoData.add(new Pair(BaseCmd.Properties.SIZE.getName(), isoSize)); + isoResponse.setSize(isoSize); } AsyncJobVO asyncJob = getManagementServer().findInstancePendingAsyncJob("vm_template", iso.getId()); if(asyncJob != null) { - isoData.add(new Pair(BaseCmd.Properties.JOB_ID.getName(), asyncJob.getId().toString())); - isoData.add(new Pair(BaseCmd.Properties.JOB_STATUS.getName(), String.valueOf(asyncJob.getStatus()))); + isoResponse.setJobId(asyncJob.getId()); + isoResponse.setJobStatus(asyncJob.getStatus()); } - isoTagList.add(isoData); - } + response.add(isoResponse); + } } - - Object[] iTag = new Object[isoTagList.size()]; - for (int i = 0; i < isoTagList.size(); i++) { - iTag[i] = isoTagList.get(i); - } - - Pair isoTag = new Pair("iso", iTag); - isoTags.add(isoTag); - - return isoTags; + + return SerializerHelper.toSerializedString(response); } } diff --git a/server/src/com/cloud/api/commands/ListTemplatesCmd.java b/server/src/com/cloud/api/commands/ListTemplatesCmd.java index 9faec83e3dc..f316c41534a 100644 --- a/server/src/com/cloud/api/commands/ListTemplatesCmd.java +++ b/server/src/com/cloud/api/commands/ListTemplatesCmd.java @@ -20,16 +20,17 @@ package com.cloud.api.commands; import java.util.ArrayList; import java.util.List; -import java.util.Map; import org.apache.log4j.Logger; -import com.cloud.api.BaseCmd; +import com.cloud.api.BaseListCmd; +import com.cloud.api.Implementation; import com.cloud.api.Parameter; -import com.cloud.api.ServerApiException; +import com.cloud.api.response.TemplateResponse; import com.cloud.async.AsyncJobVO; import com.cloud.dc.DataCenterVO; import com.cloud.host.HostVO; +import com.cloud.serializer.SerializerHelper; import com.cloud.storage.GuestOS; import com.cloud.storage.VMTemplateHostVO; import com.cloud.storage.VMTemplateStorageResourceAssoc.Status; @@ -37,29 +38,13 @@ import com.cloud.storage.VMTemplateVO; import com.cloud.storage.dao.VMTemplateDao.TemplateFilter; import com.cloud.storage.template.TemplateConstants; import com.cloud.user.Account; -import com.cloud.utils.Pair; +import com.cloud.user.UserContext; -public class ListTemplatesCmd extends BaseCmd { +@Implementation(method="listTemplates") +public class ListTemplatesCmd extends BaseListCmd { public static final Logger s_logger = Logger.getLogger(ListTemplatesCmd.class.getName()); private static final String s_name = "listtemplatesresponse"; - private static final List> s_properties = new ArrayList>(); - - static { - s_properties.add(new Pair(BaseCmd.Properties.ACCOUNT, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.DOMAIN_ID, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.ID, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.NAME, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.IS_PUBLIC, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.IS_READY, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.TEMPLATE_FILTER, Boolean.TRUE)); - s_properties.add(new Pair(BaseCmd.Properties.ZONE_ID, Boolean.FALSE)); - - s_properties.add(new Pair(BaseCmd.Properties.ACCOUNT_OBJ, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.KEYWORD, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.PAGE, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.PAGESIZE, Boolean.FALSE)); - } ///////////////////////////////////////////////////// //////////////// API parameters ///////////////////// @@ -119,135 +104,90 @@ public class ListTemplatesCmd extends BaseCmd { public String getName() { return s_name; } - @Override - public List> getProperties() { - return s_properties; - } - @Override - public List> execute(Map params) { - Account account = (Account)params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName()); - String accountName = (String)params.get(BaseCmd.Properties.ACCOUNT.getName()); - Long domainId = (Long)params.get(BaseCmd.Properties.DOMAIN_ID.getName()); - Long id = (Long) params.get(BaseCmd.Properties.ID.getName()); - String name = (String) params.get(BaseCmd.Properties.NAME.getName()); - String templateFilterString = (String) params.get(BaseCmd.Properties.TEMPLATE_FILTER.getName()); - String keyword = (String)params.get(BaseCmd.Properties.KEYWORD.getName()); - Integer page = (Integer)params.get(BaseCmd.Properties.PAGE.getName()); - Integer pageSize = (Integer)params.get(BaseCmd.Properties.PAGESIZE.getName()); - Long zoneId = (Long)params.get(BaseCmd.Properties.ZONE_ID.getName()); - - boolean isAdmin = false; - Long accountId = null; - if ((account == null) || account.getType() == Account.ACCOUNT_TYPE_ADMIN) { - isAdmin = true; - if (domainId != null) { - if ((account != null) && !getManagementServer().isChildDomain(account.getDomainId(), domainId)) { - throw new ServerApiException(BaseCmd.PARAM_ERROR, "Invalid domain id (" + domainId + ") given, unable to list templates."); - } - - if (accountName != null) { - account = getManagementServer().findActiveAccount(accountName, domainId); - if (account == null) { - throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "Unable to find account " + accountName + " in domain " + domainId); - } - accountId = account.getId(); - } - } - } else { - accountId = account.getId(); - accountName = account.getAccountName(); - domainId = account.getDomainId(); - } - - TemplateFilter templateFilter; + @Override @SuppressWarnings("unchecked") + public String getResponse() { + TemplateFilter templateFilterObj; try { - templateFilter = TemplateFilter.valueOf(templateFilterString); + templateFilterObj = TemplateFilter.valueOf(templateFilter); } catch (IllegalArgumentException e) { - throw new ServerApiException(BaseCmd.PARAM_ERROR, "Please specify a valid template filter."); + // how did we get this far? The request should've been rejected already before the response stage... + templateFilterObj = TemplateFilter.selfexecutable; } - Long startIndex = Long.valueOf(0); - int pageSizeNum = 50; - if (pageSize != null) { - pageSizeNum = pageSize.intValue(); - } - if (page != null) { - int pageNum = page.intValue(); - if (pageNum > 0) { - startIndex = Long.valueOf(pageSizeNum * (pageNum-1)); + boolean isAdmin = false; + boolean isAccountSpecific = true; + Account account = (Account)UserContext.current().getAccountObject(); + if ((account == null) || (account.getType() == Account.ACCOUNT_TYPE_ADMIN) || (account.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN)) { + isAdmin = true; + if ((accountName == null) || (domainId == null)) { + isAccountSpecific = false; } } - - boolean onlyReady = (templateFilter == TemplateFilter.featured) || - (templateFilter == TemplateFilter.selfexecutable) || - (templateFilter == TemplateFilter.sharedexecutable) || - (templateFilter == TemplateFilter.executable && accountId != null) || - (templateFilter == TemplateFilter.community); - boolean showDomr = (templateFilter != TemplateFilter.selfexecutable); - - List templates = null; - try { - templates = getManagementServer().listTemplates(id, name, keyword, templateFilter, false, null, accountId, pageSize, startIndex, zoneId); - } catch (Exception e) { - throw new ServerApiException(BaseCmd.INTERNAL_ERROR, e.getMessage()); - } - - List tTagList = new ArrayList(); - List> templateTags = new ArrayList>(); + boolean onlyReady = (templateFilterObj == TemplateFilter.featured) || + (templateFilterObj == TemplateFilter.selfexecutable) || + (templateFilterObj == TemplateFilter.sharedexecutable) || + (templateFilterObj == TemplateFilter.executable && isAccountSpecific) || + (templateFilterObj == TemplateFilter.community); + + boolean showDomr = (templateFilterObj != TemplateFilter.selfexecutable); + + // get the response + List templates = (List)getResponseObject(); + List response = new ArrayList(); + for (VMTemplateVO template : templates) { - if (!showDomr && template.getId() == TemplateConstants.DEFAULT_SYSTEM_VM_DB_ID) { - continue; - } - - List templateHostRefsForTemplate = getManagementServer().listTemplateHostBy(template.getId(), zoneId); - - for (VMTemplateHostVO templateHostRef : templateHostRefsForTemplate) { - if (onlyReady && templateHostRef.getDownloadState() != Status.DOWNLOADED) { - continue; - } - - List> templateData = new ArrayList>(); - templateData.add(new Pair(BaseCmd.Properties.ID.getName(), template.getId().toString())); - templateData.add(new Pair(BaseCmd.Properties.NAME.getName(), template.getName())); - templateData.add(new Pair(BaseCmd.Properties.DISPLAY_TEXT.getName(), template.getDisplayText())); - templateData.add(new Pair(BaseCmd.Properties.IS_PUBLIC.getName(), Boolean.valueOf(template.isPublicTemplate()).toString())); - templateData.add(new Pair(BaseCmd.Properties.CREATED.getName(), getDateString(templateHostRef.getCreated()))); - if(template.getRemoved() != null){ - templateData.add(new Pair(BaseCmd.Properties.REMOVED.getName(), getDateString(template.getRemoved()))); + if (!showDomr && template.getId() == TemplateConstants.DEFAULT_SYSTEM_VM_DB_ID) { + continue; + } + + List templateHostRefsForTemplate = getManagementServer().listTemplateHostBy(template.getId(), zoneId); + + for (VMTemplateHostVO templateHostRef : templateHostRefsForTemplate) { + if (onlyReady && templateHostRef.getDownloadState() != Status.DOWNLOADED) { + continue; } - templateData.add(new Pair(BaseCmd.Properties.IS_READY.getName(), Boolean.valueOf(templateHostRef.getDownloadState()==Status.DOWNLOADED).toString())); - templateData.add(new Pair(BaseCmd.Properties.IS_FEATURED.getName(), Boolean.valueOf(template.isFeatured()).toString())); - templateData.add(new Pair(BaseCmd.Properties.PASSWORD_ENABLED.getName(), Boolean.valueOf(template.getEnablePassword()).toString())); - templateData.add(new Pair(BaseCmd.Properties.CROSS_ZONES.getName(), Boolean.valueOf(template.isCrossZones()).toString())); - templateData.add(new Pair(BaseCmd.Properties.FORMAT.getName(), template.getFormat())); + + TemplateResponse templateResponse = new TemplateResponse(); + templateResponse.setId(template.getId()); + templateResponse.setName(template.getName()); + templateResponse.setDisplayText(template.getDisplayText()); + templateResponse.setPublic(template.isPublicTemplate()); + templateResponse.setCreated(templateHostRef.getCreated()); + if (template.getRemoved() != null) { + templateResponse.setRemoved(template.getRemoved()); + } + templateResponse.setReady(templateHostRef.getDownloadState()==Status.DOWNLOADED); + templateResponse.setFeatured(template.isFeatured()); + templateResponse.setPasswordEnabled(template.getEnablePassword()); + templateResponse.setCrossZones(template.isCrossZones()); + templateResponse.setFormat(template.getFormat()); GuestOS os = getManagementServer().findGuestOSById(template.getGuestOSId()); if (os != null) { - templateData.add(new Pair(BaseCmd.Properties.OS_TYPE_ID.getName(), os.getId())); - templateData.add(new Pair(BaseCmd.Properties.OS_TYPE_NAME.getName(), os.getDisplayName())); + templateResponse.setOsTypeId(os.getId()); + templateResponse.setOsTypeName(os.getDisplayName()); } else { - templateData.add(new Pair(BaseCmd.Properties.OS_TYPE_ID.getName(), -1)); - templateData.add(new Pair(BaseCmd.Properties.OS_TYPE_NAME.getName(), "")); + templateResponse.setOsTypeId(-1L); + templateResponse.setOsTypeName(""); } // add account ID and name Account owner = getManagementServer().findAccountById(template.getAccountId()); if (owner != null) { - templateData.add(new Pair(BaseCmd.Properties.ACCOUNT.getName(), owner.getAccountName())); - templateData.add(new Pair(BaseCmd.Properties.DOMAIN_ID.getName(), owner.getDomainId())); - templateData.add(new Pair(BaseCmd.Properties.DOMAIN.getName(), getManagementServer().findDomainIdById(owner.getDomainId()).getName())); + templateResponse.setAccount(owner.getAccountName()); + templateResponse.setDomainId(owner.getDomainId()); + templateResponse.setDomainName(getManagementServer().findDomainIdById(owner.getDomainId()).getName()); } HostVO host = getManagementServer().getHostBy(templateHostRef.getHostId()); + DataCenterVO datacenter = getManagementServer().getDataCenterBy(host.getDataCenterId()); // Add the zone ID - templateData.add(new Pair(BaseCmd.Properties.ZONE_ID.getName(), host.getDataCenterId())); - - DataCenterVO datacenter = getManagementServer().getDataCenterBy(host.getDataCenterId()); - templateData.add(new Pair(BaseCmd.Properties.ZONE_NAME.getName(), datacenter.getName())); - + templateResponse.setZoneId(host.getDataCenterId()); + templateResponse.setZoneName(datacenter.getName()); + // If the user is an admin, add the template download status if (isAdmin || account.getId().longValue() == template.getAccountId()) { // add download status @@ -262,37 +202,29 @@ public class ListTemplatesCmd extends BaseCmd { } else { templateStatus = templateHostRef.getErrorString(); } - templateData.add(new Pair(BaseCmd.Properties.TEMPLATE_STATUS.getName(), templateStatus)); + templateResponse.setStatus(templateStatus); } else if (templateHostRef.getDownloadState() == VMTemplateHostVO.Status.DOWNLOADED) { - templateData.add(new Pair(BaseCmd.Properties.TEMPLATE_STATUS.getName(), "Download Complete")); + templateResponse.setStatus("Download Complete"); } else { - templateData.add(new Pair(BaseCmd.Properties.TEMPLATE_STATUS.getName(), "Successfully Installed")); + templateResponse.setStatus("Successfully Installed"); } } long templateSize = templateHostRef.getSize(); if (templateSize > 0) { - templateData.add(new Pair(BaseCmd.Properties.SIZE.getName(), templateSize)); + templateResponse.setSize(templateSize); } AsyncJobVO asyncJob = getManagementServer().findInstancePendingAsyncJob("vm_template", template.getId()); - if(asyncJob != null) { - templateData.add(new Pair(BaseCmd.Properties.JOB_ID.getName(), asyncJob.getId().toString())); - templateData.add(new Pair(BaseCmd.Properties.JOB_STATUS.getName(), String.valueOf(asyncJob.getStatus()))); + if (asyncJob != null) { + templateResponse.setJobId(asyncJob.getId()); + templateResponse.setJobStatus(asyncJob.getStatus()); } - - tTagList.add(templateData); - } - } - - Object[] tTag = new Object[tTagList.size()]; - for (int i = 0; i < tTagList.size(); i++) { - tTag[i] = tTagList.get(i); - } - - Pair templateTag = new Pair("template", tTag); - templateTags.add(templateTag); - return templateTags; + response.add(templateResponse); + } + } + + return SerializerHelper.toSerializedString(response); } } diff --git a/server/src/com/cloud/api/response/TemplateResponse.java b/server/src/com/cloud/api/response/TemplateResponse.java index 6801d15ed33..f98f6dd4c85 100644 --- a/server/src/com/cloud/api/response/TemplateResponse.java +++ b/server/src/com/cloud/api/response/TemplateResponse.java @@ -4,6 +4,7 @@ import java.util.Date; import com.cloud.api.ResponseObject; import com.cloud.serializer.Param; +import com.cloud.storage.Storage.ImageFormat; public class TemplateResponse implements ResponseObject { @Param(name="id") @@ -21,12 +22,24 @@ public class TemplateResponse implements ResponseObject { @Param(name="created") private Date created; + @Param(name="removed") + private Date removed; + @Param(name="isready", propName="ready") private boolean isReady; @Param(name="passwordenabled") private boolean passwordEnabled; + @Param(name="format") + private ImageFormat format; + + @Param(name="bootable") + private boolean bootable; + + @Param(name="isfeatured") + private boolean featured; + @Param(name="crossZones") private boolean crossZones; @@ -48,6 +61,18 @@ public class TemplateResponse implements ResponseObject { @Param(name="zonename") private String zoneName; + @Param(name="status") + private String status; + + @Param(name="size") + private Long size; + + @Param(name="jobid") + private Long jobId; + + @Param(name="jobstatus") + private Integer jobStatus; + @Param(name="domain") private String domainName; @@ -142,6 +167,14 @@ public class TemplateResponse implements ResponseObject { this.created = created; } + public Date getRemoved() { + return removed; + } + + public void setRemoved(Date removed) { + this.removed = removed; + } + public boolean isReady() { return isReady; } @@ -158,6 +191,30 @@ public class TemplateResponse implements ResponseObject { this.passwordEnabled = passwordEnabled; } + public ImageFormat getFormat() { + return format; + } + + public void setFormat(ImageFormat format) { + this.format = format; + } + + public boolean isBootable() { + return bootable; + } + + public void setBootable(boolean bootable) { + this.bootable = bootable; + } + + public boolean isFeatured() { + return featured; + } + + public void setFeatured(boolean featured) { + this.featured = featured; + } + public boolean isCrossZones() { return crossZones; } @@ -166,6 +223,38 @@ public class TemplateResponse implements ResponseObject { this.crossZones = crossZones; } + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } + + public Long getSize() { + return size; + } + + public void setSize(Long size) { + this.size = size; + } + + public Long getJobId() { + return jobId; + } + + public void setJobId(Long jobId) { + this.jobId = jobId; + } + + public Integer getJobStatus() { + return jobStatus; + } + + public void setJobStatus(Integer jobStatus) { + this.jobStatus = jobStatus; + } + public long getDomainId() { return domainId; } diff --git a/server/src/com/cloud/server/ManagementServer.java b/server/src/com/cloud/server/ManagementServer.java index 8a94022172b..b44a0b6f4cf 100644 --- a/server/src/com/cloud/server/ManagementServer.java +++ b/server/src/com/cloud/server/ManagementServer.java @@ -43,6 +43,8 @@ import com.cloud.api.commands.ListEventsCmd; import com.cloud.api.commands.ListGuestOsCategoriesCmd; import com.cloud.api.commands.ListGuestOsCmd; import com.cloud.api.commands.ListHostsCmd; +import com.cloud.api.commands.ListIsosCmd; +import com.cloud.api.commands.ListTemplatesCmd; import com.cloud.api.commands.LockAccountCmd; import com.cloud.api.commands.LockUserCmd; import com.cloud.api.commands.RebootSystemVmCmd; @@ -104,7 +106,6 @@ import com.cloud.storage.VMTemplateHostVO; import com.cloud.storage.VMTemplateVO; import com.cloud.storage.VolumeStats; import com.cloud.storage.VolumeVO; -import com.cloud.storage.dao.VMTemplateDao.TemplateFilter; import com.cloud.storage.preallocatedlun.PreallocatedLunVO; import com.cloud.user.Account; import com.cloud.user.AccountVO; @@ -1628,20 +1629,18 @@ public interface ManagementServer { List listPermittedTemplates(long accountId); /** - * Lists templates that match the specified criteria - * @param templateId - (optional) id of the template to return template host references for - * @param name a name (possibly partial) to search for - * @param keyword a keyword (using partial match) to search for, currently only searches name - * @param templateFilter - the category of template to return - * @param isIso whether this is an ISO search or non-ISO search - * @param bootable if null will return both bootable and non-bootable ISOs, else will return only one or the other, depending on the boolean value - * @param accountId parameter to use when searching for owner of template - * @param pageSize size of search results - * @param startIndex index in to search results to use - * @param zoneId optional zoneid to limit search - * @return list of templates + * List ISOs that match the specified criteria. + * @param cmd The command that wraps the (optional) templateId, name, keyword, templateFilter, bootable, account, and zoneId parameters. + * @return list of ISOs */ - List listTemplates(Long templateId, String name, String keyword, TemplateFilter templateFilter, boolean isIso, Boolean bootable, Long accountId, Integer pageSize, Long startIndex, Long zoneId) throws InvalidParameterValueException; + List listIsos(ListIsosCmd cmd) throws IllegalArgumentException, InvalidParameterValueException; + + /** + * List templates that match the specified criteria. + * @param cmd The command that wraps the (optional) templateId, name, keyword, templateFilter, bootable, account, and zoneId parameters. + * @return list of ISOs + */ + List listTemplates(ListTemplatesCmd cmd) throws IllegalArgumentException, InvalidParameterValueException; /** * Search for disk offerings based on search criteria diff --git a/server/src/com/cloud/server/ManagementServerImpl.java b/server/src/com/cloud/server/ManagementServerImpl.java index f5b48681436..8a73c139f8e 100755 --- a/server/src/com/cloud/server/ManagementServerImpl.java +++ b/server/src/com/cloud/server/ManagementServerImpl.java @@ -54,7 +54,6 @@ import com.cloud.alert.AlertVO; import com.cloud.alert.dao.AlertDao; import com.cloud.api.BaseCmd; import com.cloud.api.ServerApiException; -import com.cloud.api.commands.AssociateIPAddrCmd; import com.cloud.api.commands.AuthorizeNetworkGroupIngressCmd; import com.cloud.api.commands.CancelMaintenanceCmd; import com.cloud.api.commands.CancelPrimaryStorageMaintenanceCmd; @@ -82,6 +81,8 @@ import com.cloud.api.commands.ListEventsCmd; import com.cloud.api.commands.ListGuestOsCategoriesCmd; import com.cloud.api.commands.ListGuestOsCmd; import com.cloud.api.commands.ListHostsCmd; +import com.cloud.api.commands.ListIsosCmd; +import com.cloud.api.commands.ListTemplatesCmd; import com.cloud.api.commands.LockAccountCmd; import com.cloud.api.commands.LockUserCmd; import com.cloud.api.commands.PrepareForMaintenanceCmd; @@ -114,21 +115,20 @@ import com.cloud.async.executor.DeleteDomainParam; import com.cloud.async.executor.DeleteRuleParam; import com.cloud.async.executor.DeleteTemplateParam; import com.cloud.async.executor.DeployVMParam; -import com.cloud.async.executor.DisassociateIpAddressParam; import com.cloud.async.executor.LoadBalancerParam; import com.cloud.async.executor.NetworkGroupIngressParam; import com.cloud.async.executor.SecurityGroupParam; import com.cloud.async.executor.UpdateLoadBalancerParam; import com.cloud.async.executor.VMOperationParam; -import com.cloud.async.executor.VolumeOperationParam; import com.cloud.async.executor.VMOperationParam.VmOp; +import com.cloud.async.executor.VolumeOperationParam; import com.cloud.async.executor.VolumeOperationParam.VolumeOp; import com.cloud.capacity.CapacityVO; import com.cloud.capacity.dao.CapacityDao; import com.cloud.configuration.ConfigurationManager; import com.cloud.configuration.ConfigurationVO; -import com.cloud.configuration.ResourceLimitVO; import com.cloud.configuration.ResourceCount.ResourceType; +import com.cloud.configuration.ResourceLimitVO; import com.cloud.configuration.dao.ConfigurationDao; import com.cloud.configuration.dao.ResourceLimitDao; import com.cloud.consoleproxy.ConsoleProxyManager; @@ -138,8 +138,8 @@ import com.cloud.dc.DataCenterIpAddressVO; import com.cloud.dc.DataCenterVO; import com.cloud.dc.HostPodVO; import com.cloud.dc.PodVlanMapVO; -import com.cloud.dc.VlanVO; import com.cloud.dc.Vlan.VlanType; +import com.cloud.dc.VlanVO; import com.cloud.dc.dao.AccountVlanMapDao; import com.cloud.dc.dao.ClusterDao; import com.cloud.dc.dao.DataCenterDao; @@ -203,21 +203,21 @@ import com.cloud.storage.GuestOSCategoryVO; import com.cloud.storage.GuestOSVO; import com.cloud.storage.LaunchPermissionVO; import com.cloud.storage.Snapshot; +import com.cloud.storage.Snapshot.SnapshotType; import com.cloud.storage.SnapshotPolicyVO; import com.cloud.storage.SnapshotScheduleVO; import com.cloud.storage.SnapshotVO; import com.cloud.storage.Storage; +import com.cloud.storage.Storage.ImageFormat; import com.cloud.storage.StorageManager; import com.cloud.storage.StoragePoolVO; import com.cloud.storage.StorageStats; import com.cloud.storage.VMTemplateHostVO; import com.cloud.storage.VMTemplateStorageResourceAssoc; import com.cloud.storage.VMTemplateVO; +import com.cloud.storage.Volume.VolumeType; import com.cloud.storage.VolumeStats; import com.cloud.storage.VolumeVO; -import com.cloud.storage.Snapshot.SnapshotType; -import com.cloud.storage.Storage.ImageFormat; -import com.cloud.storage.Volume.VolumeType; import com.cloud.storage.dao.DiskOfferingDao; import com.cloud.storage.dao.DiskTemplateDao; import com.cloud.storage.dao.GuestOSCategoryDao; @@ -227,9 +227,9 @@ import com.cloud.storage.dao.SnapshotDao; import com.cloud.storage.dao.SnapshotPolicyDao; import com.cloud.storage.dao.StoragePoolDao; import com.cloud.storage.dao.VMTemplateDao; +import com.cloud.storage.dao.VMTemplateDao.TemplateFilter; import com.cloud.storage.dao.VMTemplateHostDao; import com.cloud.storage.dao.VolumeDao; -import com.cloud.storage.dao.VMTemplateDao.TemplateFilter; import com.cloud.storage.preallocatedlun.PreallocatedLunVO; import com.cloud.storage.preallocatedlun.dao.PreallocatedLunDao; import com.cloud.storage.secondary.SecondaryStorageVmManager; @@ -251,12 +251,12 @@ import com.cloud.user.dao.UserDao; import com.cloud.user.dao.UserStatisticsDao; import com.cloud.uservm.UserVm; import com.cloud.utils.DateUtil; +import com.cloud.utils.DateUtil.IntervalType; import com.cloud.utils.EnumUtils; import com.cloud.utils.NumbersUtil; import com.cloud.utils.Pair; import com.cloud.utils.PasswordGenerator; import com.cloud.utils.StringUtils; -import com.cloud.utils.DateUtil.IntervalType; import com.cloud.utils.component.Adapters; import com.cloud.utils.component.ComponentLocator; import com.cloud.utils.concurrency.NamedThreadFactory; @@ -4181,7 +4181,66 @@ public class ManagementServerImpl implements ManagementServer { } @Override - public List listTemplates(Long templateId, String name, String keyword, TemplateFilter templateFilter, boolean isIso, Boolean bootable, Long accountId, Integer pageSize, Long startIndex, Long zoneId) throws InvalidParameterValueException { + public List listIsos(ListIsosCmd cmd) throws IllegalArgumentException, InvalidParameterValueException { + TemplateFilter isoFilter = TemplateFilter.valueOf(cmd.getIsoFilter()); + Long accountId = null; + Account account = (Account)UserContext.current().getAccountObject(); + Long domainId = cmd.getDomainId(); + String accountName = cmd.getAccountName(); + if ((account == null) || (account.getType() == Account.ACCOUNT_TYPE_ADMIN)) { + // validate domainId before proceeding + if ((domainId != null) && (accountName != null)) { + if ((account != null) && !_domainDao.isChildDomain(account.getDomainId(), domainId)) { + throw new InvalidParameterValueException("Invalid domain id (" + domainId + ") given, unable to list events."); + } + + Account userAccount = _accountDao.findActiveAccount(accountName, domainId); + if (userAccount != null) { + accountId = userAccount.getId(); + } else { + throw new InvalidParameterValueException("Failed to list ISOs. Unable to find account " + accountName + " in domain " + domainId); + } + } else if (account != null) { + accountId = account.getId(); + } + } else { + accountId = account.getId(); + } + + return listTemplates(cmd.getId(), cmd.getIsoName(), cmd.getKeyword(), isoFilter, true, cmd.isBootable(), accountId, cmd.getPageSizeVal().intValue(), cmd.getStartIndex(), cmd.getZoneId()); + } + + @Override + public List listTemplates(ListTemplatesCmd cmd) throws IllegalArgumentException, InvalidParameterValueException { + TemplateFilter templateFilter = TemplateFilter.valueOf(cmd.getTemplateFilter()); + Long accountId = null; + Account account = (Account)UserContext.current().getAccountObject(); + Long domainId = cmd.getDomainId(); + String accountName = cmd.getAccountName(); + if ((account == null) || (account.getType() == Account.ACCOUNT_TYPE_ADMIN)) { + // validate domainId before proceeding + if ((domainId != null) && (accountName != null)) { + if ((account != null) && !_domainDao.isChildDomain(account.getDomainId(), domainId)) { + throw new InvalidParameterValueException("Invalid domain id (" + domainId + ") given, unable to list events."); + } + + Account userAccount = _accountDao.findActiveAccount(accountName, domainId); + if (userAccount != null) { + accountId = userAccount.getId(); + } else { + throw new InvalidParameterValueException("Failed to list ISOs. Unable to find account " + accountName + " in domain " + domainId); + } + } else if (account != null) { + accountId = account.getId(); + } + } else { + accountId = account.getId(); + } + + return listTemplates(cmd.getId(), cmd.getTemplateName(), cmd.getKeyword(), templateFilter, false, null, accountId, cmd.getPageSizeVal().intValue(), cmd.getStartIndex(), cmd.getZoneId()); + } + + private List listTemplates(Long templateId, String name, String keyword, TemplateFilter templateFilter, boolean isIso, Boolean bootable, Long accountId, Integer pageSize, Long startIndex, Long zoneId) throws InvalidParameterValueException { VMTemplateVO template = null; if (templateId != null) { template = _templateDao.findById(templateId);