bug 8493: Check for resource limit for create template from volume/snapshot.

This commit is contained in:
nit 2011-02-09 15:33:01 +05:30
parent 17eac0c17c
commit 3bd3dacf9e
3 changed files with 23 additions and 5 deletions

11
api/src/com/cloud/api/commands/CreateTemplateCmd.java Normal file → Executable file
View File

@ -30,6 +30,9 @@ import com.cloud.api.response.StoragePoolResponse;
import com.cloud.api.response.TemplateResponse;
import com.cloud.async.AsyncJob;
import com.cloud.event.EventTypes;
import com.cloud.exception.InvalidParameterValueException;
import com.cloud.exception.PermissionDeniedException;
import com.cloud.exception.ResourceAllocationException;
import com.cloud.storage.Snapshot;
import com.cloud.storage.Volume;
import com.cloud.template.VirtualMachineTemplate;
@ -170,7 +173,13 @@ public class CreateTemplateCmd extends BaseAsyncCreateCmd {
@Override
public void create(){
VirtualMachineTemplate template = _userVmService.createPrivateTemplateRecord(this);
VirtualMachineTemplate template = null;
try {
template = _userVmService.createPrivateTemplateRecord(this);
} catch (ResourceAllocationException ex) {
s_logger.warn("Exception: ", ex);
throw new ServerApiException(BaseCmd.RESOURCE_ALLOCATION_ERROR, ex.getMessage());
}
if (template != null){
this.setEntityId(template.getId());
} else {

View File

@ -99,9 +99,12 @@ public interface UserVmService {
* @param cmd the command object that defines the name, display text, snapshot/volume, bits, public/private, etc.
* for the private template
* @return the vm template object if successful, null otherwise
* @throws InvalidParameterValueException, PermissionDeniedException
* @throws ResourceAllocationException
* @throws PermissionDeniedException
* @throws InvalidParameterValueException
* @throws PermissionDeniedException
*/
VirtualMachineTemplate createPrivateTemplateRecord(CreateTemplateCmd cmd);
VirtualMachineTemplate createPrivateTemplateRecord(CreateTemplateCmd cmd) throws InvalidParameterValueException, PermissionDeniedException, ResourceAllocationException;
/**
* Creates a private template from a snapshot of a VM

View File

@ -1172,7 +1172,7 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager
@Override
public VMTemplateVO createPrivateTemplateRecord(CreateTemplateCmd cmd) throws InvalidParameterValueException, PermissionDeniedException {
public VMTemplateVO createPrivateTemplateRecord(CreateTemplateCmd cmd) throws InvalidParameterValueException, PermissionDeniedException, ResourceAllocationException {
Long userId = UserContext.current().getCallerUserId();
if (userId == null) {
userId = User.UID_SYSTEM;
@ -1182,7 +1182,7 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager
boolean isAdmin = ((account == null) || isAdmin(account.getType()));
VMTemplateVO privateTemplate = null;
UserVO user = _userDao.findById(userId);
if (user == null) {
@ -1231,6 +1231,12 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager
throw new InvalidParameterValueException("Failed to create private template " + name + ", a template with that name already exists.");
}
AccountVO ownerAccount = _accountDao.findById(volume.getAccountId());
if (_accountMgr.resourceLimitExceeded(ownerAccount, ResourceType.template)) {
ResourceAllocationException rae = new ResourceAllocationException("Maximum number of templates and ISOs for account: " + account.getAccountName() + " has been exceeded.");
rae.setResourceType("template");
throw rae;
}
// do some parameter defaulting
Integer bits = cmd.getBits();
Boolean requiresHvm = cmd.getRequiresHvm();