diff --git a/server/src/com/cloud/api/commands/DeleteTemplateCmd.java b/server/src/com/cloud/api/commands/DeleteTemplateCmd.java index b1196c8c490..6eb0bdcdd83 100644 --- a/server/src/com/cloud/api/commands/DeleteTemplateCmd.java +++ b/server/src/com/cloud/api/commands/DeleteTemplateCmd.java @@ -18,31 +18,18 @@ 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.Implementation; import com.cloud.api.Parameter; -import com.cloud.api.ServerApiException; -import com.cloud.storage.VMTemplateVO; -import com.cloud.user.Account; -import com.cloud.utils.Pair; - +import com.cloud.api.BaseCmd.Manager; + +@Implementation(method="deleteTemplate", manager=Manager.TemplateManager) public class DeleteTemplateCmd extends BaseCmd { public static final Logger s_logger = Logger.getLogger(DeleteTemplateCmd.class.getName()); private static final String s_name = "deletetemplateresponse"; - private static final List> s_properties = new ArrayList>(); - - static { - s_properties.add(new Pair(BaseCmd.Properties.ACCOUNT_OBJ, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.USER_ID, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.ID, Boolean.TRUE)); - s_properties.add(new Pair(BaseCmd.Properties.ZONE_ID, Boolean.FALSE)); - } - + ///////////////////////////////////////////////////// //////////////// API parameters ///////////////////// ///////////////////////////////////////////////////// @@ -78,61 +65,62 @@ public class DeleteTemplateCmd extends BaseCmd { public static String getStaticName() { return s_name; - } + } + + @Override + public String getResponse() { + // TODO Auto-generated method stub + return null; + } - @Override - public List> getProperties() { - return s_properties; - } - - @Override - public List> execute(Map params) { - Long templateId = (Long)params.get(BaseCmd.Properties.ID.getName()); - Long userId = (Long)params.get(BaseCmd.Properties.USER_ID.getName()); - Account account = (Account)params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName()); - Long zoneId = (Long)params.get(BaseCmd.Properties.ZONE_ID.getName()); - - if (userId == null) { - userId = Long.valueOf(1); - } - - VMTemplateVO template = getManagementServer().findTemplateById(templateId.longValue()); - if (template == null) { - throw new ServerApiException(BaseCmd.PARAM_ERROR, "unable to find template with id " + templateId); - } - - if (account != null) { - if (!isAdmin(account.getType())) { - if (template.getAccountId() != account.getId()) { - throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "unable to delete template with id " + templateId); - } - } else { - Account templateOwner = getManagementServer().findAccountById(template.getAccountId()); - if ((templateOwner == null) || !getManagementServer().isChildDomain(account.getDomainId(), templateOwner.getDomainId())) { - throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "Unable to delete template with id " + templateId + ", permission denied."); - } - } - } - - try { - long jobId = getManagementServer().deleteTemplateAsync(userId, templateId, zoneId); - - if (jobId == 0) { - s_logger.warn("Unable to schedule async-job for DeleteTemplate command"); - } else { - if(s_logger.isDebugEnabled()) { - s_logger.debug("DeleteTemplate command has been accepted, job id: " + jobId); - } - } - - List> returnValues = new ArrayList>(); - returnValues.add(new Pair(BaseCmd.Properties.JOB_ID.getName(), Long.valueOf(jobId))); - returnValues.add(new Pair(BaseCmd.Properties.TEMPLATE_ID.getName(), Long.valueOf(templateId))); - - return returnValues; - } catch (Exception ex) { - throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to delete template: " + ex.getMessage()); - } - - } +// @Override +// public List> execute(Map params) { +// Long templateId = (Long)params.get(BaseCmd.Properties.ID.getName()); +// Long userId = (Long)params.get(BaseCmd.Properties.USER_ID.getName()); +// Account account = (Account)params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName()); +// Long zoneId = (Long)params.get(BaseCmd.Properties.ZONE_ID.getName()); +// +// if (userId == null) { +// userId = Long.valueOf(1); +// } +// +// VMTemplateVO template = getManagementServer().findTemplateById(templateId.longValue()); +// if (template == null) { +// throw new ServerApiException(BaseCmd.PARAM_ERROR, "unable to find template with id " + templateId); +// } +// +// if (account != null) { +// if (!isAdmin(account.getType())) { +// if (template.getAccountId() != account.getId()) { +// throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "unable to delete template with id " + templateId); +// } +// } else { +// Account templateOwner = getManagementServer().findAccountById(template.getAccountId()); +// if ((templateOwner == null) || !getManagementServer().isChildDomain(account.getDomainId(), templateOwner.getDomainId())) { +// throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "Unable to delete template with id " + templateId + ", permission denied."); +// } +// } +// } +// +// try { +// long jobId = getManagementServer().deleteTemplateAsync(userId, templateId, zoneId); +// +// if (jobId == 0) { +// s_logger.warn("Unable to schedule async-job for DeleteTemplate command"); +// } else { +// if(s_logger.isDebugEnabled()) { +// s_logger.debug("DeleteTemplate command has been accepted, job id: " + jobId); +// } +// } +// +// List> returnValues = new ArrayList>(); +// returnValues.add(new Pair(BaseCmd.Properties.JOB_ID.getName(), Long.valueOf(jobId))); +// returnValues.add(new Pair(BaseCmd.Properties.TEMPLATE_ID.getName(), Long.valueOf(templateId))); +// +// return returnValues; +// } catch (Exception ex) { +// throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to delete template: " + ex.getMessage()); +// } +// +// } } diff --git a/server/src/com/cloud/template/TemplateManager.java b/server/src/com/cloud/template/TemplateManager.java index 8ac0dc03943..cdbbd3f673f 100644 --- a/server/src/com/cloud/template/TemplateManager.java +++ b/server/src/com/cloud/template/TemplateManager.java @@ -22,6 +22,7 @@ import java.net.URISyntaxException; import java.util.List; import com.cloud.api.commands.CreateTemplateCmd; +import com.cloud.api.commands.DeleteTemplateCmd; import com.cloud.api.commands.DetachIsoCmd; import com.cloud.api.commands.RegisterIsoCmd; import com.cloud.api.commands.RegisterTemplateCmd; @@ -135,5 +136,6 @@ public interface TemplateManager extends Manager { void evictTemplateFromStoragePool(VMTemplateStoragePoolVO templatePoolVO); boolean templateIsDeleteable(VMTemplateHostVO templateHostRef); - + + boolean deleteTemplate(DeleteTemplateCmd cmd) throws InvalidParameterValueException, InternalErrorException; } diff --git a/server/src/com/cloud/template/TemplateManagerImpl.java b/server/src/com/cloud/template/TemplateManagerImpl.java index 14741a5cd99..91c04c6f28a 100644 --- a/server/src/com/cloud/template/TemplateManagerImpl.java +++ b/server/src/com/cloud/template/TemplateManagerImpl.java @@ -40,6 +40,7 @@ import com.cloud.agent.api.storage.PrimaryStorageDownloadCommand; import com.cloud.agent.manager.AgentManager; import com.cloud.api.BaseCmd; import com.cloud.api.ServerApiException; +import com.cloud.api.commands.DeleteTemplateCmd; import com.cloud.api.commands.DetachIsoCmd; import com.cloud.api.commands.RegisterIsoCmd; import com.cloud.api.commands.RegisterTemplateCmd; @@ -892,8 +893,10 @@ public class TemplateManagerImpl implements TemplateManager { if (vmInstanceCheck == null) { throw new ServerApiException (BaseCmd.VM_INVALID_PARAM_ERROR, "Unable to find a virtual machine with id " + vmId); } + + String errMsg = "Unable to detach ISO from virtual machine "; - userId = accountAndUserValidation(account, userId, vmInstanceCheck); + userId = accountAndUserValidation(account, userId, vmInstanceCheck, null, errMsg); UserVm userVM = _userVmDao.findById(vmId); if (userVM == null) { @@ -950,15 +953,14 @@ public class TemplateManagerImpl implements TemplateManager { return success; } - private Long accountAndUserValidation(Account account, Long userId, - UserVmVO vmInstanceCheck) { + private Long accountAndUserValidation(Account account, Long userId, UserVmVO vmInstanceCheck, VMTemplateVO template, String msg) { if (account != null) { if (!isAdmin(account.getType())) { if (account.getId().longValue() != vmInstanceCheck.getAccountId()) { - throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "Unable to detach ISO from virtual machine " + vmInstanceCheck.getName() + " for this account"); + throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, msg + vmInstanceCheck!=null ? vmInstanceCheck.getName():template.getName() + " for this account"); } } else if (!_domainDao.isChildDomain(account.getDomainId(), vmInstanceCheck.getDomainId())) { - throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "Unable to detach ISO from virtual machine " + vmInstanceCheck.getName() + ", permission denied."); + throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, msg + vmInstanceCheck!=null ? vmInstanceCheck.getName():template.getName() + ", permission denied."); } } @@ -974,4 +976,40 @@ public class TemplateManagerImpl implements TemplateManager { (accountType == Account.ACCOUNT_TYPE_DOMAIN_ADMIN) || (accountType == Account.ACCOUNT_TYPE_READ_ONLY_ADMIN)); } + + @Override + public boolean deleteTemplate(DeleteTemplateCmd cmd) throws InvalidParameterValueException, InternalErrorException{ + + Long templateId = cmd.getId(); + Long userId = UserContext.current().getUserId(); + Account account = (Account)UserContext.current().getAccountObject(); + Long zoneId = (Long)cmd.getZoneId(); + + VMTemplateVO template = _tmpltDao.findById(templateId.longValue()); + if (template == null) { + throw new ServerApiException(BaseCmd.PARAM_ERROR, "unable to find template with id " + templateId); + } + + userId = accountAndUserValidation(account, userId, null, template, "Unable to delete template " ); + + UserVO user = _userDao.findById(userId); + if (user == null) { + throw new InvalidParameterValueException("Please specify a valid user."); + } + + if (template.getFormat() == ImageFormat.ISO) { + throw new InvalidParameterValueException("Please specify a valid template."); + } + + if (template.getUniqueName().equals("routing")) { + throw new InvalidParameterValueException("The DomR template cannot be deleted."); + } + + if (zoneId != null && (_hostDao.findSecondaryStorageHost(zoneId) == null)) { + throw new InvalidParameterValueException("Failed to find a secondary storage host in the specified zone."); + } + + long eventId = EventUtils.saveScheduledEvent(userId, account.getId(), EventTypes.EVENT_TEMPLATE_DELETE, "Scheduling the template for deletion"); + return delete(userId, templateId, zoneId, eventId); + } }