mirror of https://github.com/apache/cloudstack.git
Refactored the delete template command
This commit is contained in:
parent
4bdbc64bf7
commit
8b8e0929f1
|
|
@ -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<Pair<Enum, Boolean>> s_properties = new ArrayList<Pair<Enum, Boolean>>();
|
||||
|
||||
static {
|
||||
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.ACCOUNT_OBJ, Boolean.FALSE));
|
||||
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.USER_ID, Boolean.FALSE));
|
||||
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.ID, Boolean.TRUE));
|
||||
s_properties.add(new Pair<Enum, Boolean>(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<Pair<Enum, Boolean>> getProperties() {
|
||||
return s_properties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Pair<String, Object>> execute(Map<String, Object> 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<Pair<String, Object>> returnValues = new ArrayList<Pair<String, Object>>();
|
||||
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.JOB_ID.getName(), Long.valueOf(jobId)));
|
||||
returnValues.add(new Pair<String, Object>(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<Pair<String, Object>> execute(Map<String, Object> 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<Pair<String, Object>> returnValues = new ArrayList<Pair<String, Object>>();
|
||||
// returnValues.add(new Pair<String, Object>(BaseCmd.Properties.JOB_ID.getName(), Long.valueOf(jobId)));
|
||||
// returnValues.add(new Pair<String, Object>(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());
|
||||
// }
|
||||
//
|
||||
// }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue